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. namespace CalculatorAutomationWinappDriver
  8. {
  9. [TestClass]
  10. public class UnitTest1
  11. {
  12. //Appium Driver URL it works like a windows Service on your PC
  13. private const string appiumDriverURI = "http://127.0.0.1:4723";
  14. //Application Key of your UWA
  15. //U can use any .Exe file as well for open a windows Application
  16. private const string calApp = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App";
  17. protected static WindowsDriver<WindowsElement> calSession;
  18. [TestMethod]
  19. public void TestMethod1()
  20. {
  21. if (calSession == null)
  22. {
  23. DesiredCapabilities appCapabilities = new DesiredCapabilities();
  24. appCapabilities.SetCapability("app", calApp);
  25. appCapabilities.SetCapability("deviceName", "WindowsPC");
  26. //Create a session to intract with Calculator windows application
  27. calSession = new WindowsDriver<WindowsElement>(new Uri(appiumDriverURI), appCapabilities);
  28. //Automate Button and Get answer from Calculator
  29. //find by Name
  30. calSession.FindElement(By.Name("Nine")).Click();
  31. calSession.FindElement(By.Name("One")).Click();
  32. calSession.FindElement(By.Name("Two")).Click();
  33. calSession.FindElement(By.Name("Three")).Click();
  34. calSession.FindElement(By.Name("Multiply by")).Click();
  35. //find by automation id
  36. calSession.FindElementByAccessibilityId("num9Button").Click();
  37. calSession.FindElementByAccessibilityId("equalButton").Click();
  38. //getting value from textbox
  39. string ExpectedValue = calSession.FindElementByAccessibilityId("CalculatorResults").Text;
  40. string ExpectedValue1 = ExpectedValue.Replace("Display is ","").Replace(",","");
  41. //Testcases
  42. Assert.AreEqual(82107,Convert.ToInt64(ExpectedValue1));
  43. }
  44. }
  45. }
  46. }
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.