In the PowerShell UI Automation tutorial, we’ll focus on how PowerShell uses the module to implement UI automation. To explain this we will use the Windows Form application as an example.
UI Automation is an open source project PowerShell extension for GUI automation tests. It supports Win32, Windows Form, WPF, and so on.
Let’s get started and learn more about PowerShell UI Automation in detail.
Table of Contents:
Getting started with PowerShell UIAutomation
Since the project only provides the library for the PowerShell scripts, it does not require installation, we can just download the module and import it to the script.
Also read => 35+ Best GUI Testing Tools with Complete Details
To learn more about PowerShell and understand how to get started with it, check out:
- UI Automation PowerShell Extensions
- UI Automation Overview
- Windows PowerShell
- Cool Stuff about PowerShell 5.0 in Windows 10
Import UI Automation
1) Download and unzip the latest package from its official website: UI Automation PowerShell Extensions to local path, for example, C:\UIAutomation
2) Import the module into the PowerShell script: (Note: When you run this command, make sure you are not running PowerShell as an Administrator)
Import-Module C:\UIAutomation\UIAutomation.dllWe can now use the commands defined in the module to start UI automation testing.
UI Automation with examples
Let’s begin with a simple Windows form application, which covers some typical controls we may see in GUI automation testing.
In this example, we are told to automate the steps to submit a form with some basic personal information:
Step #1) Input the name “Anna Smith”
Step #2) Select the gender “Female”
Step #3) Choose “Yes” as the answer to if graduated.
Step #4) Click “Submit”The script to do it goes like this:
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 of if 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'|You can probably tell from the script given above, how it is manipulating the controls in the application. Let us understand further.
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 is a breakdown of what is happening along this line:
1) The script first finds the top window and then finds its child control through the pipeline. It does this until it finds the target control- the edit box (or textbox) with the name “Name”.
2) Upon finding the control, it claims for the type of control so that you know what command to use, e.g. Get-UIAWindow – is a window and Get-UIAEdit – is an edit/text box.
3) It also requires one or more properties of the control. In this example, we use only Name as the property to identify the control. Note: AutomationId and Class are also properties that are commonly used when finding a control.
4) After the target control is found, we use another command to invoke the action on the control. In this example, it is Set-UIATextBoxText to set the text for a text box.
This is the main way for UI Automation to realize UI automation.
You can find the commands for different types of controls and the command to invoke actions in the help documents of its official site: UI Automation PowerShell Extensions
The rest of the steps are similar in vein. So, let us now move on to other important topics.
Find the parent-child relationship and the properties of the control
It is simple to write such pipelines, but the significant part is how to find the properties and the child controls containing the target control. In my experience, these are the two I have tried:
#1) Use UIAutomationSpy in the package:
There is a tool in the package we downloaded that can capture the controls on the desktop. When the UI is ready on the desktop, open UIAutomationSpy.exe:
- Click “Start” to start the capture.
- Move the cursor to the control you want to capture.
- When you find the controls highlighted and the script displayed on its window interface, it means the capture is successful.
- Click “Stop” to stop the capture.
- Click “Hierarchy” tab on the left and you’ll see the parent-child relationship of the controllers from top to the target
- Click “Script” tab on the right, and you can see a full pipeline for the control:
Please note that we don’t have to include every control and every property from the pipeline to search for the target control. What we use is quite simple (see the example above):
Get-UIAWindow -Name 'Test Form' | Get-UIATabItem -Name 'BasicInfo'| Get-UIAEdit -AutomationId 'textBox1' -Name 'Name'Unfortunately, no official silver bullet exists on how many controls should be included. It requires trying and experience.
What’s not so great about UIAutomationSpy:
- It does not have a friendly interface
- You’ll have to capture one control after another and get the details you want.
#2) Use external tools to inspect controls for windows desktop
The suggested tool is inspect.exe provided by Microsoft. When you open the tool, you’ll get every detail of the controls on the window:
What’s not so great about 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. inspect.exe can transverse all the controls under the window, but the performance is not assessed for complex windows.
Advanced implementation: simplify the approach to writing the script
From the above introduction, you may see that pipelines are the main way to write the script using the module, although sometimes the pipelines are not so long. There is an approach to make it simpler and easier, and the main idea is to extract the data of the controls into an XML file, and join the pipelines when needed from the XML file.
Let’s continue with the above example:
First, generate an XML file including the properties and the parent-child relationship of the controls. We add nodes for every control required: the node name is unique so that we can find the control immediately by name, and the command and its properties are set as the properties of the nodes.
Here is the XML file for the Test Form we test:
<TestForm method="Get-UIAWindow" Name="Test Form" Seconds="5"> <Basicinfo method="Get-UIATabItem" Name="BasicInfo"> <Name method="Get-UIAEdit" Name="Name" AutomationId="textBox1" /> <Gender method="Get-UIAComboBox" Name="Gender" > <Male method="Get-UIAListItem" Name="Male"/> <Female method="Get-UIAListItem" Name="Female"/> </Gender> <Graduated method="Get-UIARadioButton" Name="Yes" AutomationId="Graduated" /> <NotGraduated method="Get-UIARadioButton" Name="No" AutomationId="Graduated" /> </Basicinfo> <Submit method="Get-UIAButton" Name="Submit" /> </TestForm>Some functions should be defined to join the pipelines by searching the xml file and executing them:
function getPipeline($nodeName) { $object_xml = [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) { $pipeline = getPipeline $textbox $iex = $pipeline + "| 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 }So far, these are almost one-time tasks unless the controls themselves change.
We can now 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"In this way, we can get rid of bunches of pipelines in the scripts, and the proficiency will be improved greatly.
Recommended read => Introduction to Sikuli GUI Automation Tool
Conclusion:
Microsoft UIAutomation is a great library for those who want to take UI automation tests on a Windows desktop with PowerShell.
The introduction given in the article is just a segment of the tool. This simplified approach is inspired by the idea of extracting data from testing scripts, which is a good way to improve our proficiency in writing automation tests. Explore more about it here.
Please share your feedback in the comments section below and we would love to hear experiences from UIAutomation users.
Was this helpful?
Thanks for your feedback!
Where the heck is UIAutomation.dll? I downloaded and extracted, but in the UIAutomation I only see the folders discussions, issues, licenses, release, sourceCode, wiki. Doing a search on the whole folder doesn’t show any DLL file anywhere.
unable to locate UIAutomation.dll anywhere, could you please provide the proper location. Thank you in advance for your help.
i used this lib to automate a winform app – but had problems after the lock timesout and the security desktop is enabled (and my desktop and all controls are unfocusable and interaction then causes errors). anyway not easy way round that problem.
but i notice lots of people not understanding how to install the lib. i did this:-
1- extract the zip to a dir
2-within that dir look int he \UIAutomation\releases subdir for the file “releaseList.json”
3-open releaseList.json with notepad and look at the section for the release u want (i went for 44)
4-goto the subdir within \UIAutomation\releases for the release you chose (in my case dir called “44”)
5-now from the json section you picked, now pick the line for the level of .net you want (i was using 4.0) so found this:-
“Id”: “0c03ddc0-0666-4b73-857b-84ddd63a9149”,
“FileName”: “UIAutomation.0.8.7B3.NET40.zip”,
“Url”: “./44/0c03ddc0-0666-4b73-857b-84ddd63a9149”,
“Type”: “Application”,
“UploadDate”: “2014-07-12T01:46:08.83-07:00”
now in the subdir rename the file “0c03ddc0-0666-4b73-857b-84ddd63a9149” (the id attribute value) to “UIAutomation.0.8.7B3.NET40.zip” (the filename attribute)
7-now open the zip file you just create from the rename step above – within are the dll etc and copy them out to wherever u need…..
Thank you! You’re a life saver. Just saw this right after I posted my comment.
How to read the content from text box?
we have developed the windows application(c++)IDE is Embarcadero(c++builder) so we want automation testing on that application so could please help us to which one is better and by using xml file there will be having a chance of automation testing please suggest me
thanku for this to share a imporatnt information
I am trying to locate a copy of the UIAutomation.dll but can’t seem to find it. Any suggestions or release that I can download? I’ve tried pulling the GIT repo but getting a tonne of build errrors.
Thanks!
good tutorial
lovely page! champion!
This is a good tutorial. However I am a new to PS, I get stuck after “stop recording”. How do I convert the recorded script to xml? how do use this script in powershell?
Now i am able to make it work by making the entire files in UIAutomation folder to “Unblocking”
This is very good article
Is it possible to find parent-child relationship and the properties of the control WITHOUT using .exe files? I am in a restricted work environment and I can’t run any external executables, but I can use Powershell and I would like to automate some of my daily tasks.
Same for me as well, in current context Import-module in powershell not able to import UIAutomation.dll and the zip also not containing the .dll file any more
Hello;
With Unzipped directory at C:\UOAutomation, the PS command Import-Module C:\UIAutomation\UIAutomation.dll produces the error message
Import-Module : The specified module ‘C:\UIAutomation\UIAutomation.dll’ was not loaded because no valid module file was found in any module directory.
At line:1 char:1
+ Import-Module C:\UIAutomation\UIAutomation.dll
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (C:\UIAutomation\UIAutomation.dll:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Suggestions?
Thank you;
Allan
Nice Article… Need more articles on Automation
Hello,
I have downloaded the UiAutomation.zip from the provided link but the file UIAutomation.dll does not exist anywhere. It seems that the zip does not contain the dll, the source code is in the zip but it cant be build. From where is the UIAutomation.dll available?
can you help me please with this
Import-Module C:\UIAutomation\UIAutomation.dll
Import-Module : Could not load file or assembly ‘file:///C:\UIAutomation\UIAutomation.dll’ or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
At line:1 char:1
+ Import-Module C:\UIAutomation\UIAutomation.dll
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Import-Module], FileLoadException
+ FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.ImportModuleCommand
* In Windows Explorer, navigate to the installation media ‘C:\UiAutomation’ (If you followed suggested PATH) folder
* select UIAutomation.dll and click Properties.
* In the General tab, click Unblock.
* Click OK to close the dialog.
* restart PowerShell and perform the same steps as before
There is no un block button in properties.
Hi,
I am stuck on this. There is no UIAutomation.dll
There is:
discussions
issues
license
releases
sourceCode
wiki
I have even done a search in C:\UiAutomation for any dll files but there are none.
Please help this looks promising.
Hi,
Need your help please,
i have downloaded .dll file as mentioned above but the problem is unable to import the module.This command
“Import-Module C:\UIAutomation\UIAutomation.dll” is not working.
It is showing the error as ”The system cannot find the file as specified”
Import-Module : Could not load file or assembly ‘PSTestLibrary, Version=0.8.7.73, Culture=neutral,
PublicKeyToken=null’ or one of its dependencies. The system cannot find the file specified.
can any one help on this please?
Below link as the steps to download and use the dll.
https://stackoverflow.com/questions/51231731/how-to-import-uiautomation-files-in-powershell
No longer works
Are there new instructions on how to import the uiautomation.dll to Powershell. None of the above instructions are valid