Testing Windows Phone Apps with Coded UI Tests
Use coded UI tests to test your Windows Phone apps.
-
Create a new project for a blank Windows Phone app using either Visual C# or Visual Basic template.
-
In Solution Explorer, open MainPage.xaml. From the Toolbox, drag a button control and a textbox control to the design surface.
-
In the Properties window, name the button control.
-
Name the textbox control.
-
On designer surface, double-click the button control and add the following code:
-
Press F5 to run your Windows Phone app in the emulator and verify that it’s working.
-
Exit the emulator.
-
Add a new coded UI test project to the solution with the Windows Phone app.
-
Choose to edit the UI map using the cross-hair tool.
-
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.
-
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.
-
To add the textbox control to the UI control map, repeat the previous step.
-
Generate code to create code for changes to the UI control map.
-
Use the cross-hair tool to select the textbox control, and then select the Text property.
-
Add an assertion. It will be used in the test to verify that the value is correct.
-
Add and generate code for the assert method.
-
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
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
Controls
-
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:
-
Launch the Windows Phone app using the automation ID property you copied to the clipboard previously:
-
Add a gesture to tap the button control:
-
Verify that the call to the assert method that was automatically generated comes after launching the app and tap gesture on the button:
After the code is added, the CodedUITestMethod1 test method should appear as follows:
-
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: Make sure that you have installed Visual Studio 2013 Update 2 or later.
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 |
||
|
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