Tag Archives: ListItem
Task: click the Computer item in the Start menu.
Requirements: use UIAutomation to demonstrate the technique.
Solution: although the solution is simple, it may be difficult to find out. The current version of UIAutomationSpy is slightly outdated and not yet recommends using the Get-UIADesktop cmdlet. Okay, this time we’ll do it manually, having in mind that a newer version of UIAutomationSpy is promised.
Here is the solution. At first, we open the Start menu by clicking the Start button. After that, we are seeking for a window that is the underlying level which the menu is placed upon. Finally, we get a list item, called of course Computer and send a Win32 click to there.
This sample is not ready to globalization.
ipmo [path]\UIAutomation.dll; # click the Start button and open the Start menu Get-UIADesktop | Get-UIAButton -Name Start | Invoke-UIAButtonClick; # Navigating through the Start menu and clicking the list item of our interest Get-UIADesktop | Get-UIAcontrol -Class 'DV2ControlHost' -Name 'Start menu' | Get-UIAPane -Class 'DesktopSpecialFolders' | Get-UIAListItem -Name 'Computer' | Invoke-UIAControlClick;
After the code is processed by PowerShell, the Computer folder is open.
Task: automate combo box selection.
Task #2: provide a homework helping to learn the control.
Requirements: describe both getting the selection and selecting list items.
Solution: below is the preparation (services.msc is already running, select a service, open the context menu, click Properties):
Get-UIAWindow -Name Services | ` Get-UIADataGrid | ` Get-UIADataItem -Name Alerter | ` Invoke-UIAControlContextMenu | ` Get-UIAMenuItem -Name Properties | ` Invoke-UIAMenuItemClick;
If you are sitting against a Windows 7 box, select for our test another service, for example, BranchCache.
Now the combo box can be seen. It has three or four values (I’m again doing the testing on a Windows XP box as I care about working on older and the newest operating systems): Automatic, Manual, and Disabled. There might or mightn’t be two ‘Automatic…’ items.
How to get what is selected now? Do so:
$((Get-UIAWindow -p mmc | Get-UIAComboBox | Get-UIAComboBoxSelection)).Current
Now, is there a way to set a selection? Who roared ‘SetFocus; SendKeys’? Shut up, it’s not a sport but a torture. If a control is healthy and developers’ hands are sewn directly to shoulders, we can and we must use patterns.
Get-UIAWindow -p mmc | ` Get-UIAComboBox | ` Invoke-UIAComboBoxExpand | ` Get-UIAList | ` Get-UIAListItem -Name Automatic | ` Invoke-UIAListItemSelectItem -ItemName Automatic | ` Invoke-UIAListItemClick;
Too long? Let’s shortenize:
Get-UIAWindow -p mmc | ` Get-UIAComboBox | ` Invoke-UIAComboBoxExpand | ` Get-UIAListItem -Name Automatic | ` Invoke-UIAListItemSelectItem -ItemName Automatic | ` Invoke-UIAListItemClick;
We managed to delete only the Get-UIAList cmdlet. Why?
1) All the Get- cmdlets can perform search from the point they’ve been given with. Given a window, a childwindow or a control, from any of that start, a Get- cmdlet (excluding Get-UIAWindow as it does not have the -InputObject parameter) can continue the search down the Automation tree.
2) All the Invoke- (and pattern-cmdlets Get- and Set- like Get-UIAComboBoxSelection. Recognize them in a way: Get/Set-UIA[ControlType][Pattern name or abbreviation]) cmdlets can’t perform any search. They should be fed with a pipeline from a Get- cmdlet or a cmdlet that passed thru (-PassThru’ed) the object of the AutomationElement type.
To shortenize even more, you may cut out the first call, Get-UIAWindow (as it’s always stored in the [UIAutomation.CurrentData]::CurrentWindow variable), but be very attentively doing so: if you/your test Get-s a window (the main or an active child), closes it and tries to work with subsequent calls of Get-UIAWindow, error definitely will appear.
Homework: practisize working with the combo box, try another values, get the selection (I suppose that you know what is the difference between the selected list item and the selected list item that is braced inside the dotted box), save selection to a file, change selection, read it back from the file you’ve created, set the saved selection to the combo box, and do it until a new post got published. 🙂