/// <summary> /// Tries to return the value of the specified property. /// </summary> /// <typeparam name="T">The type of the object to read.</typeparam> /// <param name="source">The source node.</param> /// <param name="propertyName">The property name.</param> /// <param name="result">The property value converted to a value of the T type, or default(T).</param> /// <returns>Whether the operation succeeded.</returns> internal static bool TryGetProperty <T>(this IObjectTreeNode source, string propertyName, out T result) { try { result = source.GetProperty <T>(propertyName); return(true); } catch (InvocationException) { result = default(T); return(false); } }
/// <summary> /// Find all controls matching the search pattern. /// </summary> /// <param name="parentHint">The parent or null if it should be searched.</param> /// <returns>Matching controls.</returns> protected virtual IEnumerable <IControl> Matches(IObjectTreeNode parentHint = null) { parentHint = parentHint ?? TryParent; if (parentHint == null) { return(Enumerable.Empty <IControl>()); } else { if (matches == null) { Wait.For(() => (matches = parentHint.FindAll <IControl>(SearchPattern, PageObjectSearchDepth)).Any()); } return(matches); } }
/// <summary> /// Finds all nodes with the given full class name. /// </summary> /// <param name="source">The node to search from.</param> /// <param name="pattern">The search pattern.</param> /// <param name="depth">The search depth.</param> /// <returns>The enumeration nodes.</returns> /// <typeparam name="T">The expected object type.</typeparam> internal static IReadOnlyList <T> MyFindAll <T>(this IObjectTreeNode source, ISearchPattern pattern, int depth = 1) where T : class, IObjectTreeNode { List <T> result = new List <T>(); Queue <Tuple <IObjectTreeNode, int> > q = new Queue <Tuple <IObjectTreeNode, int> >(); q.Enqueue(new Tuple <IObjectTreeNode, int>(source, 0)); while (q.Count > 0) { Tuple <IObjectTreeNode, int> current = q.Dequeue(); if (current == null) { continue; } if (current.Item2 != 0) { object value; if (pattern.GetPatternItems().All((i) => current.Item1.TryGetProperty(i.Key, out value) && i.Value.Equals(value))) { result.Add(current.Item1.Cast <T>()); continue; // do not further search on this path } } if (current.Item2 != depth) { foreach (IObjectTreeNode child in current.Item1.Children) { q.Enqueue(new Tuple <IObjectTreeNode, int>(child, current.Item2 + 1)); } } } return(result); }
/// <summary> /// Find all controls matching the search pattern and the predicate. /// </summary> /// <param name="parentHint">The parent or null if it should be searched.</param> /// <returns>Matching controls.</returns> protected override IEnumerable <IControl> Matches(IObjectTreeNode parentHint = null) { return(base.Matches(parentHint).Where(c => Predicate(c))); }