Daily automation: recording user’s actions


Applications are not stones. They’re changing during the user’s session. About which sort of changes am I talking? There might be several sorts of them:

  • the application structure changed (a new control appeared or an existing’s gone)
  • a control’s property changed
  • an event invoked (user clicked a control, for the easiest example)
  • a new child window appeared
  • the main window is closed

Today we discuss the case  of a control’s adding occurrence.

Task: demonstrate how to initiate the structure changed via an application’s menu item and how the change can be recorded.

Requirements: use the UIAutomation to use an event handler.

Solution: we are going to write events we collected to a log file and, for our information, to a message box:

Set-StrictMode -Version Latest;
ipmo [path]\UIAutomation.dll;</pre>
# start the process and atach event handlers to the window
Start-Process notepad -PassThru | `
 Get-UIAWindow | `
 Register-UIAStructureChangedEvent -ChildAdded `
 -EventAction `
 { # write to a file
 param($src, $e)
 # report everything
 "'$($src.Current.Name)' has gotten a child" >> "$env:Temp\sample_report.txt";
 if ($src.Current.Name.Length -eq 0) {
 "===================================" >> "$env:Temp\sample_report.txt";
 "Oh, this is what we are waiting for!" >> "$env:Temp\sample_report.txt";
 "AutomaitonId = $($src.Current.AutomaitonId)" >> "$env:Temp\sample_report.txt";
 "ControlType = $($src.Current.ControlType.ProgrammaticName)" >> "$env:Temp\sample_report.txt";
 "ClassName = $($src.Current.ClassName)" >> "$env:Temp\sample_report.txt";
 "-----------------------------------" >> "$env:Temp\sample_report.txt";
 }
 },
 { # display a message box
 param($src, $e)
 # report only what happened under the menu item hierarchy
 if ($src.Current.ControlType.ProgrammaticName -eq 'ControlType.StatusBar') {
 [System.Windows.Forms.MessageBox]::Show($src.Current.Name + "`t" + `
 $src.Current.AutomationId + "`r`n" + `
 $e + "`r`n" + `
 $src.Current.ControlType.ProgrammaticName);
 }
 };

# depending on was or wasn't the Status Bar shown on
# the status bar will or won't be shown
# and the event handlers we linked to the window will fire
Get-UIAMenuItem -Name View | Invoke-UIAMenuItemExpand | Get-UIAMenuItem -Name 'Status Bar' | Invoke-UIAMenuItemClick;
Get-UIAMenuItem -Name View | Invoke-UIAMenuItemExpand | Get-UIAMenuItem -Name 'Status Bar' | Invoke-UIAMenuItemClick;
Get-UIAMenuItem -Name View | Invoke-UIAMenuItemExpand | Get-UIAMenuItem -Name 'Status Bar' | Invoke-UIAMenuItemClick;
Get-UIAMenuItem -Name View | Invoke-UIAMenuItemExpand | Get-UIAMenuItem -Name 'Status Bar' | Invoke-UIAMenuItemClick;
<pre>

After the code is processed by PowerShell, the log file in our user’s TEMP folder contains the information we’ve collected.

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: