[UI Automation] UIAutomationElement

Every piece of UI is represented as an object of “System.Windows.Automation.AutomationElement” in the UIA tree. Therfore “UIAutomationElement” is a main building block of UI Automation.

Its properties are used as identifiers by UIA client applications.

1. AutomationProperty

System.Windows.Automation.AutomationProperty” class represents a property of an “AutomationElement” object.

You do not create an instance of “AutomationProperty” directly. The “AutomationElement” class provides static fields from that you  can get a specific instance of “AutomationProperty“.

The main purpose of “AutomationProperty” is to identify an UI element in a tree by retrieving the property value from an “AutomationElement” object.

public static readonly AutomationProperty AutomationIdProperty; 
public static readonly AutomationProperty ClassNameProperty;
public static readonly AutomationProperty ControlTypeProperty;
public static readonly AutomationProperty NameProperty;

Please refer to the MSDN Library (“http://msdn.microsoft.com/en-us/library/system.windows.automation.automationelement.aspx” ).

2. Getting the Property Value

You can look up the property value using the “GetCurrentPropertyValue” method.

  • public Object GetCurrentPropertyValue(AutomationProperty property)

3. Getting the Root Element

The static “RootElement” property of “AutomationElement” represents the root element for the current desktop. This is the starting point of the UIA tree to navigate.

  • public static AutomationElement RootElement { get; }
AutomationElement desktop = AutomationElement.RootElement;
Console.WriteLine(desktop.GetCurrentPropertyValue(AutomationElement.ClassNameProperty) as string);

4. Getting the Focused Element

You can get the currently focused UI Element using the static “FocusedElement” property.

  • public static AutomationElement FocusedElement { get; }
AutomationElement focus = AutomationElement.FocusedElement;
Console.WriteLine("{0}: {1}",
  focus.GetCurrentPropertyValue(AutomationElement.NameProperty) as string,
  focus.GetCurrentPropertyValue(AutomationElement.ClassNameProperty) as string);

5. Finding UI Elements (1) – Conditions

To find the UI Element in a UIA tree, you need to specify the starting element and the condition.

The search condition is represented by “System.Windows.Automation.Condition” element. You are using the subclasses to set up the search conditions.

  • System.Windows.Automation.PropertyCondition
  • System.Windows.Automation.AndCondition
  • System.Windows.Automation.OrCondition
  • System.Windows.Automation.NotCondition

Generally, you are going to specify which property is used to search the element by creating “PropertyCondition” objects. You can combine these property conditions using the “AndCondition“, “OrCondition“, or “NotCondition“.

  • public PropertyCondition(AutomationProperty property, Object value) // constructor
  • public AndCondition(params Condition[] conditions)
  • public OrCondition(params Condition[] conditions)
  • public NotCondition(Condition condition)
AndCondition conditionEnabledButtons = new AndCondition(
  new PropertyCondition(AutomationElement.IsEnabledProperty, true),
  new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button));

6. Finding UI Elements (2) – TreeScope

System.Windows.Automation.TreeScope” enumeration vontains values that specify the search scope of elements within the UIA  tree.

  • Element: the element itself
  • Children: the element’s immediate children
  • Descendants: the element’s descendants, including children
  • Subtree: the search root and its all descendants

7. Finding UI Elements (3)

The “AutomationElement” class provides the methods to search the elements.

  • public AutomationElementCollection FindAll(TreeScope scope, Condition condition)
  • public AutomationElement FindFirst(TreeScope scope, Condition condition)

FindAll” returns all elements that satisfies the condition and “FindFirst” returns only the first element. When no match is found, “FindAll” returns an empty collection and “FindFirst” returns null.

AutomationElementCollection” class implements “IEnumerable” interface, so you can use LINQ to manipulate the search result.

// Looking for WPF Application whose title is "WPFApp"
AutomationElement application
  = AutomationElement.RootElement.FindFirst(
    new PropertyCondition(AutomationElement.NameProperty, "WPFApp"));

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s