Sometimes, the way that MS UI Automation uses when searches for a control may shock. Testers complained that after the application’s grid in mmc has been filled with default data (260 rows to 6 columns), searches slowed down significantly.
First, I didn’t perceive the seriousness and retorted that fifteen seconds’ search is not a problem. Complaints continued. I used Measure-Command and faced incredible delays in fifteen to seventeen minutes, almost with no dependency which host they’ve been run on.
Even more was I surprised that this time was constant, did I search for a piece of grid or a button just two or three levels below the window. This clearly said that if MS UI Automation chose the path for search, it returns what is expected no sooner than the search run around the whole Automation tree.
Okay, the short cut is now here. How search worked before? It tried to get a control by means of MS UI Automation. If the attempt failed for any reason, FindWindowsEx has been used. Need to say that there was a bug in using FindWindowEx, it could not find anything lower than one level from the root of search.
If both searched failed, the cycle phased and searches started again until timeout expires.
As can be seen, the slowest but fruitful part is the first, the MS UI Automation search. The newly added parameter -Win32 switches off the MS UI Automation search and recursive FindWindowEx search comes to light.
Task: demonstrate how to use Win32 search for a control.
Requirements: there’s no particular requirements as there is no way to ask somebody to find an application that will surely be slow.
Solution: the first example shows a typical search:
Get-UIAWindow -pn mmc | Get-UIAButton -Name Change Get-UIAWindow -pn mmc | Get-UIAButton -Name Chan*
Both lines of code do the same thing: they use MS UI Automation for performing a search. In our case, both searches finished within fifteen minutes and this was indivisible search INSIDE MS UI Automation.
With the -Win32 parameter announced, only Win32 search works and the control of interest is being returned at ögonblick:
Get-UIAWindow -pn mmc | Get-UIAButton -Name Change -Win32
The FindWindowEx does not support wildcard search. To sugar a pill, it is case insensitive.
Of course, it will work only with a control that have a handle. This parameter will be added to the 0.7.5. release.