Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Coded UI Tests: Windows Phone Utilities

DZone's Guide to

Coded UI Tests: Windows Phone Utilities

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

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.

// 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

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Published at DZone with permission of Jon Antoine, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}