Chapter 3 UI Automation中的几个重要属性
Control Tree of the AutomationElement
在UI Automation控件树中,根节点为Desktop window, 其他运行在用户桌面的窗体都作为Desktop window的子节点。
如下图所示:
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来查找控件树中的控件.
Code
public class PropertyConditions
{
static PropertyCondition propertyCondition;
/// <summary>
/// Create PropertyCondition by AutomationId
/// </summary>
/// <param name="automationId">Control AutomationId</param>
/// <returns>Return PropertyCondition instance</returns>
public static PropertyCondition GetAutomationIdProperty(object automationId)
{
propertyCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, automationId);
return propertyCondition;
}
/// <summary>
///
/// </summary>
/// <param name="controlType"></param>
/// <returns></returns>
public static PropertyCondition GetControlTypeProperty(object controlType)
{
propertyCondition = new PropertyCondition(AutomationElement.ControlTypeProperty, controlType);
return propertyCondition;
}
/// <summary>
///
/// </summary>
/// <param name="controlName"></param>
/// <returns></returns>
public static PropertyCondition GetNameProperty(object controlName)
{
propertyCondition = new PropertyCondition(AutomationElement.NameProperty, controlName);
return propertyCondition;
}
/// <summary>
/// Find element by specific PropertyCondition
/// </summary>
/// <param name="condition">PropertyCondition instance</param>
/// <returns>Target automation element</returns>
public static AutomationElement FindElement(PropertyCondition condition)
{
return AutomationElement.RootElement.FindFirst(TreeScope.Descendants, condition);
}
}
Open Source code
Github: https://github.com/cumtkangyi/ATP