Ignore error when UI element not found

Jan 16, 2013 at 5:46 AM

Hi.

Is there a way to specify that I don't want an error to appear when a command fails? For example:

$Button = Get-UIAWindow -Name 'MyWindow' | Get-UIAButton -Name 'MyButton'

I want to use such scenario to test if a certain button is present. If it is, I must do something, if it's not, there is no need for action (I am testing if $Button is $Null). Most of the times, the button will not be there.

But I receive the error: The given path's format is not supported.

I tried using the -ErrorAction SilentlyContinue switch, but doesn't work.

Can this be done somehow?

Thanks.

Coordinator
Jan 16, 2013 at 6:26 AM
Edited Jan 16, 2013 at 6:27 AM

Hi JohnQuest,

below is possible what you want:

$ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
# we are seeking for the legendary "10" button in Calculator
$button = Start-Process calc -PassThru | Get-UIAWindow | Get-UIAButton -n 10
if ($null -ne $button) { Write-Host "not null" } else { Write-Host "null" }

There is a better way (it's just my opinion. Practicians may argue):

$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Continue;
Start-Process calc -PassThru | Get-UIAWindow | Test-UIAControlState -SearchCriteria @{controltype="button";name="10"}

There are two cmdlets for true/false answer, where question is "is there a control ... ?" (Test-UIAControlState) or "will be a control ... available in _Timeout_ seconds?" (Wait-UIAControlState).

The Test-UIAControlState checks the state of a window. For example, we need to be sure that today there are buttons 1,3,5 on the Calculator form:

Start-Process calc -PassThru | Get-UIAWindow | Test-UIAControlState -SearchCriteria @{name="1"},@{name="3"},@{name="5"}

The Wait-UIAControlState cmdlet does exactly the same.

Start-Process calc -PassThru | Get-UIAWindow | Wait-UIAControlState -SearchCriteria @{name="1"},@{na
me="3"},@{name="5"}

The difference is that this cmdlet is for dynamic applications like wizards or forms that paint controls by some external rule (an order was found in the DB, etc).

Note: the window you've gotten last is always the current. After successful Get-UIAWindow you can use Get-UIAButton without repeating Get-UIAWindow every time...

Coordinator
Jan 16, 2013 at 6:39 AM

There is an awkward doc page

http://uiautomation.codeplex.com/wikipage?title=Test-UIAControl

I'll definitely re-write the page and publish a post (I even never blogged about these cmdlets, stupid me - from where practicioners should know about them?).

This is time to publish a pocket reference (wish I had time...).