private AutomationElement GetAncestorWithHandle(AutomationElement element) { try { var walker = new TreeWalker(Condition.TrueCondition); var testParent = walker.GetParent(element); while (testParent != null && testParent.Current.NativeWindowHandle == 0) { testParent = walker.GetParent(testParent); if ( testParent != null && testParent.Current.ProcessId > 0 && testParent.Current.NativeWindowHandle != 0 ) return testParent; } return testParent.Current.NativeWindowHandle != 0 ? testParent : null; } catch { 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); }
private void collectAncestry( ref System.Collections.Generic.List<string> listForNodes, ref System.Collections.Generic.List<string> listForCode, IUiElement element) { cleanAncestry(); TreeWalker walker = new TreeWalker( Condition.TrueCondition); try { // 20131109 //AutomationElementIUiElement testparent = element; IUiElement testparent = element; // 20140312 // if (testparent != null && (int)testparent.Current.ProcessId > 0) { if (testparent != null && (int)testparent.GetCurrent().ProcessId > 0) { listForNodes.Add(getNodeNameFromAutomationElement(testparent)); // 20131109 //if (testparent != AutomationElement.RootElement) { if (!Equals(testparent, UiElement.RootElement)) { listForCode.Add(getCodeFromAutomationElement(testparent, walker)); } /* if (testparent != UiElement.RootElement) { listForCode.Add(getCodeFromAutomationElement(testparent, walker)); } */ } // 20140312 // while (testparent != null && (int)testparent.Current.ProcessId > 0) { while (testparent != null && (int)testparent.GetCurrent().ProcessId > 0) { testparent = // 20131109 //walker.GetParent(testparent); // 20131118 // property to method //new UiElement(walker.GetParent(testparent.SourceElement)); // 20140102 // new UiElement(walker.GetParent(testparent.GetSourceElement())); new UiElement(walker.GetParent(testparent.GetSourceElement() as AutomationElement)); // 20140312 // if (testparent.Current.ProcessId <= 0) continue; if (testparent.GetCurrent().ProcessId <= 0) continue; /* if (testparent == null || (int) testparent.Current.ProcessId <= 0) continue; */ listForNodes.Add(getNodeNameFromAutomationElement(testparent)); // 20131109 //if (testparent != AutomationElement.RootElement) { if (!Equals(testparent, UiElement.RootElement)) { listForCode.Add(getCodeFromAutomationElement(testparent, walker)); } /* if (testparent != UiElement.RootElement) { listForCode.Add(getCodeFromAutomationElement(testparent, walker)); } */ /* if (testparent != null && (int)testparent.Current.ProcessId > 0) { listForNodes.Add(getNodeNameFromAutomationElement(testparent)); if (testparent != AutomationElement.RootElement) { listForCode.Add(getCodeFromAutomationElement(testparent, walker)); } } */ } } catch (Exception eNew) { txtFullCode.Text = eNew.Message; } }
private void collectAncestry( ref System.Collections.Generic.List<string> listForNodes, ref System.Collections.Generic.List<string> listForCode, AutomationElement element) { this.cleanAncestry(); System.Windows.Automation.TreeWalker walker = new System.Windows.Automation.TreeWalker( System.Windows.Automation.Condition.TrueCondition); System.Windows.Automation.AutomationElement testparent; try { testparent = element; // walker.GetParent(element); if (testparent != null && (int)testparent.Current.ProcessId > 0) { listForNodes.Add(getNodeNameFromAutomationElement(testparent)); if (testparent != AutomationElement.RootElement) { listForCode.Add(getCodeFromAutomationElement(testparent, walker)); } } while (testparent != null && (int)testparent.Current.ProcessId > 0) { testparent = walker.GetParent(testparent); if (testparent != null && (int)testparent.Current.ProcessId > 0) { listForNodes.Add(getNodeNameFromAutomationElement(testparent)); if (testparent != AutomationElement.RootElement) { listForCode.Add(getCodeFromAutomationElement(testparent, walker)); } } } } catch (Exception eNew) { this.txtFullCode.Text = eNew.Message; } }
private string getCodeFromAutomationElement( // 20131109 //IUiElement element, IUiElement element, TreeWalker walker) { string result = string.Empty; // 20140312 // string elementControlType = // element.Current.ControlType.ProgrammaticName.Substring( // element.Current.ControlType.ProgrammaticName.IndexOf('.') + 1); string elementControlType = element.GetCurrent().ControlType.ProgrammaticName.Substring( element.GetCurrent().ControlType.ProgrammaticName.IndexOf('.') + 1); // in case this element is an upper-level Pane // residing directrly under the RootElement // change type to window // i.e. Get-UiaPane - > Get-UiaWindow // since Get-UiaPane is unable to get something more than // a window's child pane control if (elementControlType == "Pane" || elementControlType == "Menu") { // 20131109 //if (walker.GetParent(element) == AutomationElement.RootElement) { // 20131118 // property to method //if ((new UiElement(walker.GetParent(element.SourceElement))) == UiElement.RootElement) { // 20140102 // if (Equals(new UiElement(walker.GetParent(element.GetSourceElement())), UiElement.RootElement)) { if (Equals(new UiElement(walker.GetParent(element.GetSourceElement() as AutomationElement)), UiElement.RootElement)) { elementControlType = "Window"; } /* if ((new UiElement(walker.GetParent(element.GetSourceElement()))) == UiElement.RootElement) { elementControlType = "Window"; } */ } string elementVerbosity = @"Get-Uia" + elementControlType; // 20140312 // try { // if (element.Current.AutomationId.Length > 0) { // elementVerbosity += (" -AutomationId '" + element.Current.AutomationId + "'"); // } // } // catch { // } // try { // if (element.Current.ClassName.Length > 0) { // elementVerbosity += (" -Class '" + element.Current.ClassName + "'"); // } // } // catch { // } // try { // if (element.Current.Name.Length > 0) { // elementVerbosity += (" -Name '" + element.Current.Name + "'"); // } // } // catch { // } try { if (element.GetCurrent().AutomationId.Length > 0) { elementVerbosity += (" -AutomationId '" + element.GetCurrent().AutomationId + "'"); } } catch { } try { if (element.GetCurrent().ClassName.Length > 0) { elementVerbosity += (" -Class '" + element.GetCurrent().ClassName + "'"); } } catch { } try { if (element.GetCurrent().Name.Length > 0) { elementVerbosity += (" -Name '" + element.GetCurrent().Name + "'"); } } catch { } result = elementVerbosity; return result; }
/// -------------------------------------------------------------------- /// <summary> /// Handles user input in the target. /// </summary> /// <param name="src">Object that raised the event.</param> /// <param name="e">Event arguments.</param> /// -------------------------------------------------------------------- private void TargetSelectionHandler( object src, AutomationEventArgs e) { _feedbackText = new StringBuilder(); // Get the name of the item, which is equivalent to its text. var sourceItem = src as AutomationElement; var selectionItemPattern = sourceItem.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern; AutomationElement sourceContainer; // Special case handling for composite controls. var treeWalker = new TreeWalker(new PropertyCondition( AutomationElement.IsSelectionPatternAvailableProperty, true)); sourceContainer = (treeWalker.GetParent( selectionItemPattern.Current.SelectionContainer) == null) ? selectionItemPattern.Current.SelectionContainer : treeWalker.GetParent( selectionItemPattern.Current.SelectionContainer); switch (e.EventId.ProgrammaticName) { case "SelectionItemPatternIdentifiers.ElementSelectedEvent": _feedbackText.Append(sourceItem.Current.Name) .Append(" of the ") .Append(sourceContainer.Current.AutomationId) .Append(" control was selected."); break; case "SelectionItemPatternIdentifiers.ElementAddedToSelectionEvent": _feedbackText.Append(sourceItem.Current.Name) .Append(" of the ") .Append(sourceContainer.Current.AutomationId) .Append(" control was added to the selection."); break; case "SelectionItemPatternIdentifiers.ElementRemovedFromSelectionEvent": _feedbackText.Append(sourceItem.Current.Name) .Append(" of the ") .Append(sourceContainer.Current.AutomationId) .Append(" control was removed from the selection."); break; } Feedback(_feedbackText.ToString()); for (var controlCounter = 0; controlCounter < TargetControls.Count; controlCounter++) { _clientApp.SetControlPropertiesText(controlCounter); _clientApp.EchoTargetControlSelections( TargetControls[controlCounter], controlCounter); _clientApp.EchoTargetControlProperties( TargetControls[controlCounter], controlCounter); } }
private void VerifyGetParent (TreeWalker tree, AutomationElement rootElement, AutomationElement expectedElement) { AutomationElement actualChild = tree.GetParent (rootElement); Assert.AreEqual (expectedElement, actualChild, String.Format ("GetParent 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, "GetParent returns a new instance"); }
public void GetParentTest () { Condition buttonCondition = new PropertyCondition (AEIds.ControlTypeProperty, ControlType.Button); TreeWalker buttonWalker = new TreeWalker (buttonCondition); AssertRaises<ArgumentNullException> ( () => buttonWalker.GetParent (null), "passing null to TreeWalker.GetParent"); VerifyGetParent (buttonWalker, button7Element, null); Condition groupCondition = new PropertyCondition (AEIds.ControlTypeProperty, ControlType.Group); TreeWalker groupWalker = new TreeWalker (groupCondition); // Test where applicable ancestor is parent VerifyGetParent (groupWalker, checkBox1Element, groupBox2Element); VerifyGetParent (groupWalker, groupBox3Element, groupBox1Element); VerifyGetParent (groupWalker, groupBox1Element, null); VerifyGetParent (TreeWalker.RawViewWalker, testFormElement, AutomationElement.RootElement); // Test where applicable ancestor is not direct parent Condition windowCondition = new PropertyCondition (AEIds.ControlTypeProperty, ControlType.Window); TreeWalker windowWalker = new TreeWalker (windowCondition); VerifyGetParent (windowWalker, groupBox3Element, testFormElement); }
private static void GetPath(AutomationElement element, Window rootWindow, TreeWalker walker, List<AutomationElement> paths) { try { var testparent = element; if (testparent != null && (int) testparent.Current.ProcessId > 0) { if (testparent != rootWindow.AutomationElement) { if (testparent.Current.ControlType.Equals(ControlType.Window)) { paths.Add(testparent); } } } while (testparent != null && (int) testparent.Current.ProcessId > 0) { testparent = walker.GetParent(testparent); if (testparent != null && (int) testparent.Current.ProcessId > 0) { if (testparent != AutomationElement.RootElement) { if (testparent.Current.ControlType.Equals(ControlType.Window)) { paths.Add(testparent); } } } } } catch (Exception eNew) { Log.WriteLog(eNew.Message + eNew.StackTrace, Stage.Erro); } }