In the WebDriver Series, you can find invaluable tips and tricks about web automation. I will dedicate the next couple of articles on the automation of desktop apps. For the job we will leverage on a new piece of technology called WinAppDriver, developed and maintained by Microsoft. The framework utilizes the WebDriver Wire Protocol and uses the same client-server architecture as Selenium WebDriver. This will be the first article from the series helping you to get started.

Installing and Running Windows Application Driver

1. Download WinAppDriver 2. Install WinAppDriver

3. Enable Windows 10 Developer Mode

Open Windows 10 Settings. Then navigate to Update & Settings section. Click For Developers tab. Choose the Developer mode option.

4. Download Windows SDK 5. Install Windows SDK

6. Create a new test project in Visual Studio

7. Install the required NuGet packages

Since WinAppDriver is built on top of the Appium framework the most important NuGet package that you need to install is Appium.WebDriver. You can use some other unit test framework if you don't like NUnit. All other packages are installed together with Appium.

Using Appium

Windows Application Driver is integrated with Appium, meaning if you use Appium as part of the test runner then it will launch WinAppDriver.exe and proxy the requests for you.

Initialize Windows Application Driver

Before you can run your tests you need to start the WinAppDriver server. By default you can find it here: C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe

Once opened it starts listening on port 4723. In your tests, you connect to this URL.

This is how we create a new instance of the Windows Application Driver.

For the app option, you need to set the appId for the desktop application that you want to automate. For the deviceName option, you need always to set WindowsPC. In future you will be able to automate other MS devices such as XBox then you will set something else. The constructor of WindowsDriver accepts a Uri pointing the URL of the server that we already started.

While ago when we were working on the first version of the BELLATRIX test automation framework, I did this research while I was working on the desktop testing module of the solution.

Find Application AppId

It is a little bit tricky to find the appId of your applications.

In short, you can use the below PowerShell code to locate the appId of all installed apps.

Call listAumids(“allusers”) to find the appId of all installed apps for all users of the current machine.

Locating Elements

The latest Visual Studio version by default includes the Windows SDK with a great tool to inspect the application you are testing. This tool allows you to see every UI element/node that you can query using Windows Application Driver. This inspect.exe tool can be found under the Windows SDK folder which is typically C:\Program Files (x86)\Windows Kits\10\bin\

Once started, each time you point some element, you can find a detailed information which you can later use to locate it in WebDriver.

The locators supported by WinAppDriver are similar to the ones in Selenium WebDriver.

Client API Locator Strategy Matched Attribute in inspect.exe Example FindElementByAccessibilityId accessibility id AutomationId AppNameTitle FindElementByClassName class name ClassName TextBlock FindElementById id RuntimeId (decimal) 42.333896.3.1 FindElementByName name Name Calculator FindElementByTagName tag name LocalizedControlType (upper camel case) Text

Tests Examples C# Code

Bellow, you can find a simple NUnit test for testing the default Windows 10 Calculator. We add 5 to 7 and expect 12 to be the result.

We locate the buttons by name through the help of inspect.exe. We need to modify a little bit the result to simplify the code of the rest of the tests.

Below you can find a couple of more tests where we execute various calculator operations. In each test, we use different locator strategies so that you can see all of them in action. The last test shows how you can use data driven testing in desktop apps' automation.

In the above code we open and close the app for each test, which is a slow operation. You can always reuse the app by initializing the driver in class initialize method. Do not forget to dispose the app driver at the end of your tests. Also, always make sure to close the WinAppServer.exe after the tests' execution.