public static AutomationElement FindAtIndex(this ItemContainerPattern pattern, int index) { var current = 0; AutomationElement item = null; while (true) { item = pattern.FindItemByProperty(item, null, null); if (item == null) { throw new ArgumentOutOfRangeException(nameof(index), index, "Could not get item at index."); } if (current == index) { if (item.TryGetVirtualizedItemPattern(out var virtualizedItemPattern)) { virtualizedItemPattern.Realize(); } return(item); } current++; } }
/// <summary> /// Run Testcase /// </summary> /// <param name="testcase"></param> /// <param name="targetControlName"></param> public void RunTestcase(UiaDistributedTestcase testcase, string targetControlName) { EnsureState(); //get the top window element AutomationElement topWindowElement = AutomationElement.FromHandle(hWndHost); //get the UIElement AutomationElement clientElement = topWindowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, targetControlName)); #if TESTBUILD_CLR40 //Since we could not find the element we were looking for, there is a chance it is virtualized //This only applies to .NET 4.0 and above assuming UIACore 7 is also present. if ((clientElement == null) && (AutomationElement.IsItemContainerPatternAvailableProperty != null)) { //here we will find all elements that are item containers and therefore might contain virtualized items AutomationElementCollection elementCollection = topWindowElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.IsItemContainerPatternAvailableProperty, true)); for (int i = 0; i < elementCollection.Count; i++) { ItemContainerPattern itemContainerPattern = elementCollection[i].GetCurrentPattern(ItemContainerPattern.Pattern) as ItemContainerPattern; // look for the item we are searching for in each container clientElement = itemContainerPattern.FindItemByProperty(null, AutomationElement.AutomationIdProperty, targetControlName); if (clientElement != null) { break; } } } #endif //perform the test testcase.RunTest(remoteHost, clientElement, targetControlName); }
public static AutomationElement LastOrDefault(this ItemContainerPattern pattern) { var item = pattern.FindItemByProperty(null, null, null); if (item == null) { return(null); } while (true) { var temp = pattern.FindItemByProperty(item, null, null); if (temp == null) { if (item.TryGetVirtualizedItemPattern(out var virtualizedItemPattern)) { virtualizedItemPattern.Realize(); } return(item); } item = temp; } }
public static AutomationElement?FirstOrDefault(this ItemContainerPattern pattern) { if (pattern is null) { throw new ArgumentNullException(nameof(pattern)); } var item = pattern.FindItemByProperty(null, null, null); if (item is { } &&
public static AutomationElement FirstOrDefault(this ItemContainerPattern pattern) { var item = pattern.FindItemByProperty(null, null, null); if (item != null && item.TryGetVirtualizedItemPattern(out var virtualizedItemPattern)) { virtualizedItemPattern.Realize(); } return(item); }
// Test is not working on Windows 8 due to changes in Explorer public void VirtualizedPatternTest() { AutomationElement itemsView = ExplorerTargetTests.explorerHost.Element.FindFirst(TreeScope.TreeScope_Subtree, new PropertyCondition(AutomationElement.ClassNameProperty, "UIItemsView")); Assert.IsNotNull(itemsView); // Get the container Assert.IsTrue((bool)itemsView.GetCurrentPropertyValue(AutomationElement.IsItemContainerPatternAvailableProperty)); ItemContainerPattern container = (ItemContainerPattern)itemsView.GetCurrentPattern(ItemContainerPattern.Pattern); // Look for something we know is there and is probably below the fold AutomationElement item1 = container.FindItemByProperty(null, AutomationElement.NameProperty, "winver"); Assert.IsNotNull(item1); // Let's get another one AutomationElement item2 = container.FindItemByProperty(item1, AutomationElement.NameProperty, "xcopy"); Assert.IsNotNull(item2); // Check the bounding rect -- should be empty Rectangle rect1 = item2.Current.BoundingRectangle; Assert.AreEqual(0, rect1.Width); Assert.AreEqual(0, rect1.Height); // Get the virtualized pattern Assert.IsTrue((bool)item2.GetCurrentPropertyValue(AutomationElement.IsVirtualizedItemPatternAvailableProperty)); VirtualizedItemPattern virtItem2 = (VirtualizedItemPattern)item2.GetCurrentPattern(VirtualizedItemPattern.Pattern); Assert.IsNotNull(item2); // Realize the item and give the window a moment to scroll virtItem2.Realize(); System.Threading.Thread.Sleep(100 /* ms */); // Check the bounding rect now - should not be empty Rectangle rect2 = item2.Current.BoundingRectangle; Assert.AreNotEqual(0, rect2.Width); Assert.AreNotEqual(0, rect2.Height); }
public static IEnumerable <T> AllItems <T>(this ItemContainerPattern pattern, Func <AutomationElement, T> wrap) { AutomationElement item = null; while (true) { item = pattern.FindItemByProperty(item, null, null); if (item == null) { break; } if (item.TryGetVirtualizedItemPattern(out var virtualizedItemPattern)) { virtualizedItemPattern.Realize(); } yield return(wrap(item)); } }
public static AutomationElement FindByText(this ItemContainerPattern pattern, string text) { var item = pattern.FindItemByProperty(null, AutomationElement.NameProperty, text); if (item != null) { return(item); } var byNameCondition = new PropertyCondition(AutomationElement.NameProperty, text); while (true) { item = pattern.FindItemByProperty(item, null, null); if (item == null) { throw new InvalidOperationException($"Did not find an item by text {text}"); } if (item.TryGetVirtualizedItemPattern(out var virtualizedItemPattern)) { virtualizedItemPattern.Realize(); } if (item.Name() == text) { return(item); } if (item.IsContentElement() && item.TryFindFirst(TreeScope.Children, byNameCondition, out _)) { return(item); } } }
public ItemContainerPatternDescriptorObj(ItemContainerPattern pattern) { _pattern = pattern; }
public static bool TryGetItemContainerPattern(this AutomationElement element, out ItemContainerPattern result) { if (element.TryGetCurrentPattern(System.Windows.Automation.ItemContainerPattern.Pattern, out var pattern)) { result = (ItemContainerPattern)pattern; return(true); } result = null; return(false); }
/// <summary> /// Retrieves an element by the specified property value. /// </summary> /// <param name="control">The control.</param> /// <param name="startAfter">The item in the container after which to begin the search.</param> /// <param name="property">The property that contains the value to retrieve.</param> /// <param name="value">The value to retrieve.</param> /// <returns> /// The first item that matches the search criterion; otherwise, null if no items match /// </returns> internal static AutomationElement FindItemByProperty(AutomationElement control, AutomationElement startAfter, AutomationProperty property, Object value) { ItemContainerPattern pattern = (ItemContainerPattern)CommonUIAPatternHelpers.CheckPatternSupport(ItemContainerPattern.Pattern, control); return(pattern.FindItemByProperty(startAfter, property, value)); }
public static T FindAtIndex <T>(this ItemContainerPattern pattern, int index, Func <AutomationElement, T> wrap) { return(wrap(FindAtIndex(pattern, index))); }