Using Coded UI to test XAML-based Windows Phone apps

Using Coded UI to test XAML-based Windows Phone apps

Rate This
  • Comments 28

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 2 and 3 and type the answer here:
  • Post
  • Thanks for this,very helpful

  • Hi prachi ,

    I deployed an application on the w8.1 phone emulator and then tried to generate code using the coded ui test builder but as soon as the test builder gets initiated , i'm getting the following error - "Coded UI Test Builder could not connect to the emulator. Please restart Coded UI Test Builder and the Emulator and try again."

    please let me know how to connect the test builder to the emulator in order to generate my test script.

  • Hi prachi ,

    The app is installed on my emulator but still when i'm trying to launch the app from coded ui i'm getting an error with the following exception <b>" Message=Exception occurred while trying to launch the app. Verify that the app is installed: Value does not fall within the expected range., AutomationId of Tile: default'"</b>.

    Thanks in advance !

  • Thanks for the answers prachi - very useful

    I have some more questions:

    - is there any way to determine the device profile at runtime - e.g. what model the device is? whether it's an emulator or a real device? the screen size?

    - what's the best way to determine the rotation - portrait or landscape - at runtime? Is there even a way to programmatically change it?

    Thanks again for any help :)

    Stuart

  • @Sai Kiran,

    Have you verified the parameter you are passing to app launch? This value is the same as the AutomationId of the tile for your app. This is illustrated in the "launching an app" section in the post.

  • @Stuart: No, we don't have a way to determine the device model etc. Can you specify the scenario for which you need this information when executing tests?

    Currently, there's no way to determine / change the rotation information. It's on our backlog. I recommend that you raise a user voice item for this, so that others who need it can vote for it as well. This will help drive the prioritization of this item in our backlog.

  • Hi Prachi,

    Thanks a lot for the decent demo in build. Can you please let me know whether I should buy Ultimate/Premium versions just to run automation? I mean, can I have 1 Premium license with that I'll develop my Coded UI recording. Can I play those CodedUI recording on some test system where i do not have Premium licenses?

    can I use only MStest to execute coded UI recrodings?

    Thanks in advance

    -Mahendar

  • When using CodedUI for WindowsPhone, I frequently seem to get the tests into a state where they won't start. It's not obvious what is causing this - when I try from the command line I get a stack trace starting:

    "Unhandled Exception: System.Exception: TestHostCommand_LaunchTestHostAgent failed on the device side with error code -2147024629

      at Microsoft.VisualStudio.TestPlatform.Utilities.PhoneTestHostExecutorLauncher.LaunchTestHost(IDevice _deviceInstance, TestHostCommandID command)"

    This state seems to persist across emulator restarts, and I also see the same sort of problem on real phones too - the problem eventually clears, but I''m not sure why...

    I've not worked out what causes this. Are there any suggestions?

    Thanks again

    Stuart

  • Hi Stuart,

    Which version of VS are you using? If the issue still persists, can you please enable logs for vstest.executionengine.exe and QTAgent32.exe and post the logs along with the issue to vstestsup@microsoft.com ?

    To enable logs you can refer: blogs.msdn.com/.../how-to-enable-ute-logs.aspx and blogs.msdn.com/.../how-to-enable-test-agent-logs.aspx

    Thank you.

    Vaibhav

  • Hi Prachi,

    It looks like Coded UI unable to interact with out of process Shells controls.

    Here's more details: stackoverflow.com/.../coded-ui-unable-to-interact-with-out-of-process-shells-controls

    To reproduce - add a line into some event handler in your target app "var sc = Windows.Media.Capture.ScreenCapture.GetForCurrentView();", invoke that event, see the message, and try to automate that message with Coded UI.

  • Hi,

    My apps is XAML-based apps which using a lot of custom controls, when i using the coded ui test builder  to  spy UI elements on the app, it says my app is not XAML-based apps, so i want to ask if the coded ui test builder has some restrictions on custom control?

    Thanks in advance!

  • Is there a way to launch the emulator from the Coded UI API. If so please post the sample code

  • HI Prachi

    How to collect reports for passed and failed test for coded UI

    Is there any conditions to be enabled to collect reports

    Please advice

    Thanks

    Shasan

Page 2 of 2 (28 items) 12