WinAppDriver and Desktop UI Test Automation
Published Jan 23 2020 02:35 AM 118K Views
Iron Contributor

Author : Edwin Hernandez Maynez

 

This article is a follow up on a previous post titled "UI Automation - Page Object Model and other Design Patterns" and is part of a series of posts related to UI Test automation, if you are interested in UI automation tools in general, we definitely recommend checking out our earlier post:  "What are the best UI Test Automation Tools?" which was published on this same blog not so long ago. You may want to have a look at those two articles since it provides a high level view of what tools are available in the market for Functional UI Test automation, either for Web, Mobile or Desktop and then a deeper analysis of code design approaches.

 

In this article I will describe broadly the technical highlights of how Desktop UI automation is done and I will describe the main features of a relatively new test framework that is becoming a must-use for UI automation for Windows-based applications.

 

About CodedUI

CodedUI is a Microsoft technology created to provide support of Desktop and Web UI automation, this technology is now deprecated and Visual Studio 2019 is the last version of VS that will include it. Microsoft has announced that it recommends the following technologies as replacements:

 

From <https://docs.microsoft.com/en-us/visualstudio/test/use-ui-automation-to-test-your-code?view=vs-2019

 

Given that announcement, if you have an interest or a need to be able to provide Desktop UI automation support for one of your projects, please read forward as I list the main features of WinAppDriver.

 

Windows Application Driver

WinAppDriver is a test framework developed by Microsoft as an open source project, it's an implementation of Appium which is primarily a Mobile App framework, itself based on Selenium. Therefore WinAppDriver is a Selenium-like automation framework. This project is combining the best of two worlds, on one hand it encapsulates most of the technology of the now deprecated CodedUI and it fuses it with the flexibility, easiness of use and adoption of Selenium.

 

Just like Selenium, WinAppDriver is a set of libraries that can be integrated into any Test Runner that supports Appium. E.g. WinAppDriver scripts can be developed and executed with MSTest from Visual Studio.

 

Requirements

  • WinAppDriver only runs on Windows 10
  • You will need a test runner, such as MSTest with Visual Studio, but any other Appium Test Runner would do

Where can you get it:

 

Desktop UI Automation

Unlike Web UI automation, when working with Desktop applications, there is more variance on the technologies that could have been used to develop the application you are testing. This has an impact in the toolset's ability to identify and perform actions on a given UI element:

  • UWP – Universal Windows Platform, also known as Universal Apps or Modern Apps, It's Microsoft’s latest desktop application technology. It's XAML based. Only runs on Windows 10 machines
  • WPF - also XAML based, much more mature, runs on any Windows version and has been around since 2006.
  • WinForms - one of the older technologies, now found mostly on legacy applications.
  • MFC/Classic Windows -  MFC is a UI library normally paired with Win32 applications. This option is normally chosen when more efficiency is needed with low-level C++ handling or when supporting non-Microsoft platforms.

From <https://docs.microsoft.com/en-us/windows/apps/desktop/choose-your-platform>

 

CodedUI is an umbrella framework for Web and Desktop UI technologies:

Untitled picture_jan.png

 
 

 

UI Automation (UIA) and Microsoft Active Accessibility (MSAA) are two lower-level accessibility technologies used to provide access to UI elements. MSAA is now a legacy library while UIA is newer and more capable. UIA is an accessibility framework not a test framework and it is not meant to be used as such.

 

Both of these technologies are now under the hood of WinAppDriver, which is why WinAppDriver fully supports the desktop technologies listed above (UWP,WPF,Winforms & MFC).

 

How to get started

I will provide a quick start guide and simple scripts on Visual Studio 2019:

  • First follow the instructions above to download and install WinAppDriver, then
  • Create a Unit Test Project in Visual Studio

Untitled picture1_Jan.png

 
  • Go to Manage Nuget Packages, then Browse for Appium.WebDriver.
  • Install it, it will automatically add a few more packages such as Selenium.WebDriver
  • If it doesn't pull them automatically, you may have to search for them and install them yourself.
  • It should look like this on the end:
    • Packages.png

       

  • Now that you have that setup: the driver configuration differs depending on which kind of application you are testing. You can find below the setup for a non-UWP (classic app) and a UWP app. These two examples have the minimal code needed for a WinAppDriver test:

 

 

 

using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
 
namespace Blog
{
	[TestClass]
	public class UITests
	{
		
		[TestMethod]
		public void SysInfoTest()
		{
			Process.Start(@"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe");
 
			WindowsDriver<WindowsElement> SysInfoApp;
			DesiredCapabilities appCapabilities = new DesiredCapabilities();
			appCapabilities.SetCapability("app", @"C:\Windows\System32\msinfo32.exe");            
			SysInfoApp = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appCapabilities);
 
			SysInfoApp.FindElementByName("Help").Click();
			SysInfoApp.FindElementByName("About System Info...").Click();
		}
  
		[TestMethod]
		public void CalculatorTest()
		{
			Process.Start(@"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe");
 
			WindowsDriver<WindowsElement> Calculator;
			DesiredCapabilities appCapabilities = new DesiredCapabilities();
			appCapabilities.SetCapability("app", "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");
			Calculator = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appCapabilities);
 
			Calculator.FindElementByAccessibilityId("num5Button").Click();
			Calculator.FindElementByAccessibilityId("plusButton").Click();
			Calculator.FindElementByAccessibilityId("num3Button").Click();
			Calculator.FindElementByAccessibilityId("equalButton").Click();     
		}
	}
}

 

 

 

 

 

  • Similarly to Selenium, in WinAppDriver, elements are found by providing identifiable properties such as Class, Id, Name, AccessibilityID, Xpath, etc. You can spy these properties using a few other tools such as:
    • WinAppDriver Recorder, compact app provided by the same team, gives you an Xpath to the element you need.
    • Inspect.exe, powerful tool that comes with the Microsoft SDK, it also provides pattern recognition for UIA accessibility. Use the properties listed for a given element to find it with WinAppDriver.
    • Spy++. An object identification tool that comes with Visual Studio.
  • That should be it, now you have a fully-working WinAppDriver test framework. It may sound easy to get started but the challenge on UI automation is not on the initial setup but on the maintainability, good coding practices and the feasibility of the framework to identify all the UI elements that you will need. Here are some pointers:

 

Tips and Tricks

  • Depending on the Desktop development technology that was used to create the app you are testing, and depending on whenever or not the developers chose to use the out-of-the box UI elements or chose to customize them, finding elements can be a piece of cake or a real pain.
  • Even if the developers used the standard UI elements, if they are not including an UIAutomation property or if they are leaving properties like name/text blank or the same for different elements…then identifying elements can be challenging.
  • My recommendation here is to use xpath to its full extent: look into functions such as contains(), last(), first(), sibling(), etc. … find how to go up or down levels on the xpath tree to navigate to the element you need out of another one. You can also use WinAppDriver findElements method to pull all matching elements into a Collection and just iterate thru it until you find the one you need.
  • You may find that WinAppDriver just as CodedUI, may not be able to inspect or drill down into certain elements, especially when these elements are customized or on certain corner cases of the older technologies (Winforms or MFC). In this case you may need to dive into using UIA patterns to find and perform actions into these elements.
  • Maintainability and fragility of UI scripts to changes really becomes an issue as your project grows. Look into Design Patterns such as Page-Object-Model (I just wrote an article about this, you can find a link to it at the top of this page)
31 Comments
Copper Contributor

Hello This is a Awesome Article

Brass Contributor

@Jivant1620: Glad you liked it, thanks for the feedback!

Copper Contributor

Thanks for the nice article. What is currently bothering me is the complete lack of activity in the github repo. There's also an issue on this topic, but no feedback from Microsoft. Do you have any corresponding information you can share?

Brass Contributor

Hi @bmueller, thanks for your comment. I reached out to Hassan and he told me that the WinAppDriver team is ongoing a transition. He will update the other thread with more information in the coming weeks.

 

All I can say is that WinAppDriver is a key part of the toolset that my team (Microsoft Test Team) uses on our Consulting and Premier engagements and we will keep advocating for it internally.

Copper Contributor

Why does this tool asked for the Developer Mode? 

Brass Contributor

@tdurovastorz :

I don't know the exact reason why Developer Mode is a requirement, I suspect is has something to do with been able to debug and drive UI actions into a packaged windows 10 app. There is an enhancement proposed to make away with this requirement, in case you want to add your comments there: https://github.com/microsoft/WinAppDriver/issues/975

 

Copper Contributor

@Edwin Hernandez

 

Thanks for relaying this to Hassan.

 

It is frustrating, however, months later after being promised more information, a roadmap, etc., is it as dead as before.

 

Do you have any timescales of when any activity is likely to happen, if at all still?  There is a growing unquietness on the issues for the project that there is no support for what is essentially a black box product that no longer seems to have official support anymore.  If it were open sourced, the community could be involved in resolving more of these issues.

 

Appreciate the current circumstances may have changed priorities, but after previous communications and the fact the only supported testing tool for this area now supported has received no support for over half a year, it would be good to have something more tangible now than just distant promises.

Copper Contributor

Desired Capabilities is depreciated so use the below code

 

[TestClass]
public class UnitTest1
{
[TestMethod]
public void CalculatorTest()
{
Process.Start(@"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe");

WindowsDriver<WindowsElement> Calculator;

AppiumOptions appiumOptions = new AppiumOptions();
appiumOptions.AddAdditionalCapability("deviceName", "WindowsPC");
appiumOptions.AddAdditionalCapability("app", "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");
Uri url = new Uri("http://127.0.0.1:4723");

Calculator = new WindowsDriver<WindowsElement>(url, appiumOptions);

Calculator.FindElementByAccessibilityId("num5Button").Click();
Calculator.FindElementByAccessibilityId("plusButton").Click();
Calculator.FindElementByAccessibilityId("num3Button").Click();
Calculator.FindElementByAccessibilityId("equalButton").Click();

}
}

 

Note: Make sure that you change Windows 10 to Developer Mode else you ll get an error like this.

Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:4723

Copper Contributor

If we have to change it to Developer Mode in Windows 10 then how can we run the automation tests in Azure Devops?

Copper Contributor

@Edwin Hernandez 

@Microsoft_Testing_Team 

 

Any updates on Winappdriver’s development? I advocated its use for our automation but am wary of my decision due to the lack of development or roadmap. 

Copper Contributor

@Edwin Hernandez Good article but the NuGate packges to be installed are missing or at least I cannot see any listed under the bullet point:

  • Install the following package:
Copper Contributor

Sorry for my typo. NuGate should be NuGet

Brass Contributor

Thanks @SZ-278 for pointing it out, I fixed the image and it should now show you which NuGet packages should to install.

Brass Contributor

Thank you for your comment @BrandonWolfe,

In regards to WinAppDriver, I understand the concern, there was a pause in development that lasted a couple of years and had many people that rely on this product worried. As far as I know this was due an internal re-organization of the WinAppDriver project team. However they recently completed finding a new internal owner of this project.

 

Please check this entry. They have a new PM and she has stated the new direction and approach. Sounds like the project is alive and well, although they haven’t resumed active development yet, they are working on an execution plan.

 

From my team’s perspective, Microsoft Test Team (we do a lot of quality and performance consulting), this tool remains key for us and we will continue to use it and advocate for it internally, as our preferred tool for UI-test automation of desktop apps.

Copper Contributor

@Edwin Hernandez  i would like to know whether WinAppDriver is supported on Windows Server 2019 OS?

Copper Contributor

@Edwin Hernandez and others - We have been using appium for our windows app testing for quite some, the current solution is kind of manual.. Here is is how it works:

  1. Appium and desktop app is installed in windows machine
  2. Login to windows VM
  3. Run appium tests (JUnit tests - we use java)

We are planning to deploy this is cloud, but we have hit a roadblock right now.. we can successfully run the tests only if we remotely connect to windows machine.. if we just launch the tests without RDP we get the below exception:

org.openqa.selenium.SessionNotCreatedException: Unable to create a new remote session. 
Please check the server log for more details. Original error:
An unknown server-side error occurred while processing the command.

Is it possible to run appium tests without manual RDP? Really appreciate your help. Thanks

Copper Contributor

@RamakrishnaGutha: That's a very good question, and I'd be interested in a complete solution, too. The best we could come up with is establishing a remote desktop connection and then disconnecting via tscon (see, for example, here). At least this avoids the problem of "blocking" the UI of the machine keeping the remote desktop connection alive. Does anyone have a better solution?

Copper Contributor

Thanks , this article was just awesome!

Copper Contributor

@Microsoft_Testing_Team and @Edwin Hernandez good summary, but WinApp driver is essentially dead with no updates in like two years, so it's strange that it is still the recommended tool for testing.  See: https://github.com/microsoft/WinAppDriver/issues/1550#issuecomment-948341316

 

A possibility would be to extend Playwright, an excellent, and very lively app to support Windows automation. See: https://github.com/microsoft/playwright/issues/10291

Copper Contributor

Hello, 

 

I am getting following exception. please help me out with it.

 

exception.png

Copper Contributor

Hi, Thank you to share this helpful content on the WinAppDrvier & Desktop UI testing automation. It's very helpful for beginners to advance. Our Sr. Test engineer has written a detailed blog on Introduction to WinAppDriver, Kindly click on the following link to look at our article & if you see any corrections required so feel free to drop your thought

 

WinAppDriver - Free Test Automation Tool

 

Thank you

Copper Contributor

@Microsoft_Testing_Team and @Edwin Hernandez any further planned updates or is WinAppDriver dead?

Copper Contributor

Great share, thanks for this helpful content on the WinAppDrvier & Desktop UI testing automation. Our experts have also written a few contents, do check them out on the link given below. If you see any corrections required so feel free to drop your thought.

 

https://www.fleekitsolutions.com/software-testing-latest-trends/

Copper Contributor

I'm moving from CodedUI in VS17 to WinAppDriver in VS22. Which is the right project type to create for a windows GUI application..

a MSTest or Nunit Test Project?

Copper Contributor

This was very interesting, although there is still great concern that Microsoft does not seem to want to support WinAppDriver any more?

 

@Microsoft_Testing_Team and @Edwin Hernandez do you have any news for us?

Copper Contributor

facing problems to run the test on  windows calculator.  

installed libraries from Visual studio 2022 using nuget package manager  Selenium, appium, WinappDriver  

 

code is 

using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
using System;
using System.Security.Policy;
using OpenQA.Selenium;

namespace Blog
{
[TestClass]
public class UnitTest1
{

[TestMethod]
public void CalculatorTest()
{
Process.Start(@"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe");

WindowsDriver<WindowsElement> Calculator;

AppiumOptions appiumOptions = new AppiumOptions();
appiumOptions.AddAdditionalOption("deviceName", "WindowsPC");
appiumOptions.AddAdditionalOption("app", "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");

Uri url = new Uri("http://127.0.0.1:4723");

Calculator = new WindowsDriver<WindowsElement>(url, appiumOptions);

Calculator.FindElementByAccessibilityId("plusButton").Click();
Calculator.FindElementByAccessibilityId("num3Button").Click();
Calculator.FindElementByAccessibilityId("equalButton").Click();
}
}
}

 

two errors :  1.Severity Code Description Project File Line Suppression State
Error CS1503 Argument 1: cannot convert from 'System.Uri' to 'OpenQA.Selenium.Remote.DesiredCapabilities' Blog

 

2. Severity Code Description Project File Line Suppression State
Error CS1503 Argument 2: cannot convert from 'OpenQA.Selenium.Appium.AppiumOptions' to 'System.TimeSpan' Blog 

 

 

 

Copper Contributor

issue is with is code 

Uri url = new Uri("http://127.0.0.1:4723");

Calculator = new WindowsDriver<WindowsElement>(url, appiumOptions);

two errors :  1.Severity Code Description Project File Line Suppression State
Error CS1503 Argument 1: cannot convert from 'System.Uri' to 'OpenQA.Selenium.Remote.DesiredCapabilities' Blog

 

2. Severity Code Description Project File Line Suppression State
Error CS1503 Argument 2: cannot convert from 'OpenQA.Selenium.Appium.AppiumOptions' to 'System.TimeSpan' Blog 

Copper Contributor

How to print the Winapp driver console log to a text file .. in case element not found it just says not found without proper info of which element not found . The information is visible in Winappdriver exe console but unable to print it in test logs . Any thoughts ??

Copper Contributor

Bad capabilities. Specify either app or appTopLevelWindow to create a session" while creating a session. Any solution for this issue?

 

Request DefaultHttpRequest(decodeResult: success, version: HTTP/1.1)
POST /wd/hub/session HTTP/1.1
X-Idempotency-Key: 35ebbfc4-1676-439d-9f22-9f32213a419f
User-Agent: selenium/4.2.0 (java windows)
Content-Length: 384
Content-Type: application/json; charset=utf-8
host: 127.0.0.1:4723
accept: */*

Response DefaultHttpResponse(decodeResult: success, version: HTTP/1.1)
HTTP/1.1 404 Not Found
Server: Microsoft-HTTPAPI/2.0
Date: Fri, 22 Sep 2023 14:27:56 GMT
content-length: 0

Copper Contributor

@scribble8 , winapdriver doesn't work with selenium 4, please downgrade your selenium version to v 3.141.59

Copper Contributor

Hello,

Is it supported on windows 11?
Also, is there Microsoft support for this product ?

Thank you

Version history
Last update:
‎Mar 23 2021 03:40 PM