I'm trying to automate a windows based application Using Coded UI. In this application some control are not accesible so i have used UI automatin element to identify the control.
First Screen Contains List of Employees in the form Grid.
New TabPage we have TextBox and some other controls such as Checkbox and ListItem.
List Item are detectable using coded ui but TextBox are not detectable with coded ui so we have used UI Automation Element for this.
[CodedUITest]
public class CodedUITest1
{
[TestMethod]
public void CodedUITestMethod2()
{
LaunchApplication(); //done using coded ui
Login()//done using Coded UI;
ClickonEmpListTab()//Done Using Coded UI
SelectEmployee()//done using Coded UI
//now new Tab is created in application
empoyeeUICodedUIControl.CurrentEmpComboBox.Checked= true; //done using coded ui
empoyeeUIAutomationControl.EnterText(empoyeeUIAutomationControl.FirsName,"MyFirstName"); // done using coded ui
empoyeeUIAutomationControl.EnterText(empoyeeUIAutomationControl.LastName,"MyLastName"); // done usin coded ui
}
private EmployeeUIAutomationUIMap _employeeUIAutomationcontrol;
public EmployeeUIAutomationUIMap empoyeeUIAutomationControl
{
get
{
if(_employeeUIAutomationcontrol == null)
{
_employeeUIAutomationcontrol = new EmployeeUIAutomationUIMap();
}
return _employeeUIAutomationcontrol;
}
}
private EmployeeUIMap _employeeUICodedUIcontrol;
public EmployeeUIMap empoyeeUICodedUIControl
{
get
{
if (_employeeUICodedUIcontrol == null)
{
_employeeUICodedUIcontrol = new EmployeeUIAutomationUIMap();
}
return _employeeUICodedUIcontrol;
}
}
// Class contains controls which are detectable using coded ui
public class EmployeeUIMap
{
public WinTabPage ListEmpmTab
{
get
{
WinTabPage tab = new WinTabPage();
return tab;
}
}
public WinCheckBox CurrentEmpComboBox
{
get
{
WinCheckBox combox = new WinCheckBox();
return combox;
}
}
}
//Class contains Control which are detectable using UI Automation Element
public class EmployeeUIAutomationUIMap {
public Condition GetCondition(AutomationProperty propery,string value)
{
Condition cond = new PropertyCondition(AutomationElement.AutomationIdProperty, value);
return cond;
}
public AutomationElement GetElement(AutomationElement parentControl,Condition cond)
{
return parentControl.FindFirst(TreeScope.Descendants, cond);
}
public AutomationElement MainRootElement
{
get
{
return AutomationElement.RootElement;
}
}
public AutomationPattern GetPattern(AutomationElement element)
{
//returns the pattern as per element
return pattern;
}
public void EnterText(AutomationElement element,string value)
{
var pattern = GetPattern(element);
pattern.SetValue = value;
}
public AutomationElement FirsName
{
var cond=GetCondition(AutomationElement.AutomationIdProperty,"FNID");
var element=GetElement(MainRootElement,cond);
return element;
}
public AutomationElement LastName
{
var cond=GetCondition(AutomationElement.AutomationIdProperty,"LNID");
var element=GetElement(MainRootElement,cond);
return element;
}
}
When i Run my TestMethod it works fine till clicking on selected employee. once new tab page is opened i perform operation on checkBox using coded ui which workds, but when i try to enter text in textbox using UI automation. It throws null exception in GetElement() Method on element.
So i commented below code and manually opened the application till new tab is opened. and run the test method, now uiAuomation recognizes the element and perform the respective action.
[TestMethod]
public void CodedUITestMethod2()
{
/*
LaunchApplication(); //done using coded ui
Login()//done using Coded UI;
ClickonEmpListTab()//Done Using Coded UI
SelectEmployee()//done using Coded UI
*/
empoyeeUICodedUIControl.CurrentEmpComboBox.Checked= true; //done using coded ui
empoyeeUIAutomationControl.EnterText(empoyeeUIAutomationControl.FirsName,"MyFirstName"); // done using UIAutomation element
empoyeeUIAutomationControl.EnterText(empoyeeUIAutomationControl.LastName,"MyLastName"); // done usin Automation Element }
When i run my complete test method UIAutomation is not able to identify the control.
Please help me to resolve the problem.