(MS) UIAutomation: getting events


Task: open the Options window in the Services MMC snap-in. Register an event on opening. Demonstrate how such events work.

Task #2: provide a field for learning the module.

Requirements: just do it. Register an event, let it out a message box and write to a file.

Solution: run the following code:

[string]$processName = "mmc";
Get-UIAWindow -p $processName | `
	Register-UIAWindowOpenedEvent `
		-EventAction {"fired on wnd open" >> "$($env:TEMP)\wnd1.txt";},
			{[System.Windows.Forms.MessageBox]::Show("wnd is open");};

After that run any of three code samples from my previous post:
https://softwaretestingusingpowershell.wordpress.com/2012/02/12/uiautomation-clicking-a-menu/
The message box appears (maybe, twice – depending on how control’s events are triggered) and in the TEMP folder you’ll find a file.

Task #2 the homework is as: modify the code from post https://softwaretestingusingpowershell.wordpress.com/2012/02/12/uiautomation-clicking-a-menu/ so that it opens one more child window (services.msc was selected as a snap-in of many child windows). After running the code you’ll write, event will be fired. In case you ever closed the window before running you code, you need re-register the event:
open the window
register the event
run your code

Note: you can register so many event actions for a single event as you like.

Let’s add a click event. Now we will add an event to the File->Options…->Options->Cancel button on a click:

$action = {[System.Windows.Forms.MessageBox]::Show("click on Cancel");};
[string]$processName = "mmc";
Get-UIAWindow -p $processName | `
	Get-UIAMenuItem -Name File | `
	Invoke-UIAMenuItemClick | `
	Get-UIAMenuItem -Name 'Options...' | `
	Invoke-UIAMenuItemClick;
Get-UIAChildWindow | `
	Get-UIAButton -Name Cancel | `
	Register-UIAInvokedEvent -EventAction $action;

After clicking this button manually, we’ll get a message box.
Note: this event is for this run only. Why? It’s registered for the particular button. Next time its handle will be different, and the event should be created again.

Note: there’s a trick. As you may have noticed, I use Get-UIAWindow not in every line of code. Why? After having got successfully the window, it’s available in the

[UIAutomation.CurrentData]::CurrentWindow

variable, and Get-UIA[ControlType] cmdlets get it from there unless the window is provided as the -InputObject parameter.

Note: it’s good practice to remove all the events you registered if you don’t need in them anymore:

Unregister-UIAEvent -All
Advertisements

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: