This post is the continuation of my Save Time, Money, and Your Sanity: Coded UI Testing for Windows Phone series:

Part 1: Minimum Requirements and Setup
Part 2: Preparing the Coded UI Project
Part 3: Adding Assertions

Step 5 – Creating Test Methods

Last but not least, you will need to create Coded UI Test methods, which essentially ties together everything you have done up to this point. At a bare minimum, your tests will go through the process of launching the application, optionally performing gesture actions (for example: Tap, Double Tap, Swipe, Pinch, etc.), optionally setting test values, and then executing Assert methods to verify the UI state.

Annotating the Test method

Coded UI Test methods must be annotated with the TestMethod attribute. When creating a new Coded UI Test Project, the template generates a default Test method with this attribute in place:

[TestMethod]
public void CodedUITestMethod()
{
}

Any additional test methods you create should include the TestMethod attribute.

Launching the app

The first step in your Test method will be to launch the application. For Windows Store apps and Windows Phone 8.1 apps, you must call the XamlWindow.Launch method, passing in the application’s automation ID.

To obtain the automation Id for your application, you will need to launch the Coded UI Test Builder. Right-click on a Test method within your CodedUITest1 class, and select Generate Code for Coded UI Test > Use Coded UI Test Builder from the context menu.

Within the Emulator, return to the Start screen, navigate to the Installed Apps list, and locate your application. Hover your mouse pointer over your application’s name in the list, then select Ctrl + Shift + I to select the item. The Add Assertions dialog will appear, and will include your application’s list item control in the list. In the right pane, click on the AutomationId property, and select the Copy Value to Clipboard button to copy this value.

clip_image002

Close the Coded UI Test Builder, return to the CodedTestUI1 class, and paste the AutomationId string as the parameter value within the XamlWindow.Launch method.

Using Gestures to perform an action on a control

The Gesture object contains methods to perform actions within the application’s UI which will mimic user actions such as Tap, Double Tap, Flick, Swipe, and Pinch, to name a few. To conduct an action against a control in the UI, you simply execute the desired action method on the Gesture object, passing in the control which will be the target for that action. For example:

Gesture.Tap(this.UIMap.UIMyWishListWindow.UIItemList.UIItemDress);

Notice that the control which will receive the Tap gesture is being referenced through the UIMap. The CodedUITest1 class includes a public instance of the UIMap by default. You can use the UIMap instance to drill down the control hierarchy to reference a specific control. The example above depicts a list item being selected with a Tap gesture.

Executing the Assert method

Finally, you can execute any of the Assert methods you generated. Similar to referencing controls under test, you can execute your Assert methods through the UIMap instance: this.UIMap.AssertItemSelected();

Coded UI Tests In Action

With our Coded UI Tests in place and wired up properly, we can now run the tests using the Test Explorer within Visual Studio. If your Test Explorer window isn’t open, simply select Test > Windows > Test Explorer from the Visual Studio menu.

clip_image004

Click Run All, to execute all of the available Coded UI Tests we created. You will then see your app automatically launch within the Emulator, and each test will perform the specified actions and assertions.

Once the tests have completed, an indicator beside each test will appear to denote if the test passed or failed. Tests will also be grouped according to Passed, Failed, or Tests not Run. Clicking on a test in the Test Explorer will display details about the execution of the test in the bottom panel.

Side Note: Coded UI Tests for Windows Phone do not support HTML Trace Logs. So there will not be an option to view the Output file for your tests, even if you have configured your Coded UI Test project to generate this output. HTML Trace Logs are available for Coded UI Test projects for Windows Store apps.

Wrapping It Up

Designing and developing mobile apps is only part of the process in the development lifecycle. The key goal for mobile app developers is to get their apps to market in a reasonable amount of time. Testing is a critical part, which often gets overlooked when developers lack time, money, and resources to fully test their apps. Microsoft has addressed this issue for developers by enabling the creation of Coded UI tests for Windows Phone 8.1 apps. With Unit Test projects already available, this addition of Coded UI Tests allows you to easily implement end-to-end automated testing, so that you can focus more on your app’s design & development, and less on the tedious and repetitive testing process.