Using Coded UI to test XAML-based Windows Phone apps

Using Coded UI to test XAML-based Windows Phone apps

Rate This
  • Comments 27

In order to ship a great quality app to the Store, it is essential that you test it and flush out the bugs. End to end scenario tests when automated can help you ensure that any regressions in the app are caught early. With Visual Studio 2013 Update 2, you can now write automated end-to-end tests for your app using Coded UI Test. In Visual Studio 2013, Coded UI Test support for XAML-based Windows Store apps was enabled and the experience to use Coded UI test for Phone apps is largely the same. This post walks you through the essentials of creating a Coded UI test for your XAML-based Phone app.

Getting Started 

Writing a Coded UI test for XAML based Phone app is easy. When you install Visual Studio 2013 Update 2 RC, you will see a new template under the Windows Phone apps node that lets you create Coded UI Test for your Phone app.

After you create a new project, you can use the Coded UI test builder to spy on the UI controls in your app that you want to interact with. You can also use the builder to build a UIMap, a repository of UI controls, code for which is automatically generated for you.

Coded UI test builder connects to a Windows Phone Blue emulator instance running on your machine. You can start building your UIMap by dragging and dropping the cross-hair tool on
the control of interest. You can also use keyboard shortcut keys Ctrl+I (spy) and Ctrl+Shift+I (Add to UIMap) for the same – just hover over the control of interest in the emulator with your mouse and use the shortcut keys.  A couple of points to note:

  1. If you use the cross-hair tool to spy on UI elements that are outside of the emulator, the builder will not detect those
    controls. It will tell you that the control co-ordinates are outside of the emulator.
  2. The builder can spy UI elements when the app is running on the emulator. Builder cannot be used to spy UI elements when the app
    is running on a physical Phone device.

If you want to author your test from scratch and not use the builder to build a UIMap, you can choose the option to ‘Manually edit the test’.

Working with UI elements of the app

Coded UI test makes specialized classes available so that a rich set of properties is available for working with the UI elements. In the case of XAML apps, the specialized classes
for all the controls are prefixed with Xaml. XamlButton, XamlEdit, XamlList etc., are some examples. All these classes are available under the Microsoft.VisualStudio.TestTools.UITesting.WindowsRuntimeControls namespace. This is the same set of classes that are available for Coded UI test for Windows apps.

WebView control used to host HTML content in a XAML app is currently not supported.

You can also interact with Shell controls – controls that are not XAML, but essential for testing your app E2E – such as the tiles, confirmation dialogs, etc. These controls are provided by
the OS and are not XAML. These will be identified as UITestControl. The Shell controls are identified differently than big Windows, because the UI technology behind these controls is different on the two platforms. On big Windows, the shell controls such as controls in the settings charm, tiles etc. are identified as a DirectUIControl.

Performing actions on UI elements

Once you have built the UIMap to identify UI controls, you can start writing code to act on these controls. A series of actions will make up the scenario you want to test.

Actions on controls can be performed in two ways:

1. Touch gestures on controls: All gestures supported by the Windows platform are supported. Gesture class exposes all the touch gestures. For e.g., to invoke a button, the action code would be Gesture.Tap(myButton);

2. Properties and methods on control classes. Each specialized control class exposes a bunch of properties and methods that you can use to obtain information about or interact with the control. For e.g., to input text “ABC” in an edit control, you can use

myXamlEditControl.Text=”ABC”;

Launching an app

Launching an app on Windows Phone is similar to launching a Store app on big Windows. You can launch an app in one of two ways:

  1. Tapping on the app tile in the apps list
  2. Using the XamlWindow.Launch API. The Launch method takes in the unique identifier for your app. Just as in big Windows, the AutomationId for the app tile on Start screen or app item in the apps list is the unique identifier for your app. You can obtain this string value by observing the app tile in CUIT builder and copy/pasting the AutomationId property of the tile
    control, as seen in the following screenshot:

Executing Tests

Tests can be executed from within Visual Studio using Test Explorer or from the command-line. Coded UI tests can be executed on the emulator or the device. When executing tests
from Visual Studio, use the device toolbar to specify the target device where the tests should execute.

Tests can also be executed from the command-line using vstest.console.exe. You can specify the target device for test execution using a runsettings file.

vstest.console.exe “pathToYourCodedUITestDll” /settings:devicetarget.runsettings

Sample runsettings file:

<?xml version="1.0" encoding="utf-8"?>

<RunSettings>

<MSPhoneTest>

<!--to specify test execution on device, use a TargetDevice option as follows-->

<TargetDevice>Device</TargetDevice>

<!--to specify an emulator instead, use a TargetDevice option like below-->

<!--<TargetDevice>Emulator 8.1 WVGA 4 inch 512MB</TargetDevice>-->

</MSPhoneTest>

</RunSettings>

Data driving a Coded UI Test Method

Once you have written a scenario test, you might want to run the same test multiple times with different sets of data to test different conditions. Data-driving comes in handy for such cases. Data-driven Coded UI tests for Windows Phone can be defined using the DataRow attribute on a test method.

[DataRow(1, 2, DisplayName = "Add positive numbers")]

[DataRow(-1, -2, DisplayName = "Add negative numbers")]

[TestMethod]

public void DataDrivingDemo_MyTestMethod(int x, int y)

Data that drives your test is defined inline using the DataRow attribute. In the above example, x and y would obtain the values of 1 and 2 respectively for the first iteration and -1 and -2 resp. for the second iteration of the test.  

Differences between Coded UI for XAML-based apps for Windows and Windows Phone

While Coded UI test for XAML apps on big Windows and Windows Phone is largely the same in capabilities, the table below indicates the differences between them.

Feature

Coded UI for apps on Windows

Coded UI for apps on Windows Phone

Target for running tests

Local or remote computer.

Remote computers can be specified
  when running tests using the TC/TA.

 Emulator or Phone

Execute from the command-line

Settings file not required to specify
  target

runsettings file required to specify
  target

Specialized classes for Shell
  Controls

DirectUI class

UITestControl class

WebView control in a XAML app

Supported, HTML elements can be
  interacted with using Html* specialized classes

Not supported.

Execute automated tests from MTM

Supported

Not supported.

Data-driving tests

Using external
  data-sources
, using DataSource attribute on a test method.

Data is specified inline, using DataRow
  attribute on a test method, as indicated earlier.

 Limitations

1. Recording of action steps to create Coded UI test for XAML based Phone apps is not supported.

2. Only XAML based store apps are supported. Silverlight and HTML 5 based apps cannot be tested using Coded UI.

 

 

Leave a Comment
  • Please add 8 and 2 and type the answer here:
  • Post
  • Are all the rows in the differences table correct? Can you really run Windows tests on a phone?

  • Hi Prachi,

    Thanks for writing this blog.

    I've installed Visual Studio 2013 update 2. But I cannot find Windows Phone Apps node. Please let me know if i need to install any additional component.

    Note: My OS is Windows 8

  • Sorry for Typing error!!

    I've installed Visual Studio 2013 update 2. (www.microsoft.com/.../details.aspx)

    But I cannot find Coded UI Test Project (Windows Phone) under Windows Phone Apps node. Please let me know if i need to install any additional component.

    Note: My OS is Windows 8

  • @Matt: Thanks for the question. Windows and Phone columns got mixed up in the first row of the table. I have corrected it now.

  • @Satish: You need to be on Windows 8.1 and have Phone development tools installed in order to develop Phone apps for Windows Phone 8.1 as well as write Coded UI test for them. Coded UI ships in Visual Studio Ultimate and Premium editions.

  • Thanks for your reply Prachi :)

    I am not able to find official download link for Windows Phone 8.1 SDK.

    Appreciate if you could share the same.

  • I really enjoyed seeing this presentation at Build last week. It's exciting to see the direction the development tools are going in. Would it be possible for you to share the project you used in the demo, please?

  • Hi Ms. Bora,

    Thanks for the clear Build presentation, and this great follow-up blog! I'd like to formally echo Simply Ged's request. A copy of your project (or something similar) would be so very helpful to me - not only for the testing sample code, but for the mvvm-with-database code, as well. I'm currently working on an app that includes a relational database, and I'm having a tough time finding samples that cover that level of complexity for Windows Phone 8/8.1.

    Can we bribe you with cookies or something? ;)

  • @ Satish: Tools for development for Windows Phone 8.1 are bundled in the Visual Studio 2013 Update 2 RC. Download link is www.microsoft.com/.../details.aspx

  • @Simply Ged, @Pretty Please?,

    Yes, I can make the demo project available. I am traveling right now, so I should have updates in this regard late next week.

    @Pretty Please? : Cookies are awesome :-), even virtual ones would work. I love double chocolate chip ;-)

  • @Pretty please: Forgot to mention that my demo app did not use a database. All the data is written to files in the app's isolated storage.

    For folks looking for help with MVVM and sample Store app using it, Prism for Windows Runtime is a great resource. prismwindowsruntime.codeplex.com

  • Is there a way to reset the state of the app after each test?  

  • Excellent, thanks! Dunno why I thought you used a database. *shrug* I'm still looking forward to your post.

    (PS: Bribe delivery --> onedrive.live.com/redir)

  • Thanks for the blog Prachi and for the BUILD sessions too.

    I'm working through some WinPhone Coded Ui tests at the moment as a follow up to github.com/.../WindowsPhoneTestFramework, and your blog seems to provided the best documentation available :)

    Is it OK if I ask a few technical questions?

    1. Is there any way to get screenshots off of the phone - I've dug into the resources of WindowsPhone.CodedUITestFramework and seen that there are some "ScreenShotMessage" and "ScreenShotFailure" resource messages - but can't see any API for screenshots - is there one? I've seen msdn.microsoft.com/.../microsoft.visualstudio.testtools.uitesting.uitestcontrol.captureimage.aspx - but can't find that in code.

    2. Is there any way to send keyboard messages - I've seen I can set text on a XamlEdit but I also want to be able to send Key strokes - especially "Enter/Go" so that I can trigger a navigation. Old CodedUI seems to have a Keyboard class - but that doesn't seem to be available in WinPhone. There seems to be a SendKeysAction class available but I can't work out how to use it from C# code. Are there any ideas here? Thanks.

    3. Calls to Gesture seem really fast - but searching for controls or iterating over controls is a lot slower - are there any tips for speeding up operation?

    4. Are there any samples available for how to use more native elements - things like MessageBox's, notification Toasts, ApplicationBar buttons and menus, the phone home screen, etc - the more samples the better really :)

    5. If we find suspected issues or bugs then what's the best way of reporting them? Is there a specific Connect category we should use?

    Sorry for so many questions! Overall, I love having official CodedUI support - thanks for the work you and your team are putting in :)

    Stuart

  • @Stuart,

    1. Currently, there is no way to take screenshots at will on the Phone. By default, a screenshot will be taken if the test fails.

    2. Keyboard is only available on the desktop. Is there no other affordance in your app to trigger a navigation such as a submit button / next etc. that you could tap on?

    3. Some guidelines to improve Coded UI test has been blogged at blogs.msdn.com/.../guidelines-on-improving-performance-of-coded-ui-test-playback.aspx.

    4. Messageboxes, appbar controls, start screen etc. can be automated in the same way - capture them using the builder and use the Gestures / APIs to interact with them. Some controls such as Start screen are not XAML, so they will be recognized as UITestControl. There's no difference otherwise. Let me know if you run into any specific issues with these. Testing notification toasts is not supported.

    5. If you find any issues / bugs, please file them on Connect. Adding Coded UI test to the title should route it to my team. For any suggestions / feature requests, please file on uservoice.

Page 1 of 2 (27 items) 12