UIAutomation.0.8.7B3.samples uia powershell 插件例子解析 1
uiautomation powershell 插件例子解析
作者给出了示例,不过在中文版Windows上需要略微修改下。因为中文版的进程名名字跟程序名字可能不一样。作者给出里例子是按首字母排序的 不太符合从基础到复杂的学习过程。但是可以从时间排序看出作者的编写思路。
0。导入库
导入这两个,TMX是用来做测试的。路径按自己实际情况改
ipmo D:\UIAutomation.0.8.7B3.NET40\UIAutomation.dll
ipmo D:\UIAutomation.0.8.7B3.NET40\TMX.dll
1 Caching in UI Automation Clients
这个我没看懂,大概是说这样运行会更快 。命令
Start-UiaCachedMode -Property Name,AutomationId,ClassName,ControlType,Value -Pattern InvokePattern,ValuePattern -Scope Descendants -Filter Raw -Verbose
2。点击
2. 1 简单按钮点击。打开计算器并点击按钮1。因为计算器的进程名在中文版里就是 计算器 而不是calc ,所以需要修改下源程序
# ipmo UIAutomation; # variant 1 Start-Process calc
Get-UIAWindow -n 计算器 | Get-UIAButton -Name 一 | Invoke-UIAButtonClick; # variant 2 $button1 = Start-Process calc -PassThru | Get-UIAWindow -n 计算器 | Get-UIAButton -Name 一; $button1 | Invoke-UIAButtonClick; # variant 3 $button1 = Start-Process calc -PassThru | Get-UIAWindow -n 计算器 | Get-UIAButton -Name 一; Invoke-UIAButtonClick -InputObject $button1; # variant 4 Invoke-UIAButtonClick -InputObject (Start-Process calc -PassThru | Get-UIAWindow -n 计算器 | Get-UIAButton -Name 一);
2.2 右击和双击
services.msc #run powershell as administrator 用管理员权限启动posh # open the Connect... window via right click on the tree item 右击树项 打开 连接到 窗口 #Get-UIAWindow -n services | Get-UIATreeItem | Invoke-UIAControlContextMenu | Get-UIAMenuItem -n connect* | Invoke-UIAMenuItemClick; #Get-UIAWindow -pn mmc -n *select*computer* | Get-UIAButton Cancel | Invoke-UIAButtonClick; Get-UIAWindow -n 服务 | Get-UIATreeItem | Invoke-UIAControlContextMenu | Get-UIAMenuItem -n 连接* | Invoke-UIAMenuItemClick; Get-UIAWindow -n 选择计算机 | Get-UIAButton 取消 | Invoke-UIAButtonClick; # double click on a service in the grid: 在网格上双击 #Get-UIAWindow -n services | Get-UIADataGrid | Get-UIADataItem BranchCache | Invoke-UIAControlClick -DoubleClick; Get-UIAWindow -n 服务 | Get-UIADataGrid | Get-UIADataItem BranchCache | Invoke-UIAControlClick -DoubleClick;
2.3 在不同的实例调用点击
ipmo C:\Projects\PS\STUPS\UIA\UIAutomation\bin\Release35\UIAutomation.dll # getting button 1 from the first instance 在第一一个实例中获得按键1 $button1inWindow1 = Start-Process calc -PassThru | Get-UIAWindow | Get-UIAButton 1; # getting button 2 from the second instance 在第二个实例中获得按键2 $button2inWindow2 = Start-Process calc -PassThru | Get-UIAWindow | Get-UIAButton 2; # clicking buttons from both instances 调用 $button1inWindow1 | Invoke-UIAButtonClick; $button2inWindow2 | Invoke-UIAButtonClick;
2.4 在模态窗口下点击 在非模态窗中,用户可以自由的浏览、操作、返回或是通过导航跳转;而在模态窗中,你需要先完成操作,才可以返回状态。最新的计算器好像没有帮助菜单了 ,仅供参考
ipmo C:\Projects\PS\STUPS\UIA\UIAutomation\bin\Release35\UIAutomation.dll # getting button 1 #$button1inWindow1 = Start-Process calc -PassThru | Get-UIAWindow | Get-UIAButton 1; calc $button1inWindow1 = Get-UIAWindow | Get-UIAButton 一; # opening the About window #Get-UIAMenuItem -Name help | Invoke-UIAMenuItemExpand | Get-UIAMenuItem -Name *about* | Invoke-UIAMenuItemClick; Get-UIAMenuItem -Name 帮助 | Invoke-UIAMenuItemExpand | Get-UIAMenuItem -Name *关于* | Invoke-UIAMenuItemClick; # clicking the button that is under a modal window $button1inWindow1 | Invoke-UIAButtonClick;
3.控件
3.1 通过正则表达式获取控件 中文操作系统请自己修改控件名
ipmo C:\Projects\PS\STUPS\UIA\UIAutomation\bin\Release35\UIAutomation.dll Start-Process calc -PassThru | Get-UiaWindow | Get-UiaButton 5 | Invoke-UiaButtonClick; Start-Process calc -PassThru | Get-UiaWindow | Get-UiaButton -n 5 | Invoke-UiaButtonClick; Start-Process calc -PassThru | Get-UiaWindow | Get-UiaButton -a 135 | Invoke-UiaButtonClick; Start-Process calc -PassThru | Get-UiaWindow | Get-UiaButton -n 5 -Win32 | Invoke-UiaButtonClick; Start-Process calc -PassThru | Get-UiaWindow | Get-UiaButton -n 5 -Regex | Invoke-UiaButtonClick; Start-Process calc -PassThru | Get-UiaWindow | Get-UiaButton -n [5] -Regex | Invoke-UiaButtonClick; Start-Process calc -PassThru | Get-UiaWindow | Get-UiaButton -n [\d] -Regex | Invoke-UiaButtonClick; Start-Process calc -PassThru | Get-UiaWindow | Get-UiaButton -n [\w] -Regex | Invoke-UiaButtonClick; Start-Process calc -PassThru | Get-UiaWindow | Get-UiaButton -n "store|equal|entr" -Regex | Invoke-UiaButtonClick; Start-Process calc -PassThru | Get-UiaWindow | Get-UiaButton -n "store|equal|entr|spac" -Regex | Invoke-UiaButtonClick;
3.2 通过名字获取控件 我没发现win10自带计算器还有计算抵押的功能。。。。
# Arrange: # calc.exe # the Standard mode # Worksheets -> Mortgage # Purchase price == "22" # this code returns three controls in versions until 0.8.7 Aplha 2 # in contemporary versions, this code returns only one control ("Purchase price") Get-UiaWindow -n *calc* | Get-UiaEdit -n "(purchase\sprice|down\spayment|term\s\(years\))" -Regex -Value 22
3.3 拖动 打开我的电脑,拖动左边栏里文件夹位置
ipmo C:\Projects\PS\STUPS\UIA\UIAutomation\bin\Release35\UIAutomation.dll #Get-UiaWindow -Name *etwr*dit* Get-UiaWindow -Name 此电脑 #$treeItem01 = Get-UiaTreeItem -Name *comp*coll* $treeItem01 = Get-UiaTreeItem -Name 桌面 #$treeItem02 = Get-UiaTreeItem -Name *folder* $treeItem02 = Get-UiaTreeItem -Name 文档 $treeItem01 | Invoke-UiaControlClick; $treeItem01.Mouse.LeftButtonDown(); $treeItem01 | Move-UiaCursor -X 5 -Y 5; $treeItem02 | Move-UiaCursor -X 5 -Y 5; $treeItem02.SetFocus(); sleep -Seconds 2; $treeItem02.Mouse.LeftButtonUp();
重要修正 哈哈我终于发现为啥计算器的例子都用不成了,因为win10的计算器变量 经典的计算器运行这些脚本没有问题。可以参考安装下经典计算器 (85条消息) 在WIN10中安装经典计算器_hdzihang的博客-CSDN博客_win10 计算器 安装
4.控件状态
4。1 搜索标准
Start-Process win32calc -PassThru | Get-UIAWindow | Get-UIAMenuItem -Name 查* | ConvertTo-UIASearchCriteria Start-Process win32calc -PassThru | Get-UIAWindow | Get-UIAMenuItem -Name 查* | ConvertTo-UIASearchCriteria -Full Start-Process win32calc -PassThru | Get-UIAWindow | Get-UIAMenuItem -Name 查* | ConvertTo-UIASearchCriteria -Include controltype,class Start-Process win32calc -PassThru | Get-UIAWindow | Get-UIAMenuItem -Name 查* | ConvertTo-UIASearchCriteria -exclude controltype,class
结果
@{Name="查看(V)";AutomationId="Item 1";ControlType="MenuItem";Class="";AcceleratorKey="";AccessKey="Alt+V";BoundingRectangle="317,306,54,19";FrameworkId="Win32";HasKeyboardFocus=$false;HelpText="";IsContentElement=$true;IsControlElement=$true;IsEnabled=$true;IsKeyboardFocusable=$true;IsOffscreen=$false;IsPassword=$false;IsRequiredForForm=$false;ItemStatus="";ItemType="";LocalizedControlType="菜单项目";NativeWindowHandle="0";Orientation="None";ProcessId="31860";HasaExpandCollapsePattern=$true}
4.2 高光标出被选择的控件 注意把calc改成win32calc
Start-Process calc -PassThru | Get-UIAWindow | Test-UIAControlState -SearchCriteria @{controlType="button";name="3"} Stop-Process -Name calc Start-Process calc -PassThru | Get-UIAWindow | Get-UIAMenuItem Help | Invoke-UIAMenuItemExpand | Get-UIAMenuItem -Name *about* | Invoke-UIAMenuItemClick; Get-UIAWindow -pn calc -WithControl @{controlType="button";name="9"} Get-UIAWindow -pn calc -WithControl @{controlType="button";name="OK"} Stop-Process -Name calc
4.3 Test-UIAControlState 注意把calc改成win32calc
Write-Host "should be found:"; Start-Process calc -PassThru | Get-UIAWindow | Test-UIAControlState -SearchCriteria @{controlType="button";name="1"}; Stop-Process -Name calc Write-Host "should be found:"; Start-Process calc -PassThru | Get-UIAWindow | Test-UIAControlState -SearchCriteria @{controlType="button";name="1"},@{controlType="button";name="2"},@{controlType="button";name="3"}; Stop-Process -Name calc Write-Host "could not be found:"; Start-Process calc -PassThru | Get-UIAWindow | Test-UIAControlState -SearchCriteria @{controlType="button";name="1"},@{controlType="button";name="2"},@{controlType="button";name="3"},@{controlType="button";name="10"}; # -Verbose; Stop-Process -Name calc
4.4 ConvertTo-UiaSearchCriteria 把控件对象转成搜索条件 注意把calc改成win32calc
ipmo C:\Projects\PS\STUPS\UIA\UIAutomation\bin\Release35\UIAutomation.dll $wnd = Start-Process calc -PassThru | Get-UiaWindow # default (included name, automationId, controlType; excluded labeledBy, processId, nativeWindowHandle) $wnd | ConvertTo-UiaSearchCriteria # the full set of properties $wnd | ConvertTo-UiaSearchCriteria -Full $wnd | ConvertTo-UiaSearchCriteria -Include name,classname,controltype,nativewindowhandle $wnd | ConvertTo-UiaSearchCriteria -Include name,classname,controltype,nativewindowhandle,processid $wnd | ConvertTo-UiaSearchCriteria -Include name,classname,controltype,nativewindowhandle,processid,nativewindowhandle $wnd | ConvertTo-UiaSearchCriteria -Include name,classname,controltype,nativewindowhandle,processid,nativewindowhandle -Exclude automationid
运行结果
@{Name="计算器";AutomationId="";ControlType="Window";}
@{Name="计算器";AutomationId="";ControlType="Window";Class="CalcFrame";AcceleratorKey="";AccessKey="";BoundingRectangle="313,286,228,323";FrameworkId="Win32";HasKeyboardFocus=$false;HelpText="";IsContentElement=$true;IsControlElement=$true;IsEnabled=$true;IsKeyboardFocusable=$false;IsOffscreen=$false;IsPassword=$false;IsRequiredForForm=$false;ItemStatus="";ItemType="";LocalizedControlType="窗口";NativeWindowHandle="33295086";Orientation="None";ProcessId="34344";HasaWindowPattern=$true;HasaTransformPattern=$true}
4.5
Test-UiaControlState -SearchCriteria @{controltype="button";name="3"} 测试是否可以用所给的条件搜索到控件
4.6 Wait-UIAControlState 等待控件出现,参数是搜索条件,这个可以见加一个时间参数,多长时间没有出现控件返回false
5 事件 可以在控件的某种事件发生后,让跟事件注册的代码执行
5.1 属性改变事件 没看懂
Set-StrictMode -Version Latest; ipmo $global:uiautomationModule; ipmo $global:tmxModule; Start-Process win32calc -PassThru | ` Get-UIAWindow | ` Get-UIAControlDescendants -ControlType Text | ` %{ $_ | Register-UIAPropertyChangedEvent ` -EventAction ` { # write to a file param($src, $e) # report everything "sdfsadf" >> "$env:Temp\sample_value_changed_report.txt"; "'$($src.Current.Name)' has its value changed" >> "$env:Temp\sample_value_changed_report.txt"; if ($src.Current.Name.Length -eq 0) { "===================================" >> "$env:Temp\sample_value_changed_report.txt"; "Oh, this is what we are waiting for!" >> "$env:Temp\sample_value_changed_report.txt"; "AutomaitonId = $($src.Current.AutomaitonId)" >> "$env:Temp\sample_value_changed_report.txt"; "ControlType = $($src.Current.ControlType.ProgrammaticName)" >> "$env:Temp\sample_value_changed_report.txt"; "ClassName = $($src.Current.ClassName)" >> "$env:Temp\sample_value_changed_report.txt"; "-----------------------------------" >> "$env:Temp\sample_value_changed_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.Text') { [System.Windows.Forms.MessageBox]::Show($src.Current.Name + "`t" + ` $src.Current.AutomationId + "`r`n" + ` $e + "`r`n" + ` $src.Current.ControlType.ProgrammaticName); # } }; }
5.2 菜单开关事件 给菜单开和关的事件注册一个动作,在菜单关或者开的时候弹出对话窗 注意这个[UIAutomation.CurrentData]::CurrentWindow,可以不用变量名多次使用选出来的控件
[string]$appName = "win32calc"; Start-Process $appName -PassThru | ` Get-UIAWindow | ` Register-UIAMenuClosedEvent ` -EventAction { param($src, $e) [System.Windows.Forms.MessageBox]::Show( ` "calc menu closed: Current:" + ` $src.Current.Name + " " + ` $src.Current.AutomationId + " Cached:" + ` $src.Cached.Name + " " + ` $src.Cached.AutomationId + " " + ` $e.EventId);}; [UIAutomation.CurrentData]::CurrentWindow | ` Register-UIAMenuOpenedEvent ` -EventAction { param($src, $e) [System.Windows.Forms.MessageBox]::Show( ` "calc menu opened: Current:" + ` $src.Current.Name + " " + ` $src.Current.AutomationId + " Cached:" + ` $src.Cached.Name + " " + ` $src.Cached.AutomationId + " " + ` $e.EventId);}
5.3 菜单打开事件 打开菜单弹出对话框
Start-Process win32calc -PassThru | ` Get-UIAWindow | ` Register-UIAMenuOpenedEvent ` -EventAction {[System.Windows.Forms.MessageBox]::Show("menu opened");}; Get-UIAMenuItem -Name 查* | Invoke-UIAMenuItemExpand;
5.4 窗口打开事件 未测试,要试的话自己改中文控件名
[string]$appName = "services.msc"; # you need to run a PowerShell console as Administrator Start-Process $appName -PassThru | ` Get-UIAWindow | ` Register-UIAWindowOpenedEvent -EventAction {param($src, $e) [System.Windows.Forms.MessageBox]::Show("A new window is open: $($src.Cached.Name)");}; Get-UIAMenuItem -Name File | ` Invoke-UIAMenuItemClick | ` Get-UIAMenuItem -Name Opt* | ` Invoke-UIAMenuItemClick;
5.5 调用事件
[int]$global:clickCount = 0; [string]$global:lastClickedButton; Start-Process calc -PassThru | Get-UIAWindow | Get-UIAButton -n [1-9] | Register-UIAInvokedEvent -EventAction { $global:clickCount++; },{ param($src, $e) $global:lastClickedButton = $src.Current.Name; }; sleep -Seconds 1; Get-UIAButton -Name [1-9] | Invoke-UIAButtonClick; sleep -Seconds 1; $global:clickCount $global:lastClickedButton Get-UIAControlDescendants # use the Get-UIAControlDescendants cmdlet to collect all the visible buttons 使用Get-UIAControlDescendants获取所有可见控件 # use the Register-UIAInvokedEvent cmdlet to assign an event action to each button 会使用Register-UIAInvokedEvent注册事件,发生事件就会让事件注册的动作发生 Start-Process calc -PassThru | ` Get-UIAWindow | ` Get-UIAControlDescendants -ControlType Button | ` Register-UIAInvokedEvent -EventAction {param($src) "$($src.Current.Name) clicked" >> $env:TEMP\calc.log;}
6执行计划
Show-UIAExecutionPla 这个命令遍历所有可视控件 -max参数 可以限制最多次数
遍历计算器所有按钮跟菜单
Show-UIAExecutionPlan -Max 100; Start-Process calc -PassThru | Get-UIAWindow | Get-UIAButton; Get-UIAMenuItem;
7 日志
7.1 工厂方法自动生成日志
[UIAutomation.Preferences]::AutoLog = $true Start-Process calc -PassThru | Get-UIAWindow | Get-UIAButton -n [1-3] | Invoke-UIAButtonClick;
Measure-Command -Expression { $wnd = Start-Process calc -PassThru | Get-UiaWindow } $Logger = [UIAutomation.AutomationFactory]::GetLogger("c:\1\20140213_0001.txt"); Measure-Command -Expression { Get-UiaButton 1 } Measure-Command -Expression { Get-UiaButton -SearchCriteria @{controlType="button";automationId="13*"} }
[UIAutomation.AutomationFactory]::GetLogger("c:\1\new_log.txt") $btn1 = Start-Process calc -PassThru | Get-UiaWindow | Get-UiaButton 1 | Invoke-UiaButtonClick | Invoke-UiaButtonClick; $btn1.Control.Click(); $btn1.Invoke(); $btn1.Keyboard.TypeText("333"); Get-UiaButton -n 10; Get-UiaButton -n 2;
8.例子 打开控制面板并搜索
control
Get-UIAWindow -Name '控制面板' | Get-UIAEdit -AutomationId 'SearchEditBox' | Set-UIAEditText -Text '声音'