Automation.UI.Tree.SearchEngines.TreeWalkerSearchEngine.GetFirstResult C# (CSharp) Method

GetFirstResult() public method

Gets the first result for the specified query.
public GetFirstResult ( Query query, System.TimeSpan timeout ) : AutomationElement
query Query The query.
timeout System.TimeSpan The maximum amount of time to wait for the required element to become available.
return System.Windows.Automation.AutomationElement
        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");
            }
        }