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:
static initonly Condition^ TrueCondition
J#
public static final Condition TrueCondition
JScript
public static final var TrueCondition : Condition

字段值

类型:System.Windows.Automation..::.Condition

示例

在下面的示例中,使用 TrueCondition 检索在指定范围内的全部 UI 自动化元素。

C#  复制代码
/// <summary>
/// Examples of using predefined conditions to find elements.
/// </summary>
/// <param name="elementMainWindow">The element for the target window.</param>
public void StaticConditionExamples(AutomationElement elementMainWindow)
{
if (elementMainWindow == null)
{
throw new ArgumentException();
} // Use TrueCondition to retrieve all elements.
AutomationElementCollection elementCollectionAll = elementMainWindow.FindAll(
TreeScope.Subtree, Condition.TrueCondition);
Console.WriteLine("\nAll control types:");
foreach (AutomationElement autoElement in elementCollectionAll)
{
Console.WriteLine(autoElement.Current.Name);
} // Use ContentViewCondition to retrieve all content elements.
AutomationElementCollection elementCollectionContent = elementMainWindow.FindAll(
TreeScope.Subtree, Automation.ContentViewCondition);
Console.WriteLine("\nAll content elements:");
foreach (AutomationElement autoElement in elementCollectionContent)
{
Console.WriteLine(autoElement.Current.Name);
} // Use ControlViewCondition to retrieve all control elements.
AutomationElementCollection elementCollectionControl = elementMainWindow.FindAll(
TreeScope.Subtree, Automation.ControlViewCondition);
Console.WriteLine("\nAll control elements:");
foreach (AutomationElement autoElement in elementCollectionControl)
{
Console.WriteLine(autoElement.Current.Name);
}
}
Visual Basic  复制代码
''' <summary>
''' Examples of using predefined conditions to find elements.
''' </summary>
''' <param name="elementMainWindow">The element for the target window.</param>
Public Sub StaticConditionExamples(ByVal elementMainWindow As AutomationElement)
If elementMainWindow Is Nothing Then
Throw New ArgumentException()
End If ' Use TrueCondition to retrieve all elements.
Dim elementCollectionAll As AutomationElementCollection = elementMainWindow.FindAll(TreeScope.Subtree, Condition.TrueCondition)
Console.WriteLine(vbLf + "All control types:")
Dim autoElement As AutomationElement
For Each autoElement In elementCollectionAll
Console.WriteLine(autoElement.Current.Name)
Next autoElement ' Use ContentViewCondition to retrieve all content elements.
Dim elementCollectionContent As AutomationElementCollection = elementMainWindow.FindAll(TreeScope.Subtree, Automation.ContentViewCondition)
Console.WriteLine(vbLf + "All content elements:")
For Each autoElement In elementCollectionContent
Console.WriteLine(autoElement.Current.Name)
Next autoElement ' Use ControlViewCondition to retrieve all control elements.
Dim elementCollectionControl As AutomationElementCollection = elementMainWindow.FindAll(TreeScope.Subtree, Automation.ControlViewCondition)
Console.WriteLine(vbLf & "All control elements:")
For Each autoElement In elementCollectionControl
Console.WriteLine(autoElement.Current.Name)
Next autoElement End Sub 'StaticConditionExamples

权限

  • 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。

平台

Windows Vista

.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。

版本信息

.NET Framework

受以下版本支持:3.5、3.0 SP1、3.0

请参见

概念

获取 UI 自动化元素
基于属性条件查找 UI 自动化元素

参考

System.Windows.Automation 命名空间

自动化测试 using System.Windows.Automation;的更多相关文章

  1. Windows Automation API和自动化测试

    https://zhuanlan.zhihu.com/p/22083601\ 感谢轮子哥点赞,这会儿消息扎堆过来了,轮带逛果然不是随便说说的…… 第二篇一个简单的Windows Automation ...

  2. Windows Automation API 3.0 Overview

    https://www.codemag.com/article/0810042 While general accessibility requirements (such as font color ...

  3. System.Windows.Application.Current.Dispatcher.BeginInvoke

    System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>                        ...

  4. 开发客户端软件时,出现System.Windows.Markup.XamlParseException错误

    开发客户端软件时,出现System.Windows.Markup.XamlParseException错误,通过查看错误消息,发现TCPIP的一个COM组件在安装软件过程中被删除了,重新注册了一下TC ...

  5. WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常

    WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常 在wpf中封装Com组件时,调用组件 ...

  6. 类型“System.Windows.Markup.IUriContext”在未被引用的程序集中定义 解决办法

    错误 CS0012: 类型“System.Windows.Markup.IUriContext”在未被引用的程序集中定义.必须添加对程序集“System.Xaml, Version=4.0.0.0, ...

  7. 类型“System.Windows.Markup.IQueryAmbient”在未被引用的程序集中定义

    错误    1    类型"System.Windows.Markup.IQueryAmbient"在未被引用的程序集中定义.必须添加对程序集"System.Xaml, ...

  8. System.Windows.Markup.IQueryAmbient 在未被应用的程序集中定义

    按照<WIndows Presentation Foundation>中介绍建立的WPF程序,可以在VS2008中创建控制台应用程序所得.创建之后将程序集输出类型改为:Windows应用程 ...

  9. System.Windows.Media.Imageing.BItmapImage 这么用才不会占用文件

    // Read byte[] from png file BinaryReader binReader = new BinaryReader(File.Open(filepath, FileMode. ...

随机推荐

  1. Obiee11g变量

    原文地址:http://xee123.blog.163.com/blog/static/277119942012612104438332/ Obiee 版本 11.1.1.5 库变量 库变量在同一时刻 ...

  2. JavaScript知识点

    Day 01 1.Javascript 概述 1.什么是Javascript (JS) Javascript 是一种运行于 JS解释器/引擎 中的解释型脚本语言 JS解释器/引擎 :JS运行环境 1. ...

  3. PHP 类中的魔术方法

    定义: PHP类中以两个下画线“__”开头的方法被称为魔术方法. 分类: 例如:构造方法:__construct:析构方法:__destruct:动态重载:__set().__get().__call ...

  4. 不能从const char *转换为LPCWSTR

    编译器有时候会根据编码方式来选择定义为LPCWSTR还是LPCTSTR LPSTR: 32bit指针 指向一个字符串,每个字符占1字节. 相当于 char *   LPCSTR: 32-bit指针 指 ...

  5. 如何让老式浏览器支持html5新增的语义元素

    html5新增加了一些语义元素,如header, footer, nav, aritcle, aside,等等. 然而,有些老款浏览器无法识别这些元素,会把它们当成 inline 元素对待,这会导致一 ...

  6. Python的标准输出

    遇到什么就添加到这里来. 首先,是最基本的. print "Number is %d %f %s"%(intA,floatB,stringC) 如果对浮点数的精度有所要求的话,比如 ...

  7. .net 创建计划任务开机后自动以管理员身份启动运行 win7 ~ win10

    假如要启动 this.exe.以下逻辑中会启动先后关联启动三个实例分别是ABC.先启动第一个实例A,A启动实例B,B启动实例C. 要求: 1.如果没有以管理员权限运行,则请求管理员权限运行,即使没有请 ...

  8. 其他信息: 未找到源,不过,未能搜索部分或所有事件日志。 若要创建源,您需要用于读取所有事件日志的权限以确保新的源名称是唯一的。 不可访问的日志: Security。

    其他信息: 未找到源,不过,未能搜索部分或所有事件日志.  若要创建源,您需要用于读取所有事件日志的权限以确保新的源名称是唯一的.  不可访问的日志: Security. System.Diagnos ...

  9. 理解jquery的$.extend()、$.fn和$.fn.extend()

    jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(); jQuery.extend(); jQuery.fn jQuery.fn = jQuery.prototype ...

  10. Centos 6.5编译安装yum

    1.删除redhat原有的yum rpm -aq|grep yum|xargs rpm -e --nodeps 2.下载yum安装文件 wget http://mirrors.163.com/cent ...