public override AutomationElement GetFirstResult(Query query, TimeSpan timeout)
{
Debug.WriteLine("FirstResult - " + query, "UIAutomation-SearchEngine-TreeWalker");
var root = query.Root;
var scope = query.Scope;
var conditions = query.Conditions;
// The callback for return matched elements.
var returnMatchedChild = new WithElementCallback(child => {
Trace.WriteLine("Checking " + AutomationElementHelper.ToString(child) + "...", "UIAutomation-SearchEngine-TreeWalker");
return ConditionHelper.IsMeetsRequirements(conditions, child) ? child : null;
});
// Execute based on scope.
switch (scope) {
case TreeScope.Children:
return (AutomationElement) ExecuteGetResult(() => ExecuteWithChildren(root, returnMatchedChild), timeout);
case TreeScope.Descendants:
return (AutomationElement) ExecuteGetResult(() => ExecuteWithDescendants(root, returnMatchedChild), timeout);
default:
throw new NotSupportedException("Scope '" + scope + "' is not supported for TreeWalker search engines");
}
}