Wednesday, 20 September 2017

Automation Tests in UWP Part 1: Appium

What is Appium?


Appium is a great framework for testing UWP apps. It is explained in details here, so in this post I would like to show you how to get it running in a few simple steps.



First steps: a simple Calculator app UI test


The steps below explain how to setup Appium and run a very simple UI test on the Windows 10 Calculator app.

1. Install Microsoft WinAppDriver

2. In Visual Studio, create a new Unit Tests project:


3. Install the Appium.WebDriver nuget package in your project:



4. Create a base class for all unit tests. This will keep the Appium code in one place:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
using System;
 
namespace AppiumTestProject
{
    public class AppiumTestBase
    {
        private const string packageFamilyName = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App";
 
        protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
        protected WindowsDriver<windowselement> App;
        protected WindowsDriver<windowselement> DesktopSession;
 
        protected void LaunchApp()
        {
            var appCapabilities = new DesiredCapabilities();
            appCapabilities.SetCapability("app", packageFamilyName);
            App = new WindowsDriver<windowselement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
            Assert.IsNotNull(App, "Unable to activate the app");
 
            var desktopCapabilities = new DesiredCapabilities();
            desktopCapabilities.SetCapability("app", "Root");
            DesktopSession = new WindowsDriver<windowselement>(new Uri(WindowsApplicationDriverUrl), desktopCapabilities);
            Assert.IsNotNull(DesktopSession, "Unable to activate the desktop session");
        }
 
        protected void CloseApp()
        {
            App.Quit();
        }
    }
}
5. Add your first test method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using Microsoft.VisualStudio.TestTools.UnitTesting;
 
namespace AppiumTestProject
{
    [TestClass]
    public class UnitTest1 : AppiumTestBase
    {
        [TestMethod]
        public void TestMethod1()
        {
            LaunchApp();
            var fiveButton = App.FindElementByName("Five");
            fiveButton.Click();
        }
    }
}
6. Build the project and run the tests. Make sure the WinAppDriver is also running. You should see the Appium opening the Calculator app, taking control of your mouse pointer and clicking the 5 button in the app.



More advanced scenario


Let's create a new test. The method below starts the app, executes some key and click events in the app and takes a screenshot:
1
2
3
4
5
6
7
8
9
10
11
12
13
[TestMethod]
public void TestMethod2()
{
    LaunchApp();
 
    App.FindElementByAccessibilityId("CalculatorResults").SendKeys("10");
    App.FindElementByAccessibilityId("plusButton").Click();
    App.FindElementByName("Five").Click();
    App.FindElementByAccessibilityId("equalButton").Click();
 
    var screenshot = App.GetScreenshot();
    screenshot.SaveAsFile(@"C:\AutomatedScreenshots\screenshot1.png", ImageFormat.Png);
}
In your project use meaningful names for the class and unit tests. Leaving them as 'TestMethod1', 'TestMethod2' is a crime!



What is the package family name of my app?


In our test base class we use the Calculator package name: Microsoft.WindowsCalculator_8wekyb3d8bbwe!App. Obviously, you want to replace it with your app package name, so let me show you two ways of doing that:

Visual Studio

Build your UWP project in Visual Studio and open the vs.appxrecipe file in the text editor. You can find this file in the project output directory, for example bin\x64\Debug\AppX\vs.appxrecipe. Now search for RegisteredUserModeAppID, which is your Appium package name, for example myapp!vstest.executionengine.universal.App.

Get-AppxPackage

You can find the package names of all apps installed on your PC. Open the PowerShell in the Administrator mode and run this command:

    Get-AppxPackage | Select Name

Find the package you are interested in, note the name and run another command to see more details about it. The details for the Calculator app:

    Get-AppxPackage -Name Microsoft.WindowsCalculator

You can see the package in the details:

    PackageFamilyName : Microsoft.WindowsCalculator_8wekyb3d8bbwe

Before you use it in the your test, add the !App at the end of the package family name. I am not sure what is the rule here, so if you are unable to run the app due to invalid package name, Google is your friend :)

How to access the controls in the app


In the examples above, you could see I am using methods FindElementByName and FindElementByAccessibilityId to identify UI controls in the app. You can find the name, accessibility ID and many more details of any app running on your PC by using the Inspect tool, which is a part of the Windows 10 SDK. For example, open the Calculator app and check details of the 'Five' button:


Now you can try to use these details to interact with the 'Five'button in your code. All the methods in the example below return the same button:
1
2
3
4
5
6
7
8
9
[TestMethod]
public void TestMethod3()
{
    LaunchApp();
 
    App.FindElementByName("Five");
    App.FindElementByAccessibilityId("num5Button");
    App.FindElementsByClassName("Button")[25];
}
Hint: speed up the process of finding elements, just pause the execution of the test(just put a breakpoint anywhere in your test), open the Immediate Window (my favourite debug tool) and test your code there:



A few improvements

Why not launching and closing the app automatically before and after each tests?
1
2
3
4
5
6
7
8
9
10
11
[TestInitialize]
public void TestInitialize()
{
    LaunchApp();
}
 
[TestCleanup]
public void TestCleanup()
{
    CloseApp();
}



Monday, 21 August 2017

UWP explained

No, I'm not going to write a post explaining UWP :) Someone just did it very well: www.windowscentral.com/what-makes-uwp

Monday, 31 July 2017

Future Decoded

The registration is open for the free Microsoft event Future Decoded. I learnt a lot from one of the previous edition, it was very cool. Don't miss it and register today!


Monday, 5 June 2017

Xbox development Tips & Tricks

I use these tips all the time:

- Live Visual Tree also works on Xbox - so you can use such features as tracking focus element and changing XAML properties while debugging.

- Capture screenshots with Windows Device Portal

- Control your console remotely using Xbox Dev Mode Companion

- Use Fiddler to inspect and modify traffic between your app and the Internet

Also useful:

- Separate XAML for Xbox and other devices

Friday, 27 January 2017

Xbox tools

Microsoft tools


Microsoft provides a number of useful tools for developing UWP apps on Xbox One:


Device Portal for Xbox - the Xbox version of the feature-rich Windows Device Portal. You can use it to capture screenshots, connect Fiddler to your console, upload app packages, monitor performance and many more.

Xbox Device Portal - performance tab



Xbox Dev Mode Companion - a store app which allows you to remotely view the content and control your console using keyboard.
Xbox Dev Mode Companion - connecting to the console

Xbox Dev Mode Companion - keyboard mapping

Dev Home - an Xbox dev mode app available in the console dashboard. Provides tools for the apps management, network simulation, Visual Studio pairing and many more.

Dev Home


Other tools


Fiddler - a powerful tool for capturing network traffic. It runs on your PC and acts as a proxy between Xbox and the Internet.