private void WalkTree(AutomationElement parent, TreeWalker walker, XmlWriter writer, IList<AutomationElement> elements) { var element = this.elementFactory.GetElement(parent); writer.WriteStartElement(element.TypeName); if (elements != null) { writer.WriteAttributeString("_index_", elements.Count.ToString()); elements.Add(parent); } writer.WriteAttributeString("id", element.ID); writer.WriteAttributeString("framework", element.UIFramework); writer.WriteAttributeString("name", element.Name); writer.WriteAttributeString("value", element.Value); writer.WriteAttributeString("class", element.ClassName); writer.WriteAttributeString("help", element.Help); writer.WriteAttributeString("visible", element.Visible ? "true" : "false"); writer.WriteAttributeString("enabled", element.Enabled ? "true" : "false"); writer.WriteAttributeString("focusable", element.Focusable ? "true" : "false"); writer.WriteAttributeString("focused", element.Focused ? "true" : "false"); writer.WriteAttributeString("selected", element.Selected ? "true" : "false"); writer.WriteAttributeString("protected", element.Protected ? "true" : "false"); writer.WriteAttributeString("scrollable", element.Scrollable ? "true" : "false"); writer.WriteAttributeString("handle", element.Handle.ToString()); writer.WriteAttributeString("x", element.X.ToString()); writer.WriteAttributeString("y", element.Y.ToString()); writer.WriteAttributeString("width", element.Width.ToString()); writer.WriteAttributeString("height", element.Height.ToString()); writer.WriteAttributeString( "bounds", string.Format("[{0},{1}][{2},{3}]", element.X, element.Y, element.Width, element.Height)); var node = walker.GetFirstChild(parent); var children = new List<AutomationElement>(); while (node != null) { this.WalkTree(node, walker, writer, elements); children.Add(node); node = walker.GetNextSibling(node); // GetNextSibling may recursively return the first child again. if (node != null && children.Contains(node)) { logger.Warn("Next sibling node causes a loop. STOP!"); node = null; break; } } writer.WriteEndElement(); }
public IEnumerable<WiniumElement> IterFind(TreeScope scope, Predicate<WiniumElement> predicate) { if (scope != TreeScope.Descendants && scope != TreeScope.Children) { throw new ArgumentException("scope should be one of TreeScope.Descendants or TreeScope.Children"); } var walker = new TreeWalker(Condition.TrueCondition); var elementNode = walker.GetFirstChild(this.AutomationElement); while (elementNode != null) { var winiumElement = new WiniumElement(elementNode); if (predicate == null || predicate(winiumElement)) { yield return winiumElement; } if (scope == TreeScope.Descendants) { foreach (var descendant in winiumElement.IterFind(scope, predicate)) { yield return descendant; } } elementNode = walker.GetNextSibling(elementNode); } }
public void TreeIterationTest() { TreeWalker walker = new TreeWalker(Automation.ControlViewCondition); AutomationElement startingElement = ExplorerTargetTests.explorerHost.Element; AutomationElement iter = startingElement; iter = walker.GetFirstChild(iter); iter = walker.GetNextSibling(iter); iter = walker.GetParent(iter); Assert.AreEqual(startingElement, iter); iter = walker.GetLastChild(iter); iter = walker.GetPreviousSibling(iter); iter = walker.GetParent(iter); Assert.AreEqual(startingElement, iter); }
protected void GetAutomationElementsSiblings(bool nextSibling) { if (!this.CheckControl(this)) { return; } System.Windows.Automation.TreeWalker walker = new System.Windows.Automation.TreeWalker( System.Windows.Automation.Condition.TrueCondition); // 20120823 foreach (AutomationElement inputObject in this.InputObject) { AutomationElement sibling = null; if (nextSibling) { // 20120823 //sibling = walker.GetNextSibling(this.InputObject); sibling = walker.GetNextSibling(inputObject); } else { // 20120823 //sibling = walker.GetPreviousSibling(this.InputObject); sibling = walker.GetPreviousSibling(inputObject); } WriteObject(this, sibling); } // 20120823 }
private ArrayList getAutomationElementsWithWalker( AutomationElement element, string name, string automationId, string className, string[] controlType, bool caseSensitive, bool onlyOneResult, bool onlyTopLevel) { ArrayList resultCollection = new ArrayList(); System.Windows.Automation.TreeWalker walker = new System.Windows.Automation.TreeWalker( System.Windows.Automation.Condition.TrueCondition); System.Windows.Automation.AutomationElement oneMoreElement; try { oneMoreElement = walker.GetFirstChild(element); try{ WriteVerbose( this, oneMoreElement.Current.ControlType.ProgrammaticName + "\t" + oneMoreElement.Current.Name + "\t" + oneMoreElement.Current.AutomationId); } catch {} resultCollection = processAutomationElement( oneMoreElement, name, automationId, className, controlType, caseSensitive, onlyOneResult, onlyTopLevel); if ((onlyTopLevel || onlyOneResult) && (null != resultCollection) && resultCollection.Count > 0) { return resultCollection; } else if (null != resultCollection) { WriteObject(this, resultCollection); } while (oneMoreElement != null) { oneMoreElement = walker.GetNextSibling(oneMoreElement); try{ WriteVerbose( this, oneMoreElement.Current.ControlType.ProgrammaticName + "\t" + oneMoreElement.Current.Name + "\t" + oneMoreElement.Current.AutomationId); } catch {} resultCollection = processAutomationElement( oneMoreElement, name, automationId, className, controlType, caseSensitive, onlyOneResult, onlyTopLevel); if ((onlyTopLevel || onlyOneResult) && (null != resultCollection) && resultCollection.Count > 0) { return resultCollection; } else if (null != resultCollection) { WriteObject(this, resultCollection); } } } catch {} return resultCollection; }
// Find the application's base IElement. // private void InitializeHandler() { Condition condition = Condition.FalseCondition; TreeWalker walker; bool windowFound = false; var stopWatch = new Stopwatch(); // Initialize startup conditions for an application started from file path // if (this.startedFromFilePath) { p.Refresh(); this.window = AutomationElement.FromHandle(this.GetWindowHandle()); /* Not needed - changed to grab element directly from the window handle. * System.Windows.Forms.MessageBox.Show(new IntPtr(this.GetWindowHandle()).ToString()); condition = new AndCondition(new Condition[] { new PropertyCondition(AutomationElement.NativeWindowHandleProperty, this.GetWindowHandle()), new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window) }); */ } // Initialize startup conditions for an application that's already running // else { condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window); } // Initialize walker // walker = new TreeWalker(condition); // Get AutomationElement corresponding to Application Under Test // stopWatch.Start(); //TODO: THIS while (this.window == null && stopWatch.ElapsedMilliseconds < ScriptProperties.Timeout) { AutomationElement current = walker.GetFirstChild(AutomationElement.RootElement); string name; while (current != null && !windowFound) { // Check names, if necessary // if (!this.startedFromFilePath) { name = current.Current.Name; if (this.foundByRegex) { // Check regex match // if (this.windowTitleRegex.IsMatch(name)) { windowFound = true; } } else { // Check name match // if (this.applicationIdentifier.Equals(name)) { windowFound = true; } } } else { // Found by pID, and is guaranteed to be unique, so no assignment is required // and loop will break at the appropriate time. // windowFound = true; } // Get next sibling // current = walker.GetNextSibling(current); } this.window = current; } stopWatch.Stop(); // Throw exception if application wasn't found. // if (this.window == null) { throw new WindowNotFoundException("Windows application for '" + this.applicationIdentifier + "' not found!"); } // Initialize windowHandler // this.windowHandler = Factories.ElementFactory.GetIElementFromHandlers( Factories.ElementActionHandlerFactory.GetActionHandler(window), Factories.ElementDescendantHandlerFactory.GetDescendantHandler(window) ); }
private void WalkTree(AutomationElement parent, TreeWalker walker, XmlWriter writer, IList<AutomationElement> elements) { var element = this.elementFactory.GetElement(parent); writer.WriteStartElement(element.TypeName); if (elements != null) { writer.WriteAttributeString("_index_", elements.Count.ToString()); elements.Add(parent); } writer.WriteAttributeString("id", element.ID); writer.WriteAttributeString("framework", element.UIFramework); writer.WriteAttributeString("name", element.Name); writer.WriteAttributeString("value", element.Value); writer.WriteAttributeString("class", element.ClassName); writer.WriteAttributeString("help", element.Help); writer.WriteAttributeString("visible", element.Visible ? "true" : "false"); writer.WriteAttributeString("enabled", element.Enabled ? "true" : "false"); writer.WriteAttributeString("focusable", element.Focusable ? "true" : "false"); writer.WriteAttributeString("focused", element.Focused ? "true" : "false"); writer.WriteAttributeString("selected", element.Selected ? "true" : "false"); writer.WriteAttributeString("protected", element.Protected ? "true" : "false"); writer.WriteAttributeString("scrollable", element.Scrollable ? "true" : "false"); writer.WriteAttributeString("handle", element.Handle.ToString()); writer.WriteAttributeString("x", element.X.ToString()); writer.WriteAttributeString("y", element.Y.ToString()); writer.WriteAttributeString("width", element.Width.ToString()); writer.WriteAttributeString("height", element.Height.ToString()); writer.WriteAttributeString( "bounds", string.Format("[{0},{1}][{2},{3}]", element.X, element.Y, element.Width, element.Height)); var child = walker.GetFirstChild(parent); while (child != null) { this.WalkTree(child, walker, writer, elements); child = walker.GetNextSibling(child); } writer.WriteEndElement(); }
/// <summary> /// Walks the UI Automation tree and adds the control type of each enabled control /// element it finds to a TreeView. /// </summary> /// <param name="rootElement">The root of the search on this iteration.</param> /// <param name="treeNode">The node in the TreeView for this iteration.</param> /// <remarks> /// This is a recursive function that maps out the structure of the subtree beginning at the /// UI Automation element passed in as rootElement on the first call. This could be, for example, /// an application window. /// CAUTION: Do not pass in AutomationElement.RootElement. Attempting to map out the entire subtree of /// the desktop could take a very long time and even lead to a stack overflow. /// </remarks> private void WalkEnabledElements(AutomationElement rootElement, List<AutomationElement> elementName) { Condition condition1 = new PropertyCondition(AutomationElement.IsControlElementProperty, true); Condition condition2 = new PropertyCondition(AutomationElement.IsEnabledProperty, true); TreeWalker walker = new TreeWalker(new AndCondition(condition1, condition2)); AutomationElement elementNode = walker.GetFirstChild(rootElement); while (elementNode != null) { if (add == true) { elementName.Add(elementNode); } if (elementNode.Current.Name.Contains("XTalk ")) add = true; WalkEnabledElements(elementNode, elementName); if (elementNode.Current.Name.Contains("XTalk ")) add = true; elementNode = walker.GetNextSibling(elementNode); } }
private void VerifyGetNextSibling (TreeWalker tree, AutomationElement rootElement, AutomationElement expectedElement) { AutomationElement actualChild = tree.GetNextSibling (rootElement); Assert.AreEqual (expectedElement, actualChild, String.Format ("GetNextSibling with root element named {0}: Expected element named {1}, got element named {2}", GetName (rootElement), GetName (expectedElement), GetName (actualChild))); if (expectedElement != null) Assert.AreNotSame (expectedElement, actualChild, "GetNextSibling returns a new instance"); }
public void GetNextSiblingTest () { Condition buttonCondition = new PropertyCondition (AEIds.ControlTypeProperty, ControlType.Button); TreeWalker buttonWalker = new TreeWalker (buttonCondition); AssertRaises<ArgumentNullException> ( () => buttonWalker.GetNextSibling (null), "passing null to TreeWalker.GetNextSibling"); VerifyGetNextSibling (buttonWalker, button7Element, button6Element); VerifyGetNextSibling (buttonWalker, button6Element, button5Element); VerifyGetNextSibling (buttonWalker, button5Element, button4Element); VerifyGetNextSibling (buttonWalker, button4Element, button3Element); VerifyGetNextSibling (buttonWalker, button3Element, button2Element); // Check that there are still more siblings (just without groupBox1Element as parent) VerifyGetNextSibling (buttonWalker, button2Element, button1Element); // TODO: Test how for buttonWalker, GetNextSibling // eventually gets all buttons on the entire // desktop? // Elements whose parents (not the desktop) are also in // the tree run out of siblings as expected. Condition groupCondition = new AndCondition ( new PropertyCondition (AEIds.ControlTypeProperty, ControlType.Group), new PropertyCondition (AEIds.ProcessIdProperty, p.Id)); TreeWalker groupWalker = new TreeWalker (groupCondition); VerifyGetNextSibling (groupWalker, groupBox3Element, groupBox2Element); VerifyGetNextSibling (groupWalker, groupBox2Element, null); // When only other matching thing in tree is child (TODO: Hangs) //VerifyGetNextSibling (groupWalker, groupBox1Element, groupBox2Element); }