/// <summary> /// Find *multiple* UIItems based on SearchCriteria /// </summary> /// <param name="searchCriteria">UIItem identification conditions</param> /// <param name="scope">Scope of search</param> /// <param name="timeout">Timeout value for search (milliseconds)</param> /// <returns>Matching UIItems</returns> protected static IUIItem[] FindUIItems( SearchCriteria searchCriteria, UIItem scope, int timeout) { if (timeout > 0) { WhiteConfigHelper.OriginalFindUIItemTimeout = timeout; } if (scope == null) { scope = WorkSpace.MainWindow.Window; } Report.Output( Report.Level.Debug, Resources.UIItemMultipleFindUsingSearchCriteriaMsg, searchCriteria.ToString(), typeof(T).ToString(), scope.PrimaryIdentification); IUIItem[] matchingUIItems = null; var stopwatch = new Stopwatch(); var elapsedTime = new TimeSpan(); try { stopwatch.Start(); matchingUIItems = scope.GetMultiple(searchCriteria); elapsedTime = stopwatch.Elapsed; Report.Output( Report.Level.Debug, Resources.UIItemMultipleFoundMsg, matchingUIItems.Length.ToString(), elapsedTime); } catch (AutomationException ex) { elapsedTime = stopwatch.Elapsed; Report.Output( Report.Level.Debug, Resources.UIItemMultipleNotFoundMsg, elapsedTime, ex.Message); } finally { stopwatch.Stop(); if (timeout > 0) { WhiteConfigHelper.ResetFindUIItemTimeout(); } } return(matchingUIItems); }
private static AutomationElement FindAEwithTimeout(XmlNode node, string xPath, int timeout) { WhiteConfigHelper.ResetFindUIItemTimeout(); timeout = timeout == 0 ? WhiteConfigHelper.OriginalFindUIItemTimeout : timeout; return(WaitForAE(() => XmlSearcher.XmlNodeToAutomationElement(node, xPath), timeout)); }
private static AutomationElement WaitForAE(Func <AutomationElement> functionToGetAE, int timeout) { WhiteConfigHelper.ResetFindUIItemTimeout(); timeout = timeout == 0 ? WhiteConfigHelper.OriginalFindUIItemTimeout : timeout; AutomationElement matchingAE = null; var stopwatch = new Stopwatch(); try { stopwatch.Start(); do { matchingAE = functionToGetAE(); } while (matchingAE == null && stopwatch.ElapsedMilliseconds < timeout); } catch (AutomationException ex) { Report.Output(Report.Level.Debug, Resources.UIItemNotFoundMsg, stopwatch.Elapsed, ex.Message); } finally { if (matchingAE != null) { Report.Output(Report.Level.Debug, Resources.UIItemFoundMsg, stopwatch.Elapsed); Report.Output(Report.Level.Debug, Resources.UIItemCheckUsingExtraCriteriaMsg, matchingAE.Current.AutomationId, matchingAE.Current.Name); } stopwatch.Stop(); } return(matchingAE); }
/// <summary> /// Find a UIItem (of a particular type) by Name and ClassName /// </summary> /// <param name="nameProperty">value for AutomationElement.NameProperty</param> /// <param name="classProperty">value for AutomationElement.ClassNameProperty</param> /// <param name="friendlyName">friendly name for UIItem</param> /// <param name="scope">scope of search</param> /// <param name="timeout">Timeout value for search (milliseconds)</param> /// <returns>matching UIItem</returns> protected static UIItem FindUIItem( string nameProperty, string classProperty, string friendlyName, AutomationElement scope, int timeout) { if (timeout > 0) { WhiteConfigHelper.OriginalFindUIItemTimeout = timeout; } if (scope == null) { scope = WorkSpace.MainWindow.Window.AutomationElement; } Report.Output( Report.Level.Debug, Resources.UIItemFindUsingSearchCriteriaMsg, AutomationElement.NameProperty, nameProperty, AutomationElement.ClassNameProperty, classProperty, scope); var stopwatch = new Stopwatch(); var elapsedTime = new TimeSpan(); try { Condition condition = new AndCondition( new PropertyCondition(AutomationElement.NameProperty, nameProperty), new PropertyCondition(AutomationElement.ClassNameProperty, classProperty)); stopwatch.Start(); var collection = scope.FindAll(TreeScope.Descendants, condition); elapsedTime = stopwatch.Elapsed; if (collection.Count == 0) { Report.Output( Report.Level.Debug, Resources.UIItemIsNullMsg, collection.Count.ToString(), elapsedTime); return(null); } if (collection.Count > 1) { Report.Output( Report.Level.Debug, Resources.UIItemMultipleFoundMsg, collection.Count.ToString(), elapsedTime); } return(new UIItem(collection[0], new NullActionListener())); } catch (ElementNotAvailableException ex) { elapsedTime = stopwatch.Elapsed; Report.Output( Report.Level.Debug, Resources.UIItemMultipleNotFoundMsg, elapsedTime, ex.Message); return(null); } finally { stopwatch.Stop(); if (timeout > 0) { WhiteConfigHelper.ResetFindUIItemTimeout(); } } }
/// <summary> /// Gets *multiple* UIItems based on AutomationProperties conditions /// </summary> /// <param name="propertiesList"> /// A 2-tuple where T1 is AutomationProperty and T2 is the value of AutomationElement property. /// Supports a combination of two or more PropertyConditions objects /// </param> /// <param name="scope">Scope of search</param> /// <param name="timeout">Timeout value for search (milliseconds)</param> /// <returns>Matching UIItems</returns> protected static List <IUIItem> FindUIItems(List <Tuple <AutomationProperty, object> > propertiesList, AutomationElement scope, int timeout) { if (timeout > 0) { WhiteConfigHelper.OriginalFindUIItemTimeout = timeout; } if (scope == null) { scope = WorkSpace.MainWindow.Window.AutomationElement; } Report.Output( Report.Level.Debug, Resources.UIItemMultipleFindUsingAutomationPropertyCriteriaMsg, scope); var stopwatch = new Stopwatch(); var elapsedTime = new TimeSpan(); try { var listPropertyCondition = new List <PropertyCondition>(); foreach (var tplAutomationProperty in propertiesList) { listPropertyCondition.Add(new PropertyCondition(tplAutomationProperty.Item1, tplAutomationProperty.Item2)); } var elementConditions = new AndCondition(listPropertyCondition.ToArray()); stopwatch.Start(); var collection = scope.FindAll(TreeScope.Descendants, elementConditions); elapsedTime = stopwatch.Elapsed; if (collection.Count == 0) { Report.Output( Report.Level.Debug, Resources.UIItemIsNullMsg, collection.Count.ToString(), elapsedTime); return(null); } var matchingUIItems = new List <IUIItem>(); foreach (AutomationElement child in collection) { var child1 = new UIItem(child, new NullActionListener()); matchingUIItems.Add(child1); } return(matchingUIItems); } catch (ElementNotAvailableException ex) { elapsedTime = stopwatch.Elapsed; Report.Output( Report.Level.Debug, Resources.UIItemMultipleNotFoundMsg, elapsedTime, ex.Message); return(null); } finally { stopwatch.Stop(); if (timeout > 0) { WhiteConfigHelper.ResetFindUIItemTimeout(); } } }
/// <summary> /// Find a UIItem (of a particular type) based on SearchCriteria and ExtraCriteria /// </summary> /// <param name="searchCriteria">UIItem identification conditions</param> /// <param name="automationProperty">AutomationElement property</param> /// <param name="automationPropertyValue">Value of AutomationElement property</param> /// <param name="scope">Scope of search</param> /// <param name="timeout">Timeout value for search (milliseconds)</param> /// <returns>Matching UIItem</returns> protected static T FindUIItem( SearchCriteria searchCriteria, AutomationProperty automationProperty, object automationPropertyValue, UIItem scope, int timeout) { if (timeout > 0) { WhiteConfigHelper.OriginalFindUIItemTimeout = timeout; } if (scope == null) { scope = WorkSpace.MainWindow.Window; } Report.Output( Report.Level.Debug, Resources.UIItemFindUsingExtraCriteriaMsg, searchCriteria.ToString(), typeof(T).ToString(), automationProperty.ProgrammaticName, automationPropertyValue, scope.PrimaryIdentification); T matchingUIItem = null; var hasFound = true; var stopwatch = new Stopwatch(); var elapsedTime = new TimeSpan(); try { var index = 0; stopwatch.Start(); do { matchingUIItem = scope.Get <T>(searchCriteria.AndIndex(index)); index++; Report.Output( Report.Level.Debug, Resources.UIItemCheckUsingExtraCriteriaMsg, matchingUIItem.PrimaryIdentification, matchingUIItem.Name); } while (!matchingUIItem.ValueOfEquals(automationProperty, automationPropertyValue)); } catch (AutomationException ex) { elapsedTime = stopwatch.Elapsed; Report.Output( Report.Level.Debug, Resources.UIItemNotFoundMsg, elapsedTime, ex.Message); hasFound = false; matchingUIItem = null; } finally { if (hasFound) { elapsedTime = stopwatch.Elapsed; Report.Output( Report.Level.Debug, Resources.UIItemFoundMsg, elapsedTime); } stopwatch.Stop(); if (timeout > 0) { WhiteConfigHelper.ResetFindUIItemTimeout(); } } return(matchingUIItem); }