自动化测试 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. ...
随机推荐
- PHP mysql_fetch_array() 函数
PHP mysql_fetch_array() 函数 从结果集中取得一行作为关联数组,或数字数组,或二者兼有.返回根据结果集取得的行生成的数组,如果没有更多行则返回false. 提示:有很重要的一点必 ...
- webpack使用tree shaking的问题。及关于UglifyJs不支持ES6的解决方案。
webpack: plugins:[ new webpack.optimize.UglifyJsPlugin({ compress:{warning:true} }) ] 是的,一些dead code ...
- 初学JAVA 感想
开始学习任何一门课(包括java),兴趣最重要.一直觉得自己在学计算机编程语言,学习了很多,但每门语言都停留在知识边缘地带,都没深入到它们的精华部分,对它们的精华部分知之甚少,于是趁学校开设这门课,并 ...
- 2016 、12 、11<本周>
翻了翻记录 想把上周没搞出来的1159和day2T2搞出来.
- AFNetworking3.0 Https P12证书
最近服务器由原来的ice中间件改为https.方便了和服务器交互时不用自己aes加密了. -之前服务器人员和我(IOS)都没有使用过https,所以https跑不通很难说是服务器没有配置好还是IOS这 ...
- X86 Socket 通信
struct txd_socket_handler_t { int fd; }; txd_socket_handler_t *txd_tcp_socket_create() { txd_socket_ ...
- Python 常用函数
字符串->数字: float(str) int(str) 十六进制字符串转int: 不带0x前缀: x = int('dead',16) 带0x前缀: x = int('0xff', 0) 其中 ...
- C#中dynamic的正确用法
C#中dynamic的正确用法 http://www.cnblogs.com/qiuweiguo/archive/2011/08/03/2125982.html dynamic是FrameWork4 ...
- oracle 职业学习指引
风哥 它是阿里巴巴造出的概念.其本意是,在阿里巴巴的IT架构中,去掉IBM的小型机.Oracle数据库.EMC存储设备,代之以自己在开源软件基础上开发的系统. 思科.IBM.谷歌.高通.英特尔.苹果. ...
- 《winform窗体应用程序》----------简易记事本
首先先给大家发表几张图片,描述一下记事本程序要实现的功能以及界面设计. 以上这些就是简易记事本的的主界面设计. 下面我们来做一些简单的讲解: 1.使用MenuStrip控件来实现菜单栏的基本设计. 在 ...