Windows UI Automation Tutorial
Windows provides a programmatic interface to automate various elements.
Windows Automation saves time and effort in executing regression test cases.
Let us start with an example to automate calculator perform a simple operation of 2+5
In this case on conerting 84(HEX) TO DECIMAL we get 132
Windows Automation saves time and effort in executing regression test cases.
Let us start with an example to automate calculator perform a simple operation of 2+5
- Step 1:Open Visual Studio and create a new console application
- Step 2:Navigate Tools>NuGet Package Manger>Manage Nuget Packages for solution... and click on it
- Step 3:In the search bar find system.windows.automation and add UIaComWrapper to current solution
- Step 4: Add "using System.Windows.Automation;" namespace
- Step 5:Now open calculator
- Step 6:In visual studio navigate to TOOLS>SPY ++(x64)
- Step 7:In the Microsoft Spy++ pop up window click on Find window
- Step 8:Now drop the pointer to 2 in calculator and click on ok
- Step 9:Now in the Inspect window click on properties.
In this case on conerting 84(HEX) TO DECIMAL we get 132
- Step 11:Repeat from steps 7 to 10 to find control id value for digit "5" "=" and "+" respectively
Final Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.IO;
namespace Windows_UI
{
class Program
{
static void Main(string[] args)
{
Program automate = new Program();
//Calls automate function
automate.Func();
}
public void Func()
{
AutomationElement _calculatorAutomationElement, r2, rplus, r5, requal;
Process _calculatorProcess;
//Opens Calculator
_ calculatorProcess = Process.Start(@"calc.exe");
int ct = 0;
do
{
//Finds if automation element is available
_calculatorAutomationElement = AutomationElement.RootElement.FindFirst (TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty,
"Calculator")); ++ct;
Thread.Sleep(100);
}
while (_calculatorAutomationElement == null && ct < 50);
//Finds the digit 2 through automation ID
r2 =_calculatorAutomationElement.FindFirst(TreeScope.Descendants,newPropertyCondition
(AutomationElement.AutomationIdProperty, "132"));
//Presses the digit 2
GetInvokePattern(r2).
Invoke();
Thread.Sleep(100);
//Finds the digit + through automation ID
rplus = _calculatorAutomationElement.FindFirst
(TreeScope.Descendants,newPropertyCondition
(AutomationElement.AutomationIdProperty, "93"));
//Presses the digit +
GetInvokePattern(rplus).Invoke();
Thread.Sleep(100);
//Finds the digit 5 through automation ID
r5=_calculatorAutomationElement.FindFirst(TreeScope.Descendants,newPropertyCondition
(AutomationElement.AutomationIdProperty, "135"));
//Presses the digit 5
GetInvokePattern(r5).Invoke();
Thread.Sleep(100);
//Finds the equal operation through automation ID
requal = _calculatorAutomationElement.FindFirst(TreeScope.Descendants,newPropertyCondition
(AutomationElement.AutomationIdProperty, "121"));
//Presses the equal symbol
GetInvokePattern(requal).Invoke();
Thread.Sleep(100);
}
//This function clicks the automation element
InvokePattern GetInvokePattern(AutomationElement element)
{
return element.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
}
}
}
No comments:
Post a Comment