Windows Application Automation Testing Using Winapp Driver And C# Beginner

Introduction

This blog is for beginners. It's mainly to teach you how to automate a Windows Application using C#, Winapp driver (Appium), & Selenium. In this blog, we are going to automate the Calculator app. 
 
Winapp driver (Appium) 

Winapp is a Product of Microsoft, using this driver we can automate Windows applications, classic Windows applications, universal Windows applications, and mobile applications. We can use this driver on the top of a Selenium library.

It is the latest Windows automation driver. It has advantages over Winum Driver & autoT.
 
Set up required for this:
  1. Windows 10 OS 
  2. Visual Studio 2013+
  3. Winapp driver.exe
  4. Inspect.exe for identifying your element value 
  5. Calculator app for automate
  6. Your PC should be on Developer mode (ON) 
Steps to follow,
  1. Download the Winapp driver from here,

    Download the WindowsApplicationDriver.msi
    Windows Application Automation Testing Using Winapp Driver And C# Beginner

  2. Open the Download folder and process WindowsApplicationDriver.msi up to install Windows application driver.

    Windows Application Automation Testing Using Winapp Driver And C# Beginner

  3. Click it for installation.

    Windows Application Automation Testing Using Winapp Driver And C# Beginner

  4. After installation is complete go to C>>Program Files (x86) >>Windows Application Driver.

    Windows Application Automation Testing Using Winapp Driver And C# Beginner

  5. Double click on WinAppDriver.exe for run the Windows application Driver as below:

    Windows Application Automation Testing Using Winapp Driver And C# Beginner

  6. Now, time to install windows Inspect.exe. 

    To download inspect tool click here,
Windows Application Automation Testing Using Winapp Driver And C# Beginner
  
Note
You can download any tool to inspect elements or find elements. After downloading, run inspect.exe and see the control id, name, class etc.
 
Now, we will move to the coding part,
  1. Open Visual Studio 
  2. Click on File>>New>>Project

    Windows Application Automation Testing Using Winapp Driver And C# Beginner

  3. Click on Test for creating a unit test project.

    Windows Application Automation Testing Using Winapp Driver And C# Beginner

  4. Open NuGet Package Manager to install Appium Driver.

    Installation steps

    Click on Tools >>NuGetPackageManager>>Manage NuGet PackageFor Solution.

    Windows Application Automation Testing Using Winapp Driver And C# Beginner
Open the UnitTest1.cs file and paste the below code.
  1. using System;  
  2. using System.Threading;  
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;  
  4. using OpenQA.Selenium;  
  5. using OpenQA.Selenium.Appium.Windows;  
  6. using OpenQA.Selenium.Remote;  
  7.   
  8. namespace CalculatorAutomationWinappDriver  
  9. {  
  10.     [TestClass]  
  11.     public class UnitTest1  
  12.     {  
  13.         //Appium Driver URL it works like a windows Service on your PC  
  14.         private const string appiumDriverURI = "http://127.0.0.1:4723";  
  15.         //Application Key of your UWA   
  16.         //U can use any .Exe file as well for open a windows Application  
  17.         private const string calApp = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App";  
  18.   
  19.         protected static WindowsDriver<WindowsElement> calSession;  
  20.   
  21.   
  22.         [TestMethod]  
  23.         public void TestMethod1()  
  24.         {  
  25.             if (calSession == null)  
  26.             {  
  27.                 DesiredCapabilities appCapabilities = new DesiredCapabilities();  
  28.                 appCapabilities.SetCapability("app", calApp);  
  29.                 appCapabilities.SetCapability("deviceName""WindowsPC");  
  30.                 //Create a session to intract with Calculator windows application  
  31.                 calSession = new WindowsDriver<WindowsElement>(new Uri(appiumDriverURI), appCapabilities);  
  32.   
  33.                 //Automate Button and Get answer from Calculator  
  34.   
  35.                 //find by Name  
  36.                 calSession.FindElement(By.Name("Nine")).Click();  
  37.                 calSession.FindElement(By.Name("One")).Click();  
  38.                 calSession.FindElement(By.Name("Two")).Click();  
  39.                 calSession.FindElement(By.Name("Three")).Click();  
  40.                 calSession.FindElement(By.Name("Multiply by")).Click();  
  41.                 //find by automation id  
  42.                 calSession.FindElementByAccessibilityId("num9Button").Click();  
  43.                 calSession.FindElementByAccessibilityId("equalButton").Click();  
  44.                 //getting value from textbox  
  45.                 string ExpectedValue = calSession.FindElementByAccessibilityId("CalculatorResults").Text;  
  46.                 string ExpectedValue1 = ExpectedValue.Replace("Display is ","").Replace(",","");  
  47.   
  48.                 //Testcases  
  49.                 Assert.AreEqual(82107,Convert.ToInt64(ExpectedValue1));  
  50.             }  
  51.         }  
  52.     }  
  53. }  
Now, you can automate calculator.
Windows Application Automation Testing Using Winapp Driver And C# Beginner
 
Some basic steps to find elements using Inspect.exe:
 
Windows Application Automation Testing Using Winapp Driver And C# Beginner
 
Windows Application Automation Testing Using Winapp Driver And C# Beginner
 
Now, we can automate any Windows application using C# as shown above. A very important note: Don't worry if you are new in identifying Elements on Inspect.exe.
 
Note
In the upcoming blog, I will be automating a classic Windows application.