Windows Application Driver (WinAppDriver) With C#
Tags: winappdriver, windowsapplicationdriver, appdrive
Published: 2022-12-14 08:33:32

# What Is Windows Application Driver (WinAppDriver)?

WinAppDriver is a service to support Selenium-like UI Test Automation on Windows Applications. This service supports testing Universal Windows Platform (UWP), Windows Forms (WinForms), Windows Presentation Foundation (WPF), and Classic Windows apps on Windows 10 PC.

## How to Install & Run WinAppDriver?

- 
First Download the WinAppDriver installer from https://github.com/Microsoft/WinAppDriver/releases

- 
Run the installer on a Windows 10 machine where your windows application is to be tested.

- 
Enable Developer Mode in Windows settings


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qgexdbpl363adhrh5qwn.jpg)

Open WinAppDriver.exe from the installation directory (C:\Program Files (x86)\Windows Application Driver)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdmp7ybq29e0taj9mukj.jpg)

After That, WinAppDriver will be running on the test machine listening to requests on the default IP address and port (127.0.0.1:4723).


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06l6sh07tv3xb7wlk22u.jpg)

Now you are ready to run any of your Tests Scripts.

### Required Packages in Project’s Solutions
We need to add packages ‘Microsoft.WinAppDriver.Appium.WebDriver’ in the current solution from the Nugget as below I have attached the screenshot of the packages.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/negg1n2k2eic7i51e0dy.png)

#### How to Inspect Elements on Windows Application?
First, we need to Download the Win SDK file from https://developer.microsoft.com/en-us/windows/downloads/windows-sdk/

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3gcryhfmwszgjj9wdh37.png)

By using ‘inspect.exe’ we can inspect the element present in the windows application


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pb8qf6pub08xbpjb0mul.png)

FOR EXAMPLE, I HAVE WRITTEN A DEMO SCRIPT TO PERFORM ACTION ON THE OUTLOOK APP:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oz2u5dhzdfi16t8sgung.png)

<?xml version="1.0" encoding="utf-8"?>

<RunSettings>

      <TestRunParameters>

            <Parameter name="AppId" value="C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE"/>

            <Parameter name="sessionSingletonUrl" value="http://127.0.0.1:4723"/> //When open WinAppDriver You will get this value.

      </TestRunParameters>

</RunSettings>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h72347pphbqwtorgodn6.png)

using NUnit.Framework;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
using System;
using System.Threading;

namespace WinAppDriver
{
    internal class OutlookDemo
    {
        private static WindowsDriver<WindowsElement> session;

        [Test]
        public void OpenOutlook() {

            DesiredCapabilities appCapabilities = new DesiredCapabilities();
            appCapabilities.SetCapability("app", TestContext.Parameters["AppId"]);    //Getting AppId from test.runsettings. 
            session = new WindowsDriver<WindowsElement>(new Uri(TestContext.Parameters["sessionSingletonUrl"]), appCapabilities);// Getting sessionSingletonUrl  from test.runsettings
            session.SwitchTo().Window(session.WindowHandles[0]);
            session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
            session.Manage().Window.Maximize();
            session.FindElementByName("Home").Click();
            session.FindElementByName("Send / Receive").Click();
            session.FindElementByName("View").Click();
            session.FindElementByName("Help").Click();
            session.Close();
        }
    } 
}

NOTE: Before executing this script you need to select test.run settings to your current project and WinAppDriver c# should be open.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e7hlm2pr2kl01qz84yw7.png)

#Windows Application Driver (WinAppDriver) Pros
This is an open-source tool. It uses the web driver protocol. It’s free and developed by Microsoft. WinAppDriver can integrate with selenium and Appium projects in the C# language.

## Conclusion
WinAppDriver is a very easy-to-use free tool and it is the best option for Automation on Windows applications rather than a paid one, which is easy to integrate and implement with existing automation projects.

**Original Source**- [Winappdriver tutorial c#](https://www.devstringx.com/windows-application-driver-winappdriver)