Because the Coded UI framework expires after Visual Studio version 2019 (Deprecated Coded UI), Microsoft recommends Appium with WinAppDriver for testing Windows applications (Desktop and UWP). You can use Appium (with WinAppDriver) or WinAppDriver directly to run the tests (WinAppDriver with or without Appium).
WinAppDriver directly
Here is a short description to work with the WinAppDriver directly:
download and install WinAppDriver:
WinAppDriver Release
enable Developer Mode in Windows settings
start the WinAppDriver:
C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe
create a new Visual Studio 2019 Unit Test Project (.NET Framework)
add the NuGet-Package: Appium.WebDriver Microsoft.WinAppDriver.Appium.WebDriver (comment from Microsoft: it is recommenced to use the WinAppDriver NuGet package to take full advantage of the advanced input with the Actions API.)
add a new class DesktopSession:
public class DesktopSession
{
protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
private const string NotepadAppId = @"C:\Windows\System32\notepad.exe";
protected static WindowsDriver<WindowsElement> session;
protected static WindowsElement editBox;
public static void Setup(TestContext context)
{
// Launch a new instance of Notepad application
if (session == null)
{
// Create a new session to launch Notepad application
var appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("app", NotepadAppId);
appCapabilities.SetCapability("platformName", "Windows");
appCapabilities.SetCapability("deviceName ", "WindowsPC");
session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
Assert.IsNotNull(session);
Assert.IsNotNull(session.SessionId);
// Set implicit timeout to 1.5 seconds to make element search to retry every 500 ms for at most three times
session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1.5);
// Keep track of the edit box to be used throughout the session
editBox = session.FindElementByClassName("Edit");
Assert.IsNotNull(editBox);
}
}
public static void TearDown()
{
// Close the application and delete the session
if (session != null)
{
session.Close();
try
{
// Dismiss Save dialog if it is blocking the exit
session.FindElementByName("Nicht speichern").Click();
}
catch { }
session.Quit();
session = null;
}
}
[TestInitialize]
public void TestInitialize()
{
// Select all text and delete to clear the edit box
editBox.SendKeys(Keys.Control + "a" + Keys.Control);
editBox.SendKeys(Keys.Delete);
Assert.AreEqual(string.Empty, editBox.Text);
}
}
- Change the code from the UnitTest1 class
[TestClass]
public class UnitTest1 : DesktopSession
{
[TestMethod]
public void EditorEnterText()
{
Thread.Sleep(TimeSpan.FromSeconds(2));
editBox.SendKeys("abcdeABCDE 12345");
Assert.AreEqual(@"abcdeABCDE 12345", editBox.Text);
}
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
Setup(context);
}
[ClassCleanup]
public static void ClassCleanup()
{
TearDown();
}
}
(the sample code is mainly copied from WinAppDriver
.NotepadTest).
Appium with WinAppDriver
If you want to run your tests using Appium, then you must have installed the correct version of the WinAppDriver on your machine. The installer of Appium should also install the WinAppDriver with the correct version on your machine (please install Appium for all users).
In my case, unfortunately, this did not work. So I take a look in the file:
C:\Program Files\Appium\resources\app\node_modules\appium\node_modules\appium-windows-driver\lib\installer.js
Here you will find the correct version and the download path:
const WAD_VER = "1.1";
const WAD_DL = `https://github.com/Microsoft/WinAppDriver/releases/download/v${WAD_VER}/WindowsApplicationDriver.msi`;
If you have installed the correct WinAppDriver you can start Appium.
Important: you have to change the ApplicationDriverUrl
protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723/wd/hub";
Tools:
WindowsAppDriver and UI REcorder releases
or
Donwload the WinAppDriver repository and build the WinAppDriverUIRecorder.sln in the subdirectory tools\UIRecorder
Introducing WinAppDriver UI Recorder
- inspect.exe: Windows SDK is required (look in C:\Program Files (x86)\Windows Kits\10\bin)
Other links:
WinAppDriver FAQ
Appium