Tag Archives: wildcard

Daily automation: a few windows in parallel


At work, an interesting question was raised: is it possible to work with several windows in parallel if their processes are run under various users? Yes, it surely is.

Task: use UIAutomation to get windows of AUTs that are run under various users.

Requirements: demonstrate how to use the technique and how to determine which window might cause a problem.

Solution: the first example is simple and demonstrates how to handle several windows:

Set-StrictMode -Version Latest;
ipmo [path]\UIAutomation.dll
Start-Process calc;
Start-Process calc;
Start-Process calc;
Start-Process calc;
Get-UIAWindow -ProcessName calc | Get-UIAButton -Name 1 | Invoke-UIAButtonClick | Read-UIAControlName
Get-UIAWindow -ProcessName calc | Get-UIAMenuItem -n vi* | Invoke-UIAMenuItemExpand | Get-UIAMenuItem -n scien* | Invoke-UIAMenuItemClick

The sample starts the foursome of calculators, gets them all, clicks the 1 button and changes application mode to the Scientific one.

There is no difficulty with windows run under several user accounts while your test process (i.e., powershell) is running with at least equal or higher privileges.

Now, we’ll start calc under two different user accounts (credentials are not provided to the public domain just for you not locking your accounts ;):

Set-StrictMode -Version Latest;
ipmo [path]\UIAutomation.dll;
(Start-Process calc -Credential (Get-Credential) -PassThru).Id
(Start-Process calc -Credential (Get-Credential) -PassThru).Id
# Here, we are writing up numbers to a paper
Get-UIAWindow -ProcessId 5924,6076 | Get-UIAMenuItem -Name vi* | Invoke-UIAMenuItemExpand | Get-UIAMenuItem -Name scien* | Invoke-UIAMenuItemClick

This works, but the sample is not friendly to use: you need to store process ids. Okay, below is a better one-liner:

Set-StrictMode -Version Latest;
ipmo [path]\UIAutomation.dll;
@((Start-Process calc -Credential (Get-Credential) -PassThru).Id; (Start-Process calc -Credential (Get-Credential) -PassThru).Id) | %{ Get-UIAWindow -ProcessId $_ | Get-UIAMenuItem -Name vi* | Invoke-UIAMenuItemExpand | Get-UIAMenuItem -Name stat* | Invoke-UIAMenuItemClick; }

Finally, how to learn which window failed? For example, by defining a counting variable that will increment every window and point to an element of the windows array.

Daily automation: eliminating the fragility of tests. Part 2


The logically next step, after we started searching for a control using a wildcard, is to get a window by part of its name.

Task: use UIAutomation to get a window by a fragment of its name.

Requirements: demonstrate how to use the technique.

Solution: code that is needed is as simple as it can be:

Set-StrictMode -Version Latest;
ipmo [path]\UIAutomation.dll;
Start-Process calc; Get-UIAWindow -Name *alc*r

After we started the calc process, all the lines of code below will work:

Set-StrictMode -Version Latest;
ipmo [path]\UIAutomation.dll;
Start-Process calc;
Get-UIAWindow -Name *alc*r
Get-UIAWindow -Name Calc*
Get-UIAWindow -Name CA?culator
Get-UIAWindow -pn calc
Get-UIAWindow -pid (Start-Process calc -PassThru).Id
Start-Process calc -PassThru | Get-UIAWindow

There should be placed a note: whereas the asterisk ‘*’ is a replacement for anything including an empty place, the question mark ‘?’ requires that something non-empty was under the sign. I.e., the following replacements are right:
Calc*
*Calculator*
C*l*l*
However, Cal?culator is not right. Ca?culator or Cal?ulator both are right.

%d bloggers like this: