Tutorial on UI Automation with PowerShell: Automating Desktop Applications
UIAutomation is a project that extends PowerShell for GUI automation testing. It supports various platforms such as Win32, Windows Form, WPF, etc.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
This tutorial will focus on PowerShell’s use of the module for implementing UI automation. To illustrate this, we will use a Windows Form application as an example.
What You Will Learn:
Getting started with Microsoft UIAutomation
Since the project provides the only library for PowerShell scripts, it does not require installation. You can simply download the module and import it into your script.
Also read => 35+ Best GUI Testing Tools with Complete Details
To learn more about PowerShell and get started with it, check out:
- UI Automation PowerShell Extensions
- UI Automation Overview
- Windows PowerShell
- Cool Stuff about PowerShell 5.0 in Windows 10
Importing UIAutomation
1) Download and unzip the latest package from its official website: UI Automation PowerShell Extensions to a local path, for instance, C:UIAutomation
2) Import the module into the PowerShell script: (Note: When executing this command, make sure you are not running PowerShell as an Administrator)
Import-Module C:UIAutomationUIAutomation.dll
Now you can use the commands defined in the module to start UI automation testing.
UI Automation examples
Let’s start with a simple Windows Form application, which encapsulates some common controls used in GUI automation testing.
In this example, we have been instructed to automate the steps to submit a form with basic personal information:
Step #1) Enter the name “Anna Smith”
Step #2) Select the gender “Female”
Step #3) Choose “Yes” as the answer for whether graduated
Step #4) Click “Submit”
The script to accomplish this is as follows:
Start-Process "Test Form" #Step1: input the name "Anna Smith" Get-UIAWindow -Name 'Test Form' | Get-UIATabItem -Name 'BasicInfo'| Get-UIAEdit -AutomationId 'textBox1' -Name 'Name'|Set-UIATextBoxText "Anna Smith" #Step2: select the gender "Female" Get-UIAWindow -Name 'Test Form' | Get-UIATabItem -Name 'BasicInfo'| Get-UIAComboBox -Name 'Gender' | Invoke-UIAListItemSelectItem -ItemName "Female" #Step3: choose "Yes" as the answer for whether graduated Get-UIAWindow -Name 'Test Form' | Get-UIATabItem -Name 'BasicInfo'| Get-UIARadioButton -AutomationId 'Graduated' -Name 'Yes'|Invoke-UIAControlClick #Step4: click "Submit" Get-UIAWindow -Name 'Test Form' | Get-UIAButton -Name 'Submit'|
From the above script, you can see how it manipulates the controls in the application. Let’s delve further into it.
Let’s take the first step as an example:
Get-UIAWindow -Name 'Test Form' | Get-UIATabItem -Name 'BasicInfo'| Get-UIAEdit -AutomationId 'textBox1' -Name 'Name'|Set-UIATextBoxText "Anna Smith"
Here’s a breakdown of what happens in this line:
1) The script first locates the top window and then finds its child control through the pipeline. It continues this process until it finds the target control, which is the edit box (or textbox) named “Name”.
2) After finding the control, it determines its type, such as Get-UIAWindow for a window and Get-UIAEdit for an edit/text box.
3) It also requires one or more properties of the control. In this example, we use only the Name property to identify the control. Note: AutomationId and Class are also commonly used properties when locating a control.
4) Once the target control is found, another command is used to perform an action on the control. In this example, Set-UIATextBoxText is used to set the text for a text box.
This is the main approach used by UI Automation to achieve UI automation.
You can find the commands for different control types and the commands for invoking actions in the documentation of its official website: UI Automation PowerShell Extensions
The remaining steps follow a similar pattern. Let’s now move on to other important topics.
Finding parent-child relationships and control properties
Writing these pipelines is straightforward, but the challenging part is identifying the necessary properties and the child controls associated with the target control. In my experience, I have tried two approaches:
#1) Use UIAutomationSpy tool included in the package:
This tool, which comes with the downloaded package, can capture controls on the desktop. To use it, follow these steps:
- Click “Start” to begin capturing.
- Move the cursor to the control you want to capture.
- When the control is highlighted and its script is displayed in the tool interface, it means the capture was successful.
- Click “Stop” to end the capture.
- Click the “Hierarchy” tab on the left to see the parent-child relationship of the controls from top to the target.
- Click the “Script” tab on the right to see the full pipeline for the control.
Note that you don’t have to include every control and property from the pipeline to find the target control. In fact, your approach can be much simpler (as shown in the example above):
Get-UIAWindow -Name 'Test Form' | Get-UIATabItem -Name 'BasicInfo'| Get-UIAEdit -AutomationId 'textBox1' -Name 'Name'
Unfortunately, there is no definitive answer to how many controls should be included. It requires trial and error and gaining experience.
Limits of UIAutomationSpy:
- It lacks a user-friendly interface.
- You need to capture one control after another and extract the desired details.
#2) Use external tools to inspect controls:
A recommended tool is inspect.exe provided by Microsoft. When you open this tool, you will see all the details of the controls on the window:
Limits of inspect.exe:
- There is no way to export the details.
- The UI Automation module provides some commands such as Get-UIAControlChildren to get all the child controls. While inspect.exe can traverse all the controls under the window, it may have performance issues with complex windows.
Advanced implementation: simplifying the script approach
Based on the above introduction, you may find that pipelines are the primary way to write scripts using the module, although sometimes pipelines can be complex. There is an approach to simplify and ease this process, which involves extracting control data into an XML file and dynamically generating the pipelines when needed.
Continuing with the previous example:
First, generate an XML file that includes the properties and parent-child relationships of the controls. Each required control is represented as a node in the XML file, with a unique node name for easy identification. The command and its properties are set as attributes of the nodes.
Here is the XML file for the Test Form:
Some functions need to be defined to retrieve the pipelines by searching the XML file and executing them:
function getPipeline($nodeName) { $object_xml = (Invoke-WebRequest(controls.xml')) $control = $object_xml.SelectSingleNode("//$nodeName ") $pipeline = "" do { $string = "" $string = $control.method foreach($a in $control.Attributes) { if(!$a.Name.ToLower().Equals("method")) { $string = $string + " -" + $a.Name + " '" +$a.Value + "'" } } $pipeline = $string + " |" +$ pipeline $control= $control.ParentNode } while (!$control.Name.Equals("#")) return $pipeline.Remove($pipeline.length-1,1) } function setText($textbox,$value) Set-UiaEditText -text $value" Invoke-Expression $iex function selectItem($combobox,$item) { #get the pipeline and invoke the expression } function toggleRadioButton($radioButton) { #get the pipeline and invoke the expression } function click($control) { #get the pipeline and invoke the expression }
These tasks are mostly one-time tasks, unless the controls themselves change.
Now you can automate the steps in the following scripts:
#import the functions defined previously . MyLibrary.ps1 setText "Name" "Anna Smith" selectItem "Gender" "Female" toggleRadioButton "Graduated" click "Submit"
This approach eliminates the need for lengthy pipelines in scripts and greatly improves efficiency.
Recommended read => Introduction to Sikuli GUI Automation Tool
Conclusion:
Microsoft UIAutomation is an excellent library for those who want to automate UI tests on Windows desktops using PowerShell.
The above introduction only covers a portion of the tool. The simplified approach is inspired by the idea of extracting data from testing scripts, which is a great way to enhance our proficiency in writing automation tests. To learn more about it, explore here.
Please share your comments below, and we would love to hear from UIAutomation users about their experiences.