使用 UI Automation 实现自动化测试--3

Chapter 3  UI Automation中的几个重要属性

Control Tree of the AutomationElement


在UI Automation 控件树中,根节点为Desktop window, 其他运行在用户桌面的窗体都作为Desktop window的子节点。

如下图所示:
           
使用 UI Automation 实现自动化测试--3_20869

Desktop window可通过AutomationElement.RootElement属性获取,子节点中的窗体或对话框可通过

AutomationElement.RootElement.FindAll(TreeScope.Descendants, condition)



AutomationElement.RootElement.FindFirt(TreeScope.Descendants, condition)来获取.


AutomationElement property

在UI Automation中有如下几个重要属性:

1.        AutomationIdProperty: 通过AutomationId来查找AutomationElement。

2.        NameProperty: 通过控件的Name属性来查找AutomationElement。

3.        ControlType: 通过控件的类型来查找AutomationElement

4.        AutomationId: 唯一地标识 自动化元素,将其与同级相区分。

5.        Name:  WPF 按钮的 Content 属性、Win32 按钮的 Caption 属性以及 HTML 图像的 ALT 属性都映射到 UI 自动化视图中的同一个属性 Name。

注:PropertyCondition类是用来对相关属性进行条件匹配,在控件树中查找控件时,可以通过最佳匹配来找到相应的控件。

如下代码列出了使用不同的属性来构建PropertyCondition,通过PropertyCondition来查找控件树中的控件.
  1. public class PropertyConditions
  2.     {
  3.         static PropertyCondition propertyCondition;

  4.         /// <summary>
  5.         /// Create PropertyCondition by AutomationId
  6.         /// </summary>
  7.         /// <param name="automationId">Control AutomationId</param>
  8.         /// <returns>Return PropertyCondition instance</returns>
  9.         public static PropertyCondition GetAutomationIdProperty(object automationId)
  10.         {
  11.             propertyCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, automationId);
  12.             return propertyCondition;
  13.         }

  14.         /// <summary>
  15.         ///
  16.         /// </summary>
  17.         /// <param name="controlType"></param>
  18.         /// <returns></returns>
  19.         public static PropertyCondition GetControlTypeProperty(object controlType)
  20.         {
  21.             propertyCondition = new PropertyCondition(AutomationElement.ControlTypeProperty, controlType);
  22.             return propertyCondition;
  23.         }

  24.         /// <summary>
  25.         ///
  26.         /// </summary>
  27.         /// <param name="controlName"></param>
  28.         /// <returns></returns>
  29.         public static PropertyCondition GetNameProperty(object controlName)
  30.         {
  31.             propertyCondition = new PropertyCondition(AutomationElement.NameProperty, controlName);
  32.             return propertyCondition;
  33.         }

  34.         /// <summary>
  35.         /// Find element by specific PropertyCondition
  36.         /// </summary>
  37.         /// <param name="condition">PropertyCondition instance</param>
  38.         /// <returns>Target automation element</returns>
  39.         public static AutomationElement FindElement(PropertyCondition condition)
  40.         {
  41.             return AutomationElement.RootElement.FindFirst(TreeScope.Descendants, condition);
  42.         }
  43.     }
复制代码
文/ 开着拖拉机
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值