Testing Windows Phone Apps with Coded UI Tests

Visual Studio 2013

Use coded UI tests to test your Windows Phone apps.

  1. Create a new project for a blank Windows Phone app using either Visual C# or Visual Basic template.

    Create a new Windows Phone app
  2. In Solution Explorer, open MainPage.xaml. From the Toolbox, drag a button control and a textbox control to the design surface.

    Add contols to MainPage.xaml
  3. In the Properties window, name the button control.

    Name the button control
  4. Name the textbox control.

    Name the textbox control
  5. On designer surface, double-click the button control and add the following code:

    private void button_Click_1(object sender, RoutedEventArgs e)
    {
        this.textBox.Text = this.button.Name;
    }
    
  6. Press F5 to run your Windows Phone app in the emulator and verify that it’s working.

    Run the Windows Phone app
  7. Exit the emulator.

  • Before a coded UI test can map an app’s controls, you have to deploy the app.

    Deploy the Windows Phone app

    The emulator starts. The app is now available for testing.

    App deployed on emulator

    Keep the emulator running while you create your coded UI test.

  1. Add a new coded UI test project to the solution with the Windows Phone app.

    Create new coded UI  test for Windows Phone
  2. Choose to edit the UI map using the cross-hair tool.

    Generate coded UI test using cross-hair tool.
  3. Use the cross-hair tool to select the app, then copy the value for the app’s AutomationId property, which will be used later to start the app in the test.

    Copy the app's AutomationId value
  4. In the emulator, start the app and use the cross-hair tool to select the button control. Then add the button control to the UI control map.

    Use the cross-hair tool to map controls
  5. To add the textbox control to the UI control map, repeat the previous step.

    Use the cross-hair tool and map textbox control
  6. Generate code to create code for changes to the UI control map.

    Generate code from the builder
  7. Use the cross-hair tool to select the textbox control, and then select the Text property.

    Select the Text property
  8. Add an assertion. It will be used in the test to verify that the value is correct.

    Add assertion to the test
  9. Add and generate code for the assert method.

    Generate code for the assertion
  10. Visual C#

    In Solution Explorer, open the UIMap.Designer.cs file to view the code you just added for the assert method and the controls.

    Visual Basic

    In Solution Explorer, open the CodedUITest1.vb file. In the CodedUITestMethod1() test method code, right-click the call to the assertion method that was automatically added Me.UIMap.AssertMethod1() and choose Go To Definition. This will open the UIMap.Designer.vb file in the code editor so you can view the code you added for the assert method and the controls.

    Caution note Caution

    Do not modify the UIMap.designer.cs or UIMap.Designer.vb file directly. If you do this, the changes to the file will be overwritten each time the test is generated.

    Assert method

    public void AssertMethod1()
    {
        #region Variable Declarations
        XamlEdit uITextBoxEdit = this.UIApp1Window.UITextBoxEdit;
        #endregion
    
        // Verify that the 'Text' property of 'textBox' text box equals 'button'
        Assert.AreEqual(this.AssertMethod1ExpectedValues.UITextBoxEditText, uITextBoxEdit.Text);
    }
    

    Controls

    #region Properties
    public virtual AssertMethod1ExpectedValues AssertMethod1ExpectedValues
    {
        get
        {
            if ((this.mAssertMethod1ExpectedValues == null))
            {
                this.mAssertMethod1ExpectedValues = new AssertMethod1ExpectedValues();
            }
            return this.mAssertMethod1ExpectedValues;
        }
    }
    
    public UIApp1Window UIApp1Window
    {
        get
        {
            if ((this.mUIApp1Window == null))
            {
                this.mUIApp1Window = new UIApp1Window();
            }
            return this.mUIApp1Window;
        }
    }
    #endregion
    
    #region Fields
    private AssertMethod1ExpectedValues mAssertMethod1ExpectedValues;
    
    private UIApp1Window mUIApp1Window;
    #endregion
    
  11. In Solution Explorer, open the CodedUITest1.cs or CodedUITest1.vb file. You can now add code to the CodedUTTestMethod1 method for the actions needed to run the test. Use the controls that were added to the UIMap to add code:

    1. Launch the Windows Phone app using the automation ID property you copied to the clipboard previously:

      XamlWindow myAppWindow = XamlWindow.Launch("ed85f6ff-2fd1-4ec5-9eef-696026c3fa7b_cyrqexqw8cc7c!App");
      
    2. Add a gesture to tap the button control:

      Gesture.Tap(this.UIMap.UIApp1Window.UIButtonButton);
      
    3. Verify that the call to the assert method that was automatically generated comes after launching the app and tap gesture on the button:

      this.UIMap.AssertMethod1();
      

    After the code is added, the CodedUITestMethod1 test method should appear as follows:

    [TestMethod]
    public void CodedUITestMethod1()
    {
        // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
    
        // Launch the app.
        XamlWindow myAppWindow = XamlWindow.Launch("ed85f6ff-2fd1-4ec5-9eef-696026c3fa7b_cyrqexqw8cc7c!App");
    
        // Tap the button.
        Gesture.Tap(this.UIMap.UIApp1Window.UIButtonButton);
    
        this.UIMap.AssertMethod1();
    }
    

  • Build your test and then run the test using the test explorer.

    Build and run the test using Test Explorer

    The Windows Phone app launches, the action to tap the button is completed, and the textbox’s Text property is populated and validated using the assert method.

    Running Winodws Phone test

    After the test is finished, the test explorer confirms that the test passed.

    Test Explorer results

To test different conditions, a coded UI test can be run multiple times with different sets of data.

Data-driven Coded UI tests for Windows Phone are defined using the DataRow attribute on a test method. In the following example, x and y use the values of 1 and 2 for the first iteration and -1 and -2 for the second iteration of the test.

[DataRow(1, 2, DisplayName = "Add positive numbers")]
[DataRow(-1, -2, DisplayName = "Add negative numbers")]
[TestMethod]
public void DataDrivingDemo_MyTestMethod(int x, int y)

A: Yes, the coded UI test builder requires that an emulator be running and the app be deployed to it. Otherwise, it will throw an error message saying that no running emulator could be found.

A: Either option is supported. The target for test execution is selected by changing the emulator type or selecting device in the device toolbar. If Device is selected, a Phone Blue device needs to be connected to one of the machine’s USB ports.

Select the emulator version, or physcial device

A: No, the Coded UI Test Project (Windows Phone apps) template is only available on Windows 8.1.

A: No, the builder will not detect them.

A: No, The builder can only map UI elements if your app has been deployed to the emulator.

A: Any code changes you make in the UIMapDesigner.cs file will be overwritten every time you generate code using the UIMap - Coded UI Test Builder. If you have to modify a recorded method, you must copy it to UIMap.cs file and rename it. The UIMap.cs file can be used to override methods and properties in the UIMapDesigner.cs file. You must remove the reference to the original method in the Coded UITest.cs file and replace it with the renamed method name.

A: Yes, you use a runsettings file to specify the target device for test execution. For example:

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>

A: These are some of the key differences:

Feature

Windows Store apps

Windows Phone apps

Target for running tests

Local or remote computer. Remote computers can be specified when you use an automated test case to run tests. See Automate a test case in Microsoft Test Manager.

Emulator or device. See, Q: Can tests be executed on the emulator only, or can I also use a physical device? in this topic.

Execute from the command-line

Settings file not required to specify target.

Runsettings file required to specify target.

Specialized classes for Shell Controls

DirectUIControl

UITestControl

WebView control in a XAML app

Supported if you use Html* specialized classes to interact with HTML elements. See Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.

Not supported.

Execute automated tests from MTM

Supported.

Not supported.

Data-driven tests

See Data-driven tests for information about using external data-sources and using DataSource attribute on a test method.

Data is specified inline, using DataRow attribute on a test method. See Use Data-driven coded UI tests on Windows Phone apps in this topic.

For information about coded UI tests for Windows Store apps, see Testing Windows Store Apps with Coded UI Tests.

Microsoft Visual Studio Application Lifecycle Management blog: Using Coded UI to test XAML-based Windows Phone apps

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft