自动化测试 using System.Windows.Automation;

本文档介绍了如何使用C#中的System.Windows.Automation命名空间进行自动化测试,包括查找UI元素、搜索范围、条件、从事件中检索元素等方法,适用于.NET Framework 3.0及以上版本的UI自动化测试。
摘要由CSDN通过智能技术生成

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 属性来筛选元素。

其他条件是根据一个或多个 PropertyC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值