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 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(); } } } |
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(); } } } |
More advanced scenario
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); } |
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]; } |
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(); } |