Daily automation: peeling the onion


Sometimes, hit happens. It’s a shame to confess, but rarely even I recommend simple Win32 clicks. Today was the story about such clicks. A member of the test team, not a newcomer to UIAutomation and with thousands of test cases in the suite behind, the guy complained that Invoke-UIAControlClick (i.e., PostMessage) did not work!

I approached, and it didn’t work! The panic was spreading out among the testers, suggestions like ‘use mouse_event!’ or ‘revise the code!’ was hovering around the room.

The answer was simple. The Invoke-UIAControlClick cmdlet as well as the PostMessage/SendMessage Win32 call hits by control’s handle. We attempted to do the following:

Get-UIAWindow -n window_name | Set-UIAFocus | Get-UIATab -n tab_name | Invoke-UIAControlClick -X 500 -Y 125;

What the world was awaiting was:

Get-UIAWindow -n window_name | Set-UIAFocus | Get-UIATab -n tab_name | Get-UIAPane | Get-UIAPane | Get-UIAPane | Invoke-UIAControlClick -X 500 -Y 125;

Each control in this pipeline had a handle and there was no point to hit the tab if the link was on the lowest pane.
Layers are of almost the same size and put one over one. Even spy++ couldn’t help as nobody knew which layers was pointed to by the spy++ target cursor in order to collect messages.

Task: help to find the right level.

Requirements: write out the script that hit-tests every layer of controls and prints their handles.

Solution: below is the script that walks down the hierarchy and prints out handles of controls:

function hittest
{
 param(
 [System.Windows.Automation.AutomationElement]$element,
 [int]$X,
 [int]$Y
 )
 if ($element | Invoke-UIAControlClick -X $X -Y $Y) {
 Write-Host "The control has handle $($element | Read-UIAControlNativeWindowHandle)";
 }
}

Get-UIAWindow -Name *window*name* | Set-UIAFocus | `
 Get-UIATab -Name *tab*name* | `
 Get-UIAControlDescendants | `
 %{hittest $_ 500 125;}

The script tries to click on each control and outputs the handle value. The Invoke-UIAControlClick cmdlet couldn’t decide was or wasn’t a click successful and returns $true if no crash were during the script execution.

Anyhow, you can use handles to get controls and click by handle in a cycle to find out the control you need.

Advertisements

2 responses

  1. Please let me know if you’re looking for a writer for your site. You have some really good posts and I believe I would be a good asset. If you ever want to take some of the load off, I’d really like to write some material for your blog in exchange for a link back to mine. Please shoot me an e-mail if interested. Thank you!

    1. Hi Conrad, there are two news for you, one is bad and one is good.
      The bad news is that, even voluntary, a ‘shirt’ guy can’t write about software testing using PowerShell. It’s the truth of life. Who doesn’t use PowerShell, can’t write about it. It’s impossible without practice.

      Nonetheless, there’s a good news. YOU (or anyone else) can become a German mirror of my blog. I suppose you are from Germany or a German-speaking country, taking in account your name and your e-mail’s domain.
      This way you will be given the following bonuses: all posts you translated will be references to from my blog and your mirror will have users’ traffic (from where you can also distribute shirts).
      I also can give you writen permission to sell out shirts with my blog’s slogan (“Software Testing Using PowerShell”. Please note that the terms “software testing” and “powershell” are not mine, I only work under name “Software Testing using PowerShell”).

      I’ll write a page about how to become a translator.
      Oops, I’ve already written it: http://softwaretestingusingpowershell.com/translators-wanted/

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: