A Guide to Desktop Applications UI Automation with PowerShell
The UIAutomation project extends the functionality of PowerShell to enable GUI automation testing. It supports a range of platforms, including Win32, Windows Form, WPF, and others.
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 )
In this guide, we will delve into how to utilise PowerShell’s module for UI automation. A Windows Form application will serve as our practical example.
Learning Outcomes:
Starting with Microsoft UIAutomation
As this project offers the only library for PowerShell scripts, there’s no need for an installation. You just need to download the module and incorporate it into your script.
Also recommended => 35+ Top GUI Testing Tools with Comprehensive Details
To broaden your knowledge on PowerShell and get started, consider checking out:
- Extensions for UI Automation in PowerShell
- Overview of UI Automation
- Windows PowerShell
- Interesting Features of PowerShell 5.0 in Windows 10
Incorporating UIAutomation
1) Download and extract the most recent package from the official website: UI Automation PowerShell Extensions to a specific location. For example, C:UIAutomation
2) Incorporate the module into your PowerShell script. (Note: Make sure you are not executing this command as an Administrator)
Import-Module C:UIAutomationUIAutomation.dll
Now, you’re set to use the commands defined in the module for initiating UI automation tests.
UI Automation instances
Let’s kick off with a straightforward Windows Form application, which encompasses some frequently used controls in GUI automation testing.
In this instance, our mission is to automate the process of submitting a form comprised of basic personal details:
Step #1) Input the name “Anna Smith”
Step #2) Opt for the gender “Female”
Step #3) Elect “Yes” for the question regarding graduation
Step #4) Press “Submit”
The necessary script for this process 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'|
The above script clearly shows how it influences the controls in the application. Now, let’s dive more into it.
Taking the first step as our instance:
Get-UIAWindow -Name 'Test Form' | Get-UIATabItem -Name 'BasicInfo'| Get-UIAEdit -AutomationId 'textBox1' -Name 'Name'|Set-UIATextBoxText "Anna Smith"
This is essentially what happens in that line:
1) The script initiates by locating the main window and then discovers its child control through the pipeline. This process goes on until it identifies the target control, which is the edit (or text) box labelled “Name”.
2) After locating the control, it identifies the type, such as Get-UIAWindow for a window and Get-UIAEdit for a text box.
3) It also needs one or more properties of the control. In this case, we only use the Name property to distinguish the control. Note: AutomationId and Class are also regularly used properties when identifying a control.
4) Once the target control is discovered, another command is utilized to undertake an action on the control. In this instance, Set-UIATextBoxText is employed to assign the text for a text box.
This is the fundamental method used by UI Automation to accomplish UI automation.
You can spot the commands for varying control types and the commands for activating actions in the documentation on its official website: UI Automation PowerShell Extensions
The rest of the steps adopt a similar approach. Now let’s move on to other important aspects.
Discovering parent-child relationships and control properties
Scribing these pipelines is reasonably simple, but the tricky part is determining the vital properties and the child controls connected with the target control. Based on my experience, I have tested two methods:
#1) Employ the UIAutomationSpy tool included in the package:
This tool, provided with the downloaded package, can capture controls on the desktop. Here are the steps to use it:
- Press “Start” to commence capturing.
- Move the cursor to the control you want to capture.
- Once the control is accentuated and its script is revealed in the tool interface, it signifies the capture was successful.
- Hit “Stop” to conclude the capture.
- Press on the “Hierarchy” tab on the left to identify the parent-child relationship of the controls from the top to the target.
- Press the “Script” tab on the right to see the thorough pipeline for the control.
Bare in mind, you don’t necessarily have to incorporate every single control and property from the pipeline in order to locate the target control. Indeed, your method can be considerably more straightforward (as exhibited in the above example):
Get-UIAWindow -Name 'Test Form' | Get-UIATabItem -Name 'BasicInfo'| Get-UIAEdit -AutomationId 'textBox1' -Name 'Name'
Regrettably, no definitive answer exists for how many controls should be included. This task requires trial and error, as well as experience.
Downsides of UIAutomationSpy:
- The interface is not user-friendly.
- You need to capture one control after another to extract the necessary details.
#2) Use external tools to inspect controls:
A suggested tool to use is inspect.exe offered by Microsoft. Once you launch this tool, it displays all the details of the controls on the window:
Disadvantages of inspect.exe:
- There’s no functionality to export the details.
- While the UI Automation module provides some commands like Get-UIAControlChildren to obtain all the child controls and inspect.exe can traverse all the controls under the window, if the window is complex, it may lead to performance problems.
Advanced techniques: Simplifying the script methodology
Given the above introduction, you might realise that pipelines are the main method to write scripts using the module. However, at times, pipelines can become complicated. One method to simplify and streamline this process involves extracting control data into an XML file and dynamically creating the pipelines as needed.
Keeping with the previous example:
Firstly, generate an XML file that includes the properties and parent-child relationships of the controls. Every 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 designated as attributes of the nodes.
Here’s the XML file for the Test Form:
You need to define some functions 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 usually one-off tasks unless the controls are modified.
Now, you can automate the steps in the scripts below:
#import the functions defined previously . MyLibrary.ps1 setText "Name" "Anna Smith" selectItem "Gender" "Female" toggleRadioButton "Graduated" click "Submit"
This technique eliminates the need for lengthy pipelines in scripts and greatly enhances efficiency.
Suggested read => An Introduction to the Sikuli GUI Automation Tool
Final thoughts:
Microsoft UIAutomation is a top-notch library for those aiming to conduct UI tests on Windows desktops using PowerShell.
The above introduction only touches the surface of the tool. The simplified method is inspired by the notion of extracting data from testing scripts, which is an effective method to boost our efficiency in writing automation tests. For further reading, please visit here.
Please feel free to share your comments below. We would appreciate hearing from UIAutomation users about their experiences.