I played a lot with Coded UI Tests API on WPF applications these last weeks.
It turned out that this is a really powerful framework available to use but that we need to write a lot of code ourselves even for simple things.
You may already know the coded ui test utilities of Gautam Goenka available on his blog.
On this blog post, I want to introduce a set of complementary helper methods that you can use in your project. The links to the binaries/sources are at the end of the post.
Here is a list of the available features :
- List, Tree and Menu emptiness tests as User extension methods.
- Helper to get all the AutomationIds of a control collection.
- Helper to get all the children control of a specific type.
- Easier set text syntax with fallback method: youpi !
- Easier set value syntax.
- Easier expand and collapse syntax.
- Easier selection syntax.
- Easier invoke syntax.
Features, code sample and comments
List, Tree and Menu emptiness tests as User extension methods
It’s always useful to know if a listbox is empty or not. Now you can use these simple methods.
// WpfTree treeView = ... ; //Tell me if you are empty my dear bool isEmpty = treeView.IsEmpty(); Assert.IsTrue(isEmpty);
Helper to get all the AutomationIds of a control children
Sometimes you store information trough AutomationIds. This method will help you to retrive them in a simple way.
// WpfControl control= ... ; //Rertrieve the ids List<string> idsOfChildren = control.GetChildrenAutomationIds();
Helper to get all the children control of a specific type
This is a recursive method which can helps you in several scenarii. Be sure to notice that there is an override which takes a boolean as a parameter. If it sets to false, the method will return ALL the children and not only the direct one.
Also, be sure to understand that you won’t be able to call the Find method on these controls as they do not have defined search properties.
// WpfControl control= ... ; //Get only the direct children List<WpfEdit> children= control.GetChildren<WpfEdit>(); foreach(WpfEdit edit in children) SomethingCool(); //Get all the children in the tree below the control List<WpfEdit> children= control.GetChildren<WpfEdit>(onlyDirectChildren: false); foreach(WpfEdit edit in children) SomethingCoolToo();
Easier set text syntax with fallback method: youpi !
I found out trying to set the text of a TextBox that you can end up waiting eternity for the text being really set.
I do so tried to create an helper method which can be faster using the automation pattern. I then try to use the value pattern but oddly, it wasn’t available on the TextBox automation peer.
I finally find on a MSDN code sample comments that “Elements that support TextPattern do not support ValuePattern and TextPattern does not support setting the text of multi-line edit or document controls”. So if the control implements the TextPattern, you have to use a fallback method which is in fact simulate keyboard inputs.
The helper method just do that :
//WpfControl control = ...; //Set the text bool isTextSet= control.TrySetText("I love Coded UI tests"); Assert.IsTrue(isTextSet);
Easier set value syntax
Testing if a control implements a pattern and then trying to call it represents a lot of code line.
With this helper you just have one single line method call.
//WpfControl control = ...; //Set the value bool isValueSet= control.TrySetValue("I want to set this value."); Assert.IsTrue(isValueSet);
Easier expand and collapse syntax
Same as the previous, one single line to collapse/expand a control. If the control does not implements the pattern, the method will just returns false.
//WpfControl control = ...; //Expanding bool isExanpded = control.TryExpand(); Assert.IsTrue(isExanpded); //Collapsing bool isCollapsed = control.TryCollapse(); Assert.IsTrue(isCollapsed);
Easier selection syntax
Select a control in one line of code !
//WpfControl control = ...; //Select the control bool isSelected = control.TrySelect(); Assert.IsTrue(isSelected);
Easier Invoke syntax
Instead of simulate a mouse click, you can now use this method which will use the invoke pattern even if the control is hided.
//WpfControl control = ...; //Invoke bool isInvoked = control.TryInvoke(); Assert.IsTrue(isInvoked);
Were are the binaries ?
The binaries and the, well commented, soure code can be found at this adress : https://www.dropbox.com/s/gmpybsviirm3bqd#view:list
Don’t forget to support me by registering to Dropbox using this link.