自动化测试 using System.Windows.Automation;
frameworke3.0 及以上
using System.Windows.Automation;
UIAutomationClient.dll
UIAutomationClientsideProviders.dll C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\UIAutomationClientsideProviders.dll
UIAutomationProvider.dll
UIAutomationTypes.dll
下面的示例演示如何使用 FindAll 查找窗口中的所有已启用的按钮。
/// <summary>
/// Finds all enabled buttons in the specified window element.
/// </summary>
/// <param name="elementWindowElement">An application or dialog window.</param>
/// <returns>A collection of elements that meet the conditions.</returns>
AutomationElementCollection FindByMultipleConditions(AutomationElement elementWindowElement)
{
if (elementWindowElement == null)
{
throw new ArgumentException();
}
Condition conditions = new AndCondition(
new PropertyCondition(AutomationElement.IsEnabledProperty, true),
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button)
); // Find all children that match the specified conditions.
AutomationElementCollection elementCollection = elementWindowElement.FindAll(TreeScope.Children, conditions);
return elementCollection;
}
本主题描述获取 用户界面 (UI) 元素的 AutomationElement 对象的各种方法。
|
如果客户端应用程序可以尝试在其自己的用户界面中查找元素,则必须在一个单独的线程上进行所有 UI 自动化调用。有关更多信息,请参见 UI 自动化线程处理问题。 |
本主题包括下列各节。
根元素
所有 AutomationElement 对象搜索必须具有一个起点。此起点可以是任何元素,包括桌面、应用程序窗口或控件。
所有元素所起源于的根元素是从静态的 AutomationElement..::.RootElement 属性中获得的。
|
通常,您应当尝试只获取 RootElement 的直接子项。对子代的搜索可能循环访问数百个甚至数千个元素,从而可能导致堆栈溢出。如果尝试在较低级别获取特定元素,您应当从应用程序窗口或位于较低级别的容器中开始搜索。 |
条件
对于用于检索 UI 自动化元素的大多数方法,您必须指定 Condition,这是一组用于定义要检索的元素的条件。
最简单的条件是 TrueCondition,这是一个指定要返回搜索范围内的所有元素的预定义对象。FalseCondition 是 TrueCondition 的对立条件,其作用不大,因为它将阻止找到任何元素。
下面是三个其他的预定义条件,这些条件既可以单独使用,也可以与其他条件一起使用:ContentViewCondition、ControlViewCondition 和 RawViewCondition。单独使用的 RawViewCondition 等效于 TrueCondition,因为它不根据元素的 IsControlElement 或 IsContentElement 属性来筛选元素。
其他条件是根据一个或多个 PropertyCondition 对象(每个对象都指定一个属性值)建立的。例如,PropertyCondition 可以指定元素处于启用状态或元素支持某种控件模式。
通过构建 AndCondition、OrCondition 和 NotCondition 类型的对象,可以用布尔逻辑将条件结合起来。
搜索范围
用 FindFirst 或 FindAll 执行的搜索必须具有一个范围和一个起点。
范围定义了要搜索的起点周围的空间。这可以包括元素本身、其同级项、父项、祖先、直接子项以及子代。
搜索范围由按位组合的 TreeScope 枚举值来定义。
查找已知元素
若要查找由已知元素的 Name、AutomationId、某些其他属性或属性组合标识的已知元素,最简单的方法是使用 FindFirst 方法。如果查找的元素是一个应用程序窗口,则搜索的起点可以是 RootElement。
这种查找 UI 自动化 元素的方法在自动化测试方案中最有用。
查找子树中的元素
若要查找与已知元素有关且满足特定条件的所有元素,您可以使用 FindAll。例如,您可以使用这种方法从列表或菜单中检索列表项或菜单项,或找出对话框中的所有控件。
浏览子树
如果您事先不了解可与客户端一起使用的应用程序,则可以使用 TreeWalker 类建立一个包含感兴趣的所有元素的子树。您的应用程序可能会执行此操作以响应 focus-changed 事件;也就是说,在应用程序或控件接收输入焦点时,UI 自动化客户端将检查子项,可能还会检查有焦点的元素的所有子代。
使用 TreeWalker 的另一种方式是识别元素的祖先。例如,通过向上浏览树,您可以识别控件的父窗口。
通过创建类的对象(用 Condition 定义感兴趣的元素),或者使用以下被定义为 TreeWalker 的字段的预定义对象之一,您可以使用 TreeWalker。
|
ContentViewWalker |
只查找 IsContentElement 属性为 true 的元素。 |
|
ControlViewWalker |
只查找 IsControlElement 属性为 true 的元素。 |
|
RawViewWalker |
查找所有元素。 |
在获得了 TreeWalker 之后,可以直接使用它。只需调用 Get 方法便可在子树的元素中导航。
Normalize 方法可用于从视图外的另一元素导航到子树中的某个元素。例如,假设您使用 ContentViewWalker 创建了一个子树视图。然后,您的应用程序收到通知,得知滚动条已经接收了输入焦点。因为滚动条不是内容元素,所以子树视图中未呈现滚动条。但是,您可以将代表滚动条的 AutomationElement 传递到 Normalize 并检索内容视图中最接近的祖先。
检索元素的其他方法
除了应用搜索和导航之外,您还可以通过以下方法来检索 AutomationElement。
从事件中
当您的应用程序接收 UI 自动化事件时,传递到事件处理程序的源对象为 AutomationElement。例如,如果您订阅了 focus-changed 事件,则传递到 AutomationFocusChangedEventHandler 的源是收到焦点的元素。
有关更多信息,请参见订阅 UI 自动化事件。
从某个点中
如果您具有屏幕坐标(例如,光标位置),则可以使用静态的 FromPoint 方法来检索 AutomationElement。
从窗口句柄中
若要从 HWND 中检索 AutomationElement,请使用静态的 FromHandle 方法。
从具有焦点的控件中
可以从静态的 FocusedElement 属性中检索表示焦点控件的 AutomationElement。
请参见
概念
表示一个 Condition,其计算结果总是为 true。
命名空间: System.Windows.Automation
程序集: UIAutomationClient(在 UIAutomationClient.dll 中)
语法
| Visual Basic(声明) |
|---|
Public Shared ReadOnly TrueCondition As Condition |
| Visual Basic(用法) |
|---|
Dim value As Condition value = Condition.TrueCondition |
| C# |
|---|
public static readonly Condition TrueCondition |
| Visual C++ |
|---|
public: |
| J# |
|---|
public static final Condition TrueCondition |
| JScript |
|---|
public static final var TrueCondition : Condition |
字段值
示例
在下面的示例中,使用 TrueCondition 检索在指定范围内的全部 UI 自动化元素。
| C# | |
|---|---|
/// <summary> |
|
| Visual Basic | |
|---|---|
''' <summary> |
|
权限
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。
平台
Windows Vista
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
版本信息
.NET Framework
受以下版本支持:3.5、3.0 SP1、3.0
请参见
自动化测试 using System.Windows.Automation;的更多相关文章
- Windows Automation API和自动化测试
https://zhuanlan.zhihu.com/p/22083601\ 感谢轮子哥点赞,这会儿消息扎堆过来了,轮带逛果然不是随便说说的…… 第二篇一个简单的Windows Automation ...
- Windows Automation API 3.0 Overview
https://www.codemag.com/article/0810042 While general accessibility requirements (such as font color ...
- System.Windows.Application.Current.Dispatcher.BeginInvoke
System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => ...
- 开发客户端软件时,出现System.Windows.Markup.XamlParseException错误
开发客户端软件时,出现System.Windows.Markup.XamlParseException错误,通过查看错误消息,发现TCPIP的一个COM组件在安装软件过程中被删除了,重新注册了一下TC ...
- WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常
WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常 在wpf中封装Com组件时,调用组件 ...
- 类型“System.Windows.Markup.IUriContext”在未被引用的程序集中定义 解决办法
错误 CS0012: 类型“System.Windows.Markup.IUriContext”在未被引用的程序集中定义.必须添加对程序集“System.Xaml, Version=4.0.0.0, ...
- 类型“System.Windows.Markup.IQueryAmbient”在未被引用的程序集中定义
错误 1 类型"System.Windows.Markup.IQueryAmbient"在未被引用的程序集中定义.必须添加对程序集"System.Xaml, ...
- System.Windows.Markup.IQueryAmbient 在未被应用的程序集中定义
按照<WIndows Presentation Foundation>中介绍建立的WPF程序,可以在VS2008中创建控制台应用程序所得.创建之后将程序集输出类型改为:Windows应用程 ...
- System.Windows.Media.Imageing.BItmapImage 这么用才不会占用文件
// Read byte[] from png file BinaryReader binReader = new BinaryReader(File.Open(filepath, FileMode. ...
随机推荐
- ES6笔记一
遍历数组: 1:传统的 for (var index = 0; index < myArray.length; index++) { console.log(myArray[index]);} ...
- Interpolation in MATLAB
Mathematics One-Dimensional Interpolation There are two kinds of one-dimensional interpolation i ...
- iOS、swift、React Native学习常用的社区、论坛
<!----iOS> <!----Swift>*IOS开发常用社区:http://code4app.com/ *IOS开发常用社区:http://www.cocoachina. ...
- Codeforces Round #361 Jul.6th B题 ☺译
最近迈克忙着考前复习,他希望通过出门浮躁来冷静一下.迈克所在的城市包含N个可以浮躁的地方,分别编号为1..N.通常迈克在家也很浮躁,所以说他家属于可以浮躁的地方并且编号为1.迈克从家出发,去一些可以浮 ...
- glusterfs 内存管理方式
glusterfs中的内存管理方式: 首先来看看glusterfs的内存管理结构吧: struct mem_pool { struct list_head list; int hot_count; i ...
- [课程设计]Scrum 1.1 NABCD模型&产品Backlog
多鱼点餐系统WEB NABCD模型 & 产品Backlog ● 一.NABCD模型 1) N (Need 需求) 为了解决餐饮企业在同行中的竞争优势,减少顾客到店后的点餐.等餐及结算过程消耗 ...
- ExtJs 学习之开篇(三)Ext.grid.Panel表格中的处理
Ext.grid.Panel Ext.create('Ext.grid.Panel',{ title:'测试表格', width:400, height:20 ...
- C#四种深拷贝方法
//四种深拷贝方法 public static T DeepCopyByReflect<T>(T obj) { //如果是字符串或值类型则直接返回 if (obj is string || ...
- HTTP head 详解 (转)
HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP协议的详细内 容请参考RFC2616.HTTP协议采用了请求/响应模型.客 ...
- Nuke
- Debugging python code IN nuke with Eclipse - Documents: http://www.thefoundry.co.uk/products/nuke- ...