I wanted to inform myself about Coded UI Tests. So I created some coded ui tests and played a bit around.
But now I'm at a point I can't help myself. I have the following problem. When I do record some actions from a dotNet 4.5 program, the recorder locates the forms, buttons, textfields and so on via SearchProperties[WinText.PropertyNames.Name] That's definitely uncool.
Imagine you want to do an assertation on a text-control. The WinText.PropertyNames.Name would be the calculated value. So if you change some input values in the form which affect the calculation, the WinText.PropertyNames.Name will be changed to the new value and the coded ui test wont find the text-control with the changed value because it searched for the WinText.PropertyNames.Name of the control instead of the WinText.PropertyNames.ControlName.
My Question is, is there a way to change the configuration of the standard search property the coded ui test builder uses to locate the needed objects?
Because its a big effort to change this for every recorded object, afterwards.
PS: I'm not talking about the C:\Program Files (x86)\Common Files\microsoft shared\VSTT\11.0\IEPropertyConfiguration.xml which takes only effect on ie sites =)
Lets make things clear:
Here's my code:
public class UIItem100180Window : WinWindow
{
public UIItem100180Window(UITestControl searchLimitContainer) :
base(searchLimitContainer)
{
#region Search Criteria
this.SearchProperties[WinWindow.PropertyNames.ControlName] = "wert2Praemie12";
this.WindowTitles.Add("Any Title");
#endregion
}
#region Properties
public WinText UIItem100180Text
{
get
{
if ((this.mUIItem100180Text == null))
{
this.mUIItem100180Text = new WinText(this);
#region Search Criteria
this.mUIItem100180Text.SearchProperties[WinText.PropertyNames.Name] = "1.001,80";
this.mUIItem100180Text.WindowTitles.Add("Any Title");
#endregion
}
return this.mUIItem100180Text;
}
}
#endregion
#region Fields
private WinText mUIItem100180Text;
#endregion
}
I got a parent control(UIItem100180Window: WinWindow) which creates my Text-Control(UIITem100180Text: WinText) on UI, that represents the value of my calculation. The problem is both have the same .PropertyNames.ControlName =).
That means if I'm going to add:
this.mUIItem100180Text.SearchProperties[WinText.PropertyNames.ControlName] = "wert2Praemie12";
it will find alltime my parent-control: WinWindow. If I'm not adding it and if the value of the calculation changes (maybe calculation method was edited,..), and its not equal to this.mUIItem100180Text.SearchProperties[WinText.PropertyNames.Name] = "1.001,80"; the control wont be found and an errorMessage "..control not found" will be thrown.
Later I will get problems at this line, checking the Value of my text-control(WinText):
public void VerifyTextControl()
{
#region Variable Declarations
WinText uIItem100180Text = this.xxx.UIItem100180Window.UIItem100180Text;
#endregion
// Verify that the 'DisplayText' property of '1.001,80' label equals '1.001,80'
Assert.AreEqual("1.001,80", uIItem100180Text.DisplayText, "järhlich muss 1.001,80 € betragen!");
}
my uIItem100180Text:WinText is handled as WinWindow because it finds only my parent-control when I'm searching for the PropertyNames.ControlName, like I said both got the same name. So I can't use the .DisplayText-method, and it ends in an error Message.
The best solution is, do your assertation on parent control, because it also keeps the value of the text-control and you wont have such problems like I had have.
Maybe this post is helpful for people who struggle at the same point.
Note:
UITestControl->WinControl->WinText
UITestControl->WinControl->WinWindow
Update 2: !! Problem solved !!
Im gonna try to explain.
Heres our example:
public class UITextWindow : WinWindow
{
public UITextWindow(UITestControl searchLimitContainer) :
base(searchLimitContainer)
{
#region Search Criteria
this.SearchProperties[WinWindow.PropertyNames.ControlName] = "label1";
this.WindowTitles.Add("Form1");
#endregion
}
#region Properties
public WinText UITextText
{
get
{
if ((this.mUITextText == null))
{
this.mUITextText = new WinText(this);
#region Search Criteria
this.mUITextText.SearchProperties[WinText.PropertyNames.Name] = "text";
this.mUITextText.WindowTitles.Add("Form1");
#endregion
}
return this.mUITextText;
}
}
The problem is that the coded ui test builder generates a SearchProperty to find our Wintext UITextText. But thats not needed because our Wintext UITextText inherits the needed property(ControlName) from UITextWindow:WinWindow(parent) in the cunstructor:
this.mUITextText = new WinText(this);
So theres no need to add an additional search property to find the child element, because both got the same controlName.
That prevents that the system throws an "..ElementNotFound"-Exception when the value of the Label changes. Instead we will get an "..AssertationFailure"-Exception, wich informs us that somethings wrong with the value of our label. Thats the standard way how it should work.
Just delete this line and everything will be fine:
this.mUITextText.SearchProperties[WinText.PropertyNames.Name] = "text";
I dont know why the coded ui builder automatically adds this searchproperty to Labels to get us in trouble :/
Update 1:
System.Windows.Forms.Label is responsible for the troubles. If you do an assert on a label with coded ui tests and the value of the label changes one day, you will have the same problems like i have :/