Coded UI Tests : WPF Utilities

, , 4 Comments

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 :

  1. List, Tree and Menu emptiness tests as User extension methods.
  2. Helper to get all the AutomationIds of a control collection.
  3. Helper to get all the children control of a specific type.
  4. Easier set text syntax with fallback method: youpi !
  5. Easier set value syntax.
  6. Easier expand and collapse syntax.
  7. Easier selection syntax.
  8. 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.
[csharp]
// WpfTree treeView = … ;

//Tell me if you are empty my dear
bool isEmpty = treeView.IsEmpty();
Assert.IsTrue(isEmpty);
[/csharp]

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.
[csharp]
// WpfControl control= … ;

//Rertrieve the ids
List<string> idsOfChildren = control.GetChildrenAutomationIds();
[/csharp]

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.

[csharp]
// 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();
[/csharp]

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 :

[csharp]
//WpfControl control = …;

//Set the text
bool isTextSet= control.TrySetText("I love Coded UI tests");
Assert.IsTrue(isTextSet);
[/csharp]

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.
[csharp]
//WpfControl control = …;

//Set the value
bool isValueSet= control.TrySetValue("I want to set this value.");
Assert.IsTrue(isValueSet);
[/csharp]

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.
[csharp]
//WpfControl control = …;

//Expanding
bool isExanpded = control.TryExpand();
Assert.IsTrue(isExanpded);

//Collapsing
bool isCollapsed = control.TryCollapse();
Assert.IsTrue(isCollapsed);
[/csharp]

Easier selection syntax

Select a control in one line of code !
[csharp]
//WpfControl control = …;

//Select the control
bool isSelected = control.TrySelect();
Assert.IsTrue(isSelected);
[/csharp]

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.
[csharp]
//WpfControl control = …;

//Invoke
bool isInvoked = control.TryInvoke();
Assert.IsTrue(isInvoked);
[/csharp]

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.

 

4 Responses

  1. David A R Kemp

    18/01/2012 15 h 28 min

    Go on – put it all on github. Then other's can (easily) contribure too.

  2. ThreeLittleBirds

    25/09/2012 17 h 04 min

    Utility looks great! Can I use it to iterate through list items to see if its the current selected item? Thanks!

  3. Hoan Nguyen

    02/04/2013 4 h 12 min

    Hi Jonathan ANTOINE,

    I am using CodedUI to automate on a WPF application, I have 2 buttons in a form, I want to verify the text of each button. But when running only have passed script. Because The actual result of the second script is always the actual of the first button. So how I get the second text to verify? Please help me.

    (I saw they are the same AutomationID when I record)

Comments are closed.