public void GridPatternCachedTest() { CacheRequest req = new CacheRequest(); req.Add(GridItemPattern.Pattern); req.Add(GridPattern.Pattern); req.Add(GridPattern.RowCountProperty); req.Add(GridPattern.ColumnCountProperty); req.Add(GridItemPattern.RowProperty); req.Add(GridItemPattern.ColumnProperty); req.Add(GridItemPattern.ContainingGridProperty); using (req.Activate()) { AutomationElement itemsView = ExplorerTargetTests.explorerHost.Element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ClassNameProperty, "UIItemsView")); Assert.IsNotNull(itemsView); // Try out the Grid Pattern GridPattern grid = (GridPattern)itemsView.GetCachedPattern(GridPattern.Pattern); Assert.IsTrue(grid.Cached.ColumnCount > 0); Assert.IsTrue(grid.Cached.RowCount > 0); // GridItem AutomationElement gridItemElement = grid.GetItem(0, 0); Assert.IsNotNull(gridItemElement); GridItemPattern gridItem = (GridItemPattern)gridItemElement.GetCachedPattern(GridItemPattern.Pattern); Assert.AreEqual(gridItem.Cached.Row, 0); Assert.AreEqual(gridItem.Cached.Column, 0); Assert.AreEqual(gridItem.Cached.ContainingGrid, itemsView); } }
public IEnumerable<ITab> GetTabsOfWindow(IntPtr hWnd) { var notepadPlusPlusWindow = AutomationElement.FromHandle(hWnd); var cacheRequest = new CacheRequest(); cacheRequest.Add(AutomationElement.NameProperty); cacheRequest.Add(AutomationElement.LocalizedControlTypeProperty); cacheRequest.Add(SelectionItemPattern.Pattern); cacheRequest.Add(SelectionItemPattern.SelectionContainerProperty); cacheRequest.TreeScope = TreeScope.Element; AutomationElement tabBarElement; using (cacheRequest.Activate()) { tabBarElement = notepadPlusPlusWindow.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "tab")); } if(tabBarElement == null) yield break; var tabElements = tabBarElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "tab item")); for (var tabIndex = 0; tabIndex < tabElements.Count; tabIndex++) { yield return new NotepadPlusPlusTab(tabElements[tabIndex]); } }
public void ActivateTest() { CacheRequest target = new CacheRequest(); Assert.AreNotEqual(CacheRequest.Current, target); using (target.Activate()) { Assert.AreEqual(CacheRequest.Current, target); CacheRequest target2 = new CacheRequest(); using (target2.Activate()) { Assert.AreNotEqual(CacheRequest.Current, target); } } }
public void MyTestInitialize() { // Get all children of the desktop for our target collection CacheRequest cacheReq = new CacheRequest(); cacheReq.Add(AutomationElement.NameProperty); cacheReq.Add(AutomationElement.NativeWindowHandleProperty); using (cacheReq.Activate()) { this.testColl = AutomationElement.RootElement.FindAll( TreeScope.Children, Condition.TrueCondition); Assert.IsNotNull(this.testColl); Assert.IsTrue(this.testColl.Count > 0); } }
public void CachedRelationshipTest() { CacheRequest req = new CacheRequest(); req.TreeScope = TreeScope.Element | TreeScope.Children; using (req.Activate()) { AutomationElement rootElement = AutomationElement.RootElement; AutomationElementCollection rootChildren = rootElement.CachedChildren; Assert.IsNotNull(rootChildren); Assert.IsTrue(rootChildren.Count > 0); AutomationElement firstChild = rootChildren[0]; AutomationElement cachedParent = firstChild.CachedParent; Assert.IsNotNull(cachedParent); Assert.AreEqual(cachedParent, rootElement); } }
public void ExpandCollapsePatternCachedTest() { using (AppHost host = new AppHost("rundll32.exe", "shell32.dll,Control_RunDLL intl.cpl")) { CacheRequest req = new CacheRequest(); req.Add(ExpandCollapsePattern.Pattern); req.Add(ExpandCollapsePattern.ExpandCollapseStateProperty); using (req.Activate()) { // Find a well-known combo box AutomationElement combo = host.Element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.AutomationIdProperty, "1021")); Assert.IsNotNull(combo); ExpandCollapsePattern expando = (ExpandCollapsePattern)combo.GetCachedPattern(ExpandCollapsePattern.Pattern); Assert.AreEqual(expando.Cached.ExpandCollapseState, ExpandCollapseState.Collapsed); } } }
public void WindowPatternCachedTest() { using (AppHost host = new AppHost("notepad.exe", "")) { CacheRequest req = new CacheRequest(); req.Add(WindowPattern.Pattern); req.Add(WindowPattern.CanMaximizeProperty); req.Add(WindowPattern.CanMinimizeProperty); req.Add(WindowPattern.IsTopmostProperty); req.Add(WindowPattern.WindowInteractionStateProperty); req.Add(WindowPattern.WindowVisualStateProperty); using (req.Activate()) { AutomationElement cachedEl = host.Element.GetUpdatedCache(req); // Window Pattern WindowPattern windowPattern = (WindowPattern)cachedEl.GetCachedPattern(WindowPattern.Pattern); Assert.IsTrue(windowPattern.Cached.CanMaximize); Assert.IsTrue(windowPattern.Cached.CanMinimize); Assert.IsFalse(windowPattern.Cached.IsTopmost); Assert.AreNotEqual(windowPattern.Cached.WindowVisualState, WindowVisualState.Minimized); Assert.AreNotEqual(windowPattern.Cached.WindowInteractionState, WindowInteractionState.Closing); } } }
public void GetCachedPatternTest() { CacheRequest req = new CacheRequest(); req.Add(LegacyIAccessiblePattern.Pattern); using (req.Activate()) { LegacyIAccessiblePattern pattern = (LegacyIAccessiblePattern)GetTaskbar().GetCachedPattern(LegacyIAccessiblePattern.Pattern); Assert.IsNotNull(pattern); } }
public void FindFirstTest() { // Find a child CacheRequest cacheReq = new CacheRequest(); cacheReq.Add(AutomationElement.NativeWindowHandleProperty); using (cacheReq.Activate()) { Assert.AreSame(CacheRequest.Current, cacheReq); AutomationElement actualCached = AutomationElement.RootElement.FindFirst( TreeScope.Children, Condition.TrueCondition); Assert.IsNotNull(actualCached); int nativeHwnd = (int)actualCached.GetCachedPropertyValue(AutomationElement.NativeWindowHandleProperty); Assert.IsTrue(nativeHwnd != 0); } }
public void FindAllTest() { // Find all children CacheRequest cacheReq = new CacheRequest(); cacheReq.Add(AutomationElement.NameProperty); cacheReq.Add(AutomationElement.NativeWindowHandleProperty); using (cacheReq.Activate()) { AutomationElementCollection actual = AutomationElement.RootElement.FindAll( TreeScope.Children, Condition.TrueCondition); Assert.IsNotNull(actual); Assert.IsTrue(actual.Count > 0); foreach (AutomationElement elem in actual) { Assert.IsNotNull(elem); int nativeHwnd = (int)elem.GetCachedPropertyValue(AutomationElement.NativeWindowHandleProperty); Assert.IsTrue(nativeHwnd != 0); } } }
public AutomationElement GetUpdatedAEWhenStructureChanged(AutomationElement mainWidow, AutomationPattern patternToCache = null, AutomationProperty propertyToCache = null) { CacheRequest cacheRequest = new CacheRequest(); cacheRequest.TreeScope = TreeScope.Element | TreeScope.Descendants | TreeScope.Children; cacheRequest.AutomationElementMode = AutomationElementMode.Full; cacheRequest.TreeFilter = System.Windows.Automation.Automation.RawViewCondition; if (patternToCache != null) { cacheRequest.Add(patternToCache); } else { cacheRequest.Add(SelectionPattern.Pattern); cacheRequest.Add(WindowPattern.Pattern); cacheRequest.Add(InvokePattern.Pattern); cacheRequest.Add(TogglePattern.Pattern); cacheRequest.Add(ExpandCollapsePattern.Pattern); cacheRequest.Add(ValuePattern.Pattern); cacheRequest.Add(SelectionItemPattern.Pattern); } if (propertyToCache != null) { cacheRequest.Add(propertyToCache); } else { cacheRequest.Add(AutomationElement.NameProperty); cacheRequest.Add(AutomationElement.AutomationIdProperty); cacheRequest.Add(AutomationElement.ClassNameProperty); cacheRequest.Add(AutomationElement.ControlTypeProperty); } AutomationElement updatedElement; using (cacheRequest.Activate()) { updatedElement = mainWidow.GetUpdatedCache(cacheRequest); } return updatedElement; }
public void DoWork() { long timeToGetUncached = 0; long timeToGetCached = 0; // Create a System.Diagnostics.Stopwatch. var stopWatchTimer = new Stopwatch(); // TEST 1: Get the target element without caching, and retrieve // current properties. stopWatchTimer.Start(); AutomationElement targetNoCache = null; try { targetNoCache = AutomationElement.FromPoint(_targetPoint); } catch (ElementNotAvailableException) { OutputLine("Could not retrieve element."); return; } // Get current properties. _currentPropCount = 0; GetCurrentProperties(targetNoCache, 0); stopWatchTimer.Stop(); timeToGetUncached = stopWatchTimer.Elapsed.Ticks; // TEST 2: Get the target element with caching, and retrieve // cached properties. // Create CacheRequest. var fetchRequest = new CacheRequest(); // Add properties to fetch. fetchRequest.Add(AutomationElement.NameProperty); fetchRequest.Add(AutomationElement.AutomationIdProperty); fetchRequest.Add(AutomationElement.ControlTypeProperty); fetchRequest.Add(AutomationElement.FrameworkIdProperty); fetchRequest.Add(AutomationElement.IsContentElementProperty); // Set options. fetchRequest.AutomationElementMode = _mode; fetchRequest.TreeScope = _treeScope; fetchRequest.TreeFilter = Automation.RawViewCondition; // Activate the CacheRequest and fetch the target. AutomationElement targetCached = null; using (fetchRequest.Activate()) { stopWatchTimer.Reset(); stopWatchTimer.Start(); try { targetCached = AutomationElement.FromPoint(_targetPoint); } catch (InvalidOperationException) { OutputLine("InvalidOperationException. Could not retrieve element."); return; } catch (ElementNotAvailableException) { OutputLine("ElementNotAvailableException. Could not retrieve element."); return; } } // CacheRequest is now inactive. // Get cached properties. GetCachedProperties(targetCached, true); stopWatchTimer.Stop(); timeToGetCached = stopWatchTimer.Elapsed.Ticks; // TEST 3: Get updated cache. stopWatchTimer.Reset(); stopWatchTimer.Start(); var cacheUpdated = false; if (_mode == AutomationElementMode.Full) { var updatedTargetCached = targetCached.GetUpdatedCache(fetchRequest); GetCachedProperties(updatedTargetCached, false); // Fetches were counted again, so divide count by 2. _cachedPropCount /= 2; cacheUpdated = true; stopWatchTimer.Stop(); } var updateTicks = stopWatchTimer.Elapsed.Ticks; // END OF TESTS. // Display results var nameProperty = targetNoCache.Current.Name; var framework = targetNoCache.Current.FrameworkId; OutputLine("Name: " + nameProperty); OutputLine("Framework: " + framework); OutputLine(_elementCount + " cached element(s)."); OutputLine(timeToGetUncached.ToString("N0") + " Ticks to retrieve element(s) and get " + _currentPropCount + " current properties."); OutputLine(timeToGetCached.ToString("N0") + " Ticks to retrieve element(s) and get " + _cachedPropCount + " cached properties."); // Show ratio between current and cached performance. var ratio = timeToGetUncached/(float) timeToGetCached; if (ratio > 2) { OutputLine("Current:Cached = " + ratio.ToString("N0") + ":1"); } else { // Print with decimal. OutputLine("Current:Cached = " + ratio.ToString("N1") + ":1"); } if (cacheUpdated) { OutputLine(updateTicks.ToString("N0") + " Ticks to update cache and get properties."); } else { OutputLine("Cannot update cache in None mode."); } OutputLine(""); }
public void TablePatternCachedTest() { CacheRequest req = new CacheRequest(); req.Add(TablePattern.Pattern); req.Add(TableItemPattern.Pattern); req.Add(GridPattern.Pattern); req.Add(GridItemPattern.Pattern); req.Add(GridPattern.RowCountProperty); req.Add(GridPattern.ColumnCountProperty); req.Add(GridItemPattern.RowProperty); req.Add(GridItemPattern.ColumnProperty); req.Add(GridItemPattern.ContainingGridProperty); req.Add(TablePattern.RowHeadersProperty); req.Add(TablePattern.ColumnHeadersProperty); req.Add(TableItemPattern.RowHeaderItemsProperty); req.Add(TableItemPattern.ColumnHeaderItemsProperty); using (req.Activate()) { AutomationElement itemsView = ExplorerTargetTests.explorerHost.Element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ClassNameProperty, "UIItemsView")); Assert.IsNotNull(itemsView); // TablePattern test TablePattern table = (TablePattern)itemsView.GetCachedPattern(TablePattern.Pattern); Assert.IsTrue(table.Cached.ColumnCount > 0); Assert.IsTrue(table.Cached.RowCount > 0); Assert.IsTrue(table.Cached.GetRowHeaders().Length == 0); Assert.IsTrue(table.Cached.GetColumnHeaders().Length > 0); AutomationElement tableItemElement = table.GetItem(0, 0); TableItemPattern tableItem = (TableItemPattern)tableItemElement.GetCachedPattern(TableItemPattern.Pattern); Assert.AreEqual(tableItem.Cached.Row, 0); Assert.AreEqual(tableItem.Cached.Column, 0); Assert.AreEqual(tableItem.Cached.ContainingGrid, itemsView); Assert.IsTrue(tableItem.Cached.GetColumnHeaderItems().Length == 1); Assert.IsTrue(tableItem.Cached.GetRowHeaderItems().Length == 0); } }
public void ValuePatternCachedTest() { using (AppHost host = new AppHost("rundll32.exe", "shell32.dll,Control_RunDLL intl.cpl")) { CacheRequest req = new CacheRequest(); req.Add(WindowPattern.Pattern); req.Add(WindowPattern.CanMaximizeProperty); req.Add(WindowPattern.CanMinimizeProperty); req.Add(WindowPattern.IsTopmostProperty); req.Add(WindowPattern.WindowInteractionStateProperty); req.Add(WindowPattern.WindowVisualStateProperty); using (req.Activate()) { // Find a well-known combo box AutomationElement combo = host.Element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.AutomationIdProperty, "1021")); Assert.IsNotNull(combo); ValuePattern value = (ValuePattern)combo.GetCurrentPattern(ValuePattern.Pattern); Assert.IsFalse(value.Current.IsReadOnly); Assert.IsTrue(value.Current.Value.Length > 0); } } }
public void TransformPatternCachedTest() { using (AppHost host = new AppHost("notepad.exe", "")) { CacheRequest req = new CacheRequest(); req.Add(TransformPattern.Pattern); req.Add(TransformPattern.CanMoveProperty); req.Add(TransformPattern.CanResizeProperty); req.Add(TransformPattern.CanRotateProperty); using (req.Activate()) { AutomationElement cachedEl = host.Element.GetUpdatedCache(req); TransformPattern transformPattern = (TransformPattern)cachedEl.GetCachedPattern(TransformPattern.Pattern); // Coded to expectations for an explorer window Assert.IsTrue(transformPattern.Cached.CanMove); Assert.IsTrue(transformPattern.Cached.CanResize); Assert.IsFalse(transformPattern.Cached.CanRotate); // Little move transformPattern.Move(10, 10); // Little resize transformPattern.Resize(200, 200); } } }
public void TogglePatternCachedTest() { using (AppHost host = new AppHost("rundll32.exe", "shell32.dll,Control_RunDLL main.cpl")) { CacheRequest req = new CacheRequest(); req.Add(TogglePattern.Pattern); req.Add(TogglePattern.ToggleStateProperty); using (req.Activate()) { // Find a well-known checkbox AutomationElement checkbox = host.Element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.AutomationIdProperty, "114")); Assert.IsNotNull(checkbox); TogglePattern toggle = (TogglePattern)checkbox.GetCachedPattern(TogglePattern.Pattern); ToggleState originalState = toggle.Cached.ToggleState; Assert.IsTrue(originalState == ToggleState.On || originalState == ToggleState.Off); } } }
public void RangeValuePatternCachedTest() { using (AppHost host = new AppHost("rundll32.exe", "shell32.dll,Control_RunDLL main.cpl ,2")) { CacheRequest req = new CacheRequest(); req.Add(RangeValuePattern.Pattern); req.Add(RangeValuePattern.IsReadOnlyProperty); req.Add(RangeValuePattern.MaximumProperty); req.Add(RangeValuePattern.MinimumProperty); req.Add(RangeValuePattern.SmallChangeProperty); req.Add(RangeValuePattern.LargeChangeProperty); using (req.Activate()) { // Find a well-known slider AutomationElement slider = host.Element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.AutomationIdProperty, "101")); Assert.IsNotNull(slider); RangeValuePattern range = (RangeValuePattern)slider.GetCachedPattern(RangeValuePattern.Pattern); double originalValue = range.Cached.Value; Assert.IsTrue(range.Cached.SmallChange >= 0); Assert.IsTrue(range.Cached.LargeChange >= 0); Assert.IsTrue(originalValue >= range.Cached.Minimum); Assert.IsTrue(originalValue <= range.Cached.Maximum); Assert.IsFalse(range.Cached.IsReadOnly); } } }
public void GetCachedPatternTest() { CacheRequest req = new CacheRequest(); req.Add(InvokePatternIdentifiers.Pattern); using (req.Activate()) { InvokePattern pattern = (InvokePattern)GetStartButton().GetCachedPattern(InvokePatternIdentifiers.Pattern); Assert.IsNotNull(pattern); } }
public void LegacyIAccessiblePatternCachedTest() { using (AppHost host = new AppHost("rundll32.exe", "shell32.dll,Control_RunDLL intl.cpl")) { CacheRequest req = new CacheRequest(); req.Add(LegacyIAccessiblePattern.Pattern); req.Add(LegacyIAccessiblePattern.NameProperty); req.Add(LegacyIAccessiblePattern.ChildIdProperty); req.Add(LegacyIAccessiblePattern.KeyboardShortcutProperty); req.Add(LegacyIAccessiblePattern.RoleProperty); req.Add(LegacyIAccessiblePattern.ValueProperty); req.Add(LegacyIAccessiblePattern.StateProperty); req.Add(LegacyIAccessiblePattern.SelectionProperty); using (req.Activate()) { // Find a well-known combo box AutomationElement combo = host.Element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.AutomationIdProperty, "1021")); Assert.IsNotNull(combo); // BLOCK { LegacyIAccessiblePattern acc = (LegacyIAccessiblePattern)combo.GetCachedPattern(LegacyIAccessiblePattern.Pattern); Assert.AreEqual(0, acc.Cached.ChildId); Assert.IsTrue(acc.Cached.Name.Length > 0); Assert.IsTrue(acc.Cached.Value.Length > 0); Assert.AreEqual("Alt+f", acc.Cached.KeyboardShortcut); Assert.AreEqual((uint)0x2E /* combo box */, acc.Cached.Role); Assert.AreEqual((uint)0x100400 /* collapsed|focusable */, acc.Cached.State & 0x100400); } // Get the tab controls AutomationElement tabCtrl = host.Element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.AutomationIdProperty, "12320")); Assert.IsNotNull(tabCtrl); // BLOCK { LegacyIAccessiblePattern acc = (LegacyIAccessiblePattern)tabCtrl.GetCachedPattern(LegacyIAccessiblePattern.Pattern); AutomationElement[] selection = acc.Cached.GetSelection(); Assert.IsTrue(selection.Length > 0); Assert.IsInstanceOf<AutomationElement>(selection[0]); Assert.AreEqual(ControlType.TabItem, selection[0].Current.ControlType); } } } }
public void MultipleViewPatternTest() { CacheRequest req = new CacheRequest(); req.Add(MultipleViewPattern.Pattern); req.Add(MultipleViewPattern.CurrentViewProperty); req.Add(MultipleViewPattern.SupportedViewsProperty); using (req.Activate()) { AutomationElement itemsView = ExplorerTargetTests.explorerHost.Element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ClassNameProperty, "UIItemsView")); Assert.IsNotNull(itemsView); MultipleViewPattern multiView = (MultipleViewPattern)itemsView.GetCachedPattern(MultipleViewPattern.Pattern); int[] supportedViews = multiView.Cached.GetSupportedViews(); Assert.IsNotNull(supportedViews.Length > 0); bool inSupportedViews = false; foreach (int view in supportedViews) { if (view == multiView.Cached.CurrentView) { inSupportedViews = true; break; } string viewName = multiView.GetViewName(view); Assert.IsTrue(viewName.Length > 0); } Assert.IsTrue(inSupportedViews); } }
public static void ExecuteWithUpdatedCache(this AutomationElement ae, CacheRequest cr, Action<AutomationElement> action) { using (cr.Activate()) action(ae.GetUpdatedCache(cr)); }
public void Bug1() { application = new WinFormTestConfiguration(string.Empty).Launch(); Thread.Sleep(1000); var propertyCondition = new PropertyCondition(AutomationElement.NameProperty, "Form1"); AutomationElement element = AutomationElement.RootElement.FindFirst(TreeScope.Children, propertyCondition); AutomationElementCollection collection; var request = new CacheRequest(); using (request.Activate()) collection = element.FindAll(TreeScope.Subtree, Condition.TrueCondition); int cachedChildrenFoundFor = 0; foreach (AutomationElement automationElement in collection) { try { AutomationElementCollection children = automationElement.CachedChildren; cachedChildrenFoundFor++; } catch (InvalidOperationException) {} } Assert.AreNotEqual(0, cachedChildrenFoundFor, "Cached children not found for even one AutomationElement out of " + collection.Count + " elements"); }