Home .NET Testing UWP applications with Appium

Testing UWP applications with Appium

by admin

Testing UWP applications with Appium

At the moment UWP applications are used as a Coded UI testing solution. But if you have an application for multiple platforms, then you have to write tests for each platform separately.
In 2016, the ability to test UWP applications was announced with Appium A driver called the Windows Application Driver (WinAppDriver) was created for this purpose.
Selenium using WebDriver API , was created to automatically test web applications. Appium is actually Selenium for Apps.
With Appium and WinAppDriver, you can test not only UWP but also Win32 applications (they don’t mention .NET for some reason, but you can test them too). As usual, I am most interested in UWP applications.

Installing WinAppDriver

Download the latest release From the releases page on GitHub
After installation in the directory C:Program Files (x86)Windows Application Driver will contain the executable file WinAppDriver.exe, which can be used to run the service
You can also install Appium, which will interact with the WinAppDriver, but the WinAppDriver should be installed anyway. The WinAppDriver should be installed together with Appium, but as of this writing there are still some issues, so you have to install it separately. Just in case, let me leave under the spoiler how to install Appium on a Windows machine.
How to install Appium on Windows 10 using the npm package manager Appium requires Node.js version 6.0 or higher and npm version 3.5 or higher
You can check the version of Node.js with the console command
node -v
The npm version
npm -v
Download the Node.js installer from official site
The installation installs and npm
If the installed versions are higher than the minimum required, you can run the installation with the following command (the command line must be run as administrator):
npm install -g appium
Now you can run appium on the command line and get the following message :

Creating a draft application for testing

Let’s create a simple UWP application which we will test right away. Let the application perform some simple operation, such as squaring a number. The source code of the demo application is really easy.
In MainPage.xaml we add :

<StackPanel Orientation="Vertical" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"><TextBlock Margin="0, 25, 0, 0" TextAlignment="Center"> Enter a number :</TextBlock><TextBox AutomationProperties.Name="txtNumber" x:Name="txtNumber"Margin="0, 5, 0, 0" FontSize="18" Width="150"> </TextBox><TextBlock Margin="0, 25, 0, 0" TextAlignment="Center"> Result :</TextBlock><TextBox AutomationProperties.Name="txtResult" x:Name="txtResult"TextAlignment="Center" FontSize="18" Width="150"> </TextBox><Button AutomationProperties.Name="btnGetSquare" x:Name="btnGetSquare"Margin="0, 20, 0, 0" HorizontalAlignment="Center" Click="btnGetSquare_Click">Square it</Button></StackPanel>

And MainPage.xaml.cs is only one event :

private void btnGetSquare_Click(object sender, RoutedEventArgs e){double n = Convert.ToDouble(txtNumber.Text);txtResult.Text = (n * n).ToString();}

From the manifest we need to save the Package family name. It is needed to write the test.
The same value can also be taken from the vs.appxrecipe file, which is located in the Debug/AppX folder. The value is contained within RegisteredUserModeAppID.
Before testing the application, you need to deploy it. This is how the application turned out :
Using the utility inspect.exe which is located in the directory C:Program Files (x86)Windows Kits10binx86 you can define the names of the items to be used in the test.
Starting the application you want to test, you can either select any control you are interested in in the tree or even just click on the control in the window of the application itself. You will get approximately the following information :
Testing UWP applications with Appium
To search for an element the following values are usually used: ClassName and Name. With the IOs driver, you can search for an element by its AutomationId.

Creating an automated testing project

In Visual Studio, create a project of the Unit Test type
In the NuGet package manager we find and install Appium.WebDriver together with the necessary dependent packages for its operation.
The RemoteWebDriver can be used to work with the UWP application, or the IOSDriver can be used. Most likely some third driver for the Windows platform will appear after the final release. Windows Namespace should be included in the NuGet package Appium .NET Driver.
Let’s write the code of the test. In this case it can be this :

[TestClass]public class UnitTest1{protected const string AppDriverUrl = "http://127.0.0.1:4723";protected static RemoteWebDriver AppSession;[ClassInitialize]public static void Setup(TestContext context){DesiredCapabilities cap = new DesiredCapabilities();cap.SetCapability("app", "6b86c2c7-c428-4039-9281-8da10ee45769_dyre41xy79knw!App");AppSession = new RemoteWebDriver(new Uri(AppDriverUrl), cap);Assert.IsNotNull(AppSession);}[ClassCleanup]public static void TestsCleanup(){AppSession.Dispose();AppSession = null;}[TestMethod]public void MakeOperation(){AppSession.FindElementByName("txtNumber").Clear();AppSession.FindElementByName("txtNumber").SendKeys("5");AppSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1));AppSession.FindElementByName("btnGetSquare").Click();AppSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1));RemoteWebElement txtResultTextElement;txtResultTextElement = AppSession.FindElementByName("txtResult") as RemoteWebElement;Assert.IsNotNull(txtResultTextElement);Assert.AreEqual("25", txtResultTextElement.Text);AppSession.Quit();}}

If IOSDriver is used, the initialization code changes to the following :

protected const string AppDriverUrl = "http://127.0.0.1:4723";protected static IOSDriver<IOSElement> AppSession;[ClassInitialize]public static void Setup(TestContext context){DesiredCapabilities cap = new DesiredCapabilities();cap.SetCapability("app", "6b86c2c7-c428-4039-9281-8da10ee45769_dyre41xy79knw!App");AppSession = new IOSDriver<IOSElement> (new Uri(AppDriverUrl), cap);Assert.IsNotNull(AppSession);}

As you can see the changes are minimal. The MakeOperation test code remains the same.
Additionally, you can (and if you run Appium, should) specify the platform and device :

cap.SetCapability("platformName", "Windows");cap.SetCapability("deviceName", "WindowsPC");

If you run WinAppDriver, the URL string:

protected const string AppDriverUrl = "http://127.0.0.1:4723";

Well, if you launched Appium, then :

protected const string AppDriverUrl = "http://127.0.0.1:4723/wd/hub";

Let’s execute the test. You can do it this way :
And in case everything is correct, we get :
A small animation of the automated process :
Testing UWP applications with Appium
Finally, here is a standard example of working with a Win32 application :

DesiredCapabilities cap = new DesiredCapabilities();cap.SetCapability("app", @"C:WindowsSystem32notepad.exe");AppSession = new RemoteWebDriver(new Uri("http://127.0.0.1:4723"), cap);AppSession.FindElementByClassName("Edit").SendKeys("Hello Appium!");

This example opens notepad and enters text. To test a .NET application, just write the path to the executable file in the same way.
A couple of links :
GitHub page
Official documentation page : Windows Application UI Testing

You may also like