赞
踩
1、简介
UIAutomation微软提供的UI自动化库,主要用AutomationElement类来表示UI 自动化目录树中的一个UI自动化元素,.NET Windows的窗体应用程序和WPF应用程序
2、体系
在服务端由UIAutomationProvider.dll和UIAutomationTypes.dll提供
在客户端由UIAutomationClient.dll和UIAutomationTypes.dll提供
UIAutomationCore.dll为UI自动化的核心部分,负责Server端和Client端的交互
UIAUtomationClientSideProvides.dll为客户端程序提供自动化支持
3、UI自动化流程
项目中一定要添加引用:
使用UISpy查看控件属性值
Process p = Process.Start(@"C:\Windows\system32\calc.exe");//启动应用程序
Thread.Sleep(2000);
AutomationElement desktop = AutomationElement.RootElement;//获取RootElement
AutomationElement calcframe = desktop.FindFirst(TreeScope.Descendants | TreeScope.Children,
new PropertyCondition(AutomationElement.NameProperty, "计算器"));//获取应用程序
AutomationElement sevenbtn = calcframe.FindFirst(TreeScope.Descendants | TreeScope.Children,
new PropertyCondition(AutomationElement.NameProperty, "7"));//获取控件
InvokePattern ivkp = (InvokePattern)sevenbtn.GetCurrentPattern(InvokePattern.Pattern);
ivkp.Invoke(); //触发控件事件1
2
3
4
5
6
7
8
9
10
11
12
13
执行效果:
注:PropertyCondition类是用来对相关属性进行条件匹配,在控件树中查找控件时,可以通过最佳匹配来找到相应的控件
4、UIAutomation重要属性
AutomationIdProperty:通过AutomationId来查找AutomationElement
NameProperty:通过控件的Name属性来查找AutomationElement
ControlType:通过控件的类型来查找AutomationElement
AutomationId: 唯一地标识自动化元素,将其与同级相区分
Name: WPF 按钮的Content 属性、Win32 按钮的Caption 属性以及 HTML 图像的ALT 属性都映射到 UI 自动化视图中的同一个属性Name
5、TreeScope
Element:指定搜索包括元素本身。
Children:指定搜索包括元素的直接子级。
Descendants:指定搜索包括元素的子代(包括子级)。
Parent:指定搜索包括元素的父级。不支持。
Ancestors:指定搜索包括元素的上级
Subtree:指定搜索包括搜索的根和全部子代。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。