// Get the child element by its Name/AutomationId // public IElement GetChild(string identifier) { Log.LogMessage("Looking for child '" + identifier+ "' of '" + this.ae.Current.Name + "'."); var walker = new TreeWalker(this.GenerateOrCondition(identifier)); var currentElement = walker.GetFirstChild(this.ae); // Throw exception if element was not found correctly. // if (currentElement == null) { throw new ChildNotFoundException("Child '" + identifier + "' not found for parent '" + this.ae.Current.Name + "'."); } return Factories.ElementFactory.GetIElementFromBase(currentElement); }
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(); }
private InfoItem GetActiveItemFromHandle(string processName, IntPtr handle) { try { if (processName == ProcessList.iexplore.ToString()) { #region IE IEAccessible tabs = new IEAccessible(); string pageTitle = tabs.GetActiveTabCaption(handle); string pageURL = tabs.GetActiveTabUrl(handle); return new InfoItem { Title = pageTitle, Uri = pageURL, Type = InfoItemType.Web, }; #endregion } else if (processName == ProcessList.firefox.ToString()) { #region FireFox AutomationElement ff = AutomationElement.FromHandle(handle); System.Windows.Automation.Condition condition1 = new PropertyCondition(AutomationElement.IsContentElementProperty, true); System.Windows.Automation.Condition condition2 = new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaContentWindowClass"); TreeWalker walker = new TreeWalker(new AndCondition(condition1, condition2)); AutomationElement elementNode = walker.GetFirstChild(ff); ValuePattern valuePattern; if (elementNode != null) { AutomationPattern[] pattern = elementNode.GetSupportedPatterns(); foreach (AutomationPattern autop in pattern) { if (autop.ProgrammaticName.Equals("ValuePatternIdentifiers.Pattern")) { valuePattern = elementNode.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern; string pageURL = valuePattern.Current.Value.ToString(); string pageTitle = GetActiveWindowText(handle); pageTitle = pageTitle.Remove(pageTitle.IndexOf(" - Mozilla Firefox")); return new InfoItem { Title = pageTitle, Uri = pageURL, Type = InfoItemType.Web, }; } } } return null; #endregion } else if (processName == "chrome") { String pageURL = string.Empty; String pageTitle = string.Empty; IntPtr urlHandle = FindWindowEx(handle, IntPtr.Zero, "Chrome_AutocompleteEditView", null); //IntPtr titleHandle = FindWindowEx(handle, IntPtr.Zero, "Chrome_WindowImpl_0", null); IntPtr titleHandle = FindWindowEx(handle, IntPtr.Zero, "Chrome_WidgetWin_0", null); const int nChars = 256; StringBuilder Buff = new StringBuilder(nChars); int length = SendMessage(urlHandle, WM_GETTEXTLENGTH, 0, 0); if (length > 0) { //Get URL from chrome tab SendMessage(urlHandle, WM_GETTEXT, nChars, Buff); pageURL = Buff.ToString(); //Get the title GetWindowText((int)titleHandle, Buff, nChars); pageTitle = Buff.ToString(); return new InfoItem { Title = pageTitle, Uri = pageURL, Type = InfoItemType.Web, }; } else return null; } else if (processName == ProcessList.OUTLOOK.ToString()) { #region Outlook MS_Outlook.Application outlookApp; MS_Outlook.MAPIFolder currFolder; MS_Outlook.Inspector inspector; MS_Outlook.MailItem mailItem; outlookApp = (MS_Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application"); currFolder = outlookApp.ActiveExplorer().CurrentFolder; inspector = outlookApp.ActiveInspector(); if (inspector == null) { mailItem = (MS_Outlook.MailItem)outlookApp.ActiveExplorer().Selection[1]; } else { Regex regex = new Regex(@"\w* - Microsoft Outlook"); if (regex.IsMatch(GetActiveWindowText(handle))) { mailItem = (MS_Outlook.MailItem)outlookApp.ActiveExplorer().Selection[1]; } else { mailItem = (MS_Outlook.MailItem)inspector.CurrentItem; } } string subject = mailItem.Subject; string entryID = mailItem.EntryID; return new InfoItem { Title = subject, Uri = entryID, Type = InfoItemType.Email, }; #endregion } else if (processName == ProcessList.WINWORD.ToString() || processName == ProcessList.EXCEL.ToString() || processName == ProcessList.POWERPNT.ToString() || processName == ProcessList.AcroRd32.ToString()) { #region Word, Excel, PPT, Adobe Reader return null; #endregion } else { return null; } } catch (Exception) { return null; } }
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); }
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); } }
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; }
protected void GetAutomationElementsChildren(AutomationElement inputObject, bool firstChild) { if (!this.CheckControl(this)) { return; } System.Windows.Automation.TreeWalker walker = new System.Windows.Automation.TreeWalker( System.Windows.Automation.Condition.TrueCondition); AutomationElement sibling = null; if (firstChild) { // 20120824 //sibling = walker.GetFirstChild(this.InputObject); sibling = walker.GetFirstChild(inputObject); } else { // 20120824 //sibling = walker.GetLastChild(this.InputObject); sibling = walker.GetLastChild(inputObject); } WriteObject(this, sibling); }
// 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); } }
public string GetURLfromFirefox(AutomationElement rootElement) { Condition condition1 = new PropertyCondition(AutomationElement.IsContentElementProperty, true); TreeWalker walker = new TreeWalker(condition1); AutomationElement elementNode = walker.GetFirstChild(rootElement); if (elementNode != null) { Console.WriteLine(elementNode.Current.Name); } return "null"; }
public void GetFirstChildTest () { Condition buttonCondition = new PropertyCondition (AEIds.ControlTypeProperty, ControlType.Button); TreeWalker buttonWalker = new TreeWalker (buttonCondition); AssertRaises<ArgumentNullException> ( () => buttonWalker.GetFirstChild (null), "passing null to TreeWalker.GetFirstChild"); VerifyGetFirstChild (buttonWalker, groupBox1Element, button7Element); }
private void VerifyGetFirstChild (TreeWalker tree, AutomationElement rootElement, AutomationElement expectedElement) { AutomationElement actualChild = tree.GetFirstChild (rootElement); Assert.AreEqual (expectedElement, actualChild, String.Format ("GetFirstChild 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, "GetFirstChild returns a new instance"); }