Daily automation: search for a specific control


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;
Advertisements

2 responses

  1. Search-UIAControl – does not exist as a cmdlet ?

    1. Hi Ram S, you’re right. It was replaced ages ago by the Get-UiaControl (Get-UiaWahtever, for example, Get-UiaButton) cmdlet.
      The reason was that initially Get cmdlets returned only the first element. The idea was taken probably from the WASP framework. One day Tobias Waltner suggested to make it the way as it’s done in Microsoft’s cmdlets as a standard: a Get- cmdlet returns all the items of types the cmlets retuens. For example, Get-ChildItem returns all the elements of real or any other file system (SQL objects, etc). By the analogy, the Get-UiaControl/Get-UiaButton/Get-UiaComboBox/etc return all the elements of the type the cmdlet supports.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: