Sometimes we don’t know the type of object we are searching for. Often, we don’t even want to know the type. For instance, we need to check the list of controls is represented on a form or wizard page. To have this task done, we need the way that led as directly to the control of our interest.
Another example is a rich form. This may be OWA (Outlook Web Access) or anything else with a grid or a list with data. If we need to get a particular control or controls, there’d be useful a search allowing us to type in a part of name.
These tasks can be easily accomplished with the Search-UIAControl cmdlet. Let’s see how to do that.
Task: demonstrate using the search cmdlet.
Requirements: perform search that consumes a part of control’s name.
Solution: the first search shown below is inside the Calculator window:
Set-StrictMode -Version Latest; ipmo [path]\UIAutomation.dll; [UIAutomation.Mode]::Profile = [UIAutomation.Modes]::Presentation; cls; # here we are searching for all the element whose names start with 'a' Start-Process calc -PassThru | Get-UIAWindow | Search-UIAControl -Name A*; # here we get only button(s) Start-Process calc -PassThru | Get-UIAWindow | Search-UIAControl -ControlType button -Name A*; # this code should return the same as the first code snippet, # because there are only two types of controls whose names start with 'a' Start-Process calc -PassThru | Get-UIAWindow | Search-UIAControl -ControlType button,menubar -Name A*;
The second code start the Control Panel window, searches for the Sound link, open the dialog and searches inside for the Speakers control. Which type does the control have? I don’t know unless I run the code:
Set-StrictMode -Version Latest; ipmo [path]\UIAutomation.dll; [UIAutomation.Mode]::Profile = [UIAutomation.Modes]::Normal; # click the Start button Get-UIADesktop | Get-UIAButton -Name Start | Invoke-UIAButtonClick; # open the Control Panel window Get-UIAListItem -Name 'Control Panel' | Invoke-UIAControlClick; # search for the Sound dialog $w = Get-UIAWindow -Name 'Control Panel'; # the name will be changed during the test Get-UIAWindow -Name 'Control Panel' | Get-UIAEdit -AutomationId 'SearchEditBox' | Set-UIAEditText -Text 'sound'; sleep -Seconds 1; # for the Normal mode only # if you are running this script with the # [UIAutomation.Mode]::Profile = [UIAutomation.Modes]::Presentation # set, you can cut out this sleep # Otherwise, there happened someting like 'the window couldn't form itself without a bit of time' # Thus, this is unavoidable timeout (at least a half of second) # open the Sound dialog #Get-UIAHyperlink -AutomationId 'name' -Name 'Sound' | Invoke-UIAHyperlinkClick; #$w | Get-UIAHyperlink -Name 'Sound' | Invoke-UIAHyperlinkClick; $w | Search-UIAControl -Name 'Sound' -Highlight | Invoke-UIAHyperlinkClick; # search for the Speakers device Get-UIAWindow -Name 'Sound' | Search-UIAControl -Name 'Speak*' -Highlight;