Skip to content
Roman edited this page Apr 13, 2017 · 9 revisions

Table of Contents

  1. Intro to searching
  2. Primary search methods
  3. Search Conditions
  4. Searching by XPath

Intro to searching

Searching controls is probably the most important things that need to be done when doing UI tests. FlaUI provides several possibilities to make this as easy as possible.

Primary search methods

Each AutomationElement has a bunch of methods which help you with that.
FindFirst and FindAll can be used to fully control the search. They are pretty much redirected 1:1 to the native calls.
You can pass a TreeScope to define in which scope the search should be and a ConditionBasewhich describes what it should search. There are convenience methods available for searching in children or descendants:

  • FindFirstChild
  • FindAllChildren
  • FindFirstDescendant
  • FindAllDescendants

There is also a FindFirstNested and FindAllNested method where you can pass an array of conditions and it searches each level of the array in the children of the previous one.

Examples:

// Full manual search and condition creation
var child = parent.FindFirst(TreeScope.Children, new PropertyCondition(Automation.PropertyLibrary.Element.AutomationIdProperty, "someId"));
// Using the convenience method and the ConditionFactory
var child = parent.FindFirstChild(ConditionFactory.ByAutomationId("someId"));
// Using the convenience method and the func notation
var child = parent.FindFirstChild(cf => cf.ByAutomationId("someId"));

Search Conditions

The search conditions describe the "where" of the search. The following conditions are available:

Condition Description
AndCondition Used to put multiple conditions together with an and
OrCondition Used to put multiple conditions together with an or
BoolCondition Used for simple true and false conditions, usually not needed
NotCondition Used to negate a condition
PropertyCondition Used to search for specific properties like name or automation id

Conditions can be either directly created or with the ConditionFactory (available on any element).

Searching by XPath

Use FindFirstByXPath and FindAllByXPath to search for an element by XPath. The node name is the ControlType of the element. The element where you call the find method is the root element of the search. Currently supported attributes are:

  • AutomationId
  • Name
  • ClassName
  • HelpText

Be aware that certain things, like searching in submenues, does not work since the search by XPath only finds elements currently available by UIA wheres submenues mostly need a click on the parent menu item to be available to UIA. Example:

window.FindFirstByXPath($"/MenuBar/MenuItem[@Name='File']");
Clone this wiki locally