Пример #1
1
        public static CondictionScript GetBaseCondiction(AutomationElement element,Window rootWindow)
        {
            var walker =
              new System.Windows.Automation.TreeWalker(
                  System.Windows.Automation.Condition.TrueCondition);

            System.Windows.Automation.AutomationElement testparent;
            var baseCondiction = new CondictionScript();
            var paths=new List<AutomationElement>();

            GetPath(element, rootWindow, walker, paths);

            int flag = AnalyseSimpleCondiction(paths, rootWindow, baseCondiction);

            if(flag>0)
            {
                return baseCondiction;
            }
            else if(flag==0)
            {
                return new CondictionScript();
            }
            else
            {
                return null;
            }
        }
Пример #2
0
        public ITreeWalker GetCustomTreeWalker(ConditionBase condition)
        {
            var nativeCondition  = ConditionConverter.ToNative(condition);
            var nativeTreeWalker = new UIA.TreeWalker(nativeCondition);

            return(new UIA2TreeWalker(_automation, nativeTreeWalker));
        }
Пример #3
0
		static TreeWalker ()
		{
			RawViewWalker = new TreeWalker (Automation.RawViewCondition);
			InitializeRootElements ();

			ControlViewWalker = new TreeWalker (Automation.ControlViewCondition);
			ContentViewWalker = new TreeWalker (Automation.ContentViewCondition);
		}
Пример #4
0
		public void ConditionTest ()
		{
			AssertRaises<ArgumentNullException> (
				() => new TreeWalker (null),
				"passing null to TreeWalker constructor");

			Condition buttonCondition = new PropertyCondition (AEIds.ControlTypeProperty, ControlType.Button);
			TreeWalker buttonWalker = new TreeWalker (buttonCondition);
			Assert.AreEqual (buttonCondition, buttonWalker.Condition, "Condition");
		}
Пример #5
0
		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);
		}
Пример #6
0
 public InternalTreeWalker()
 {
     // TreeWalker instance as a global variable started consuming more
     // memory over a period of time, moving the code to individual methods
     // kept the memory usage low
     // Ignore Ldtpd from list of applications
     Condition condition1 = new PropertyCondition(AutomationElement.ProcessIdProperty,
         Process.GetCurrentProcess().Id);
     Condition condition2 = new AndCondition(new Condition[] {
         System.Windows.Automation.Automation.ControlViewCondition,
         new NotCondition(condition1)});
     walker = new TreeWalker(condition2);
 }
        // 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);
        }
        /// <summary>
        /// this method will asign default tree walker if there is not asigned one
        /// </summary>
        private void InitializeTreeWalker()
        {
            // if _treeWalker hasn't been assigned then use default walker (which is ControlViewWalker)
            if (this._treeWalker == null)
            {
//                this._treeWalker = TreeWalker.ControlViewWalker;

                //I am going to ignore all elements of this application
                Condition condition1 = new PropertyCondition(AutomationElement.ProcessIdProperty, Process.GetCurrentProcess().Id);
                Condition condition2 = new AndCondition(new Condition[] { System.Windows.Automation.Automation.ControlViewCondition, new NotCondition(condition1) });
                this._treeWalker = new TreeWalker(condition2);
            }
        }
Пример #9
0
        // 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)
            );
        }
Пример #10
0
        public static AutomationElement Normalize(AutomationElement element)
        {
            if (element.Current.ControlType == ControlType.Window)
                return element;

            TreeWalker walker = new TreeWalker(ConditionBuilder.ToCondition(a => a.Current.ControlType == ControlType.Window));

            return walker.Normalize(element);
        }
Пример #11
0
		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");
		}
Пример #12
0
        /// <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 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;
        }
Пример #14
0
        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;
            }
        }
Пример #15
0
 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);
 }
Пример #16
0
        private static void TestByAutomationElementAPI()
        {
            等待(1);

            var window = new TreeWalker(
                    new AndCondition
                    (
                    new PropertyCondition(AutomationElement.NameProperty, "指标管理系统"),
                    new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window)
                    )
                ).GetFirstChild(AutomationElement.RootElement);
            var page = window.FindFirst(TreeScope.Subtree,
                new AndCondition
                (
                    new PropertyCondition(AutomationElement.NameProperty, "专业字典"),
                    new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem)
                ));
            var button = page.FindFirst(TreeScope.Subtree,
                new AndCondition
                (
                    new PropertyCondition(AutomationElement.NameProperty, "添加"),
                    new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button)
                ));

            var p1 = button.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
            p1.Invoke();

            var treeGrid = page.FindFirst(TreeScope.Subtree,
                new AndCondition
                (
                    new PropertyCondition(AutomationElement.NameProperty, "专业字典"),
                    new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Tree)
                ));

            var siPattern = treeGrid.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern;
            var rowElement = siPattern.Current.GetSelection()[0];

            var cellElement = rowElement.FindFirst(TreeScope.Subtree,
                new AndCondition
                (
                    new PropertyCondition(AutomationElement.NameProperty, "专业名称"),
                    new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom)
                ));

            var value = cellElement.GetCurrentPropertyValue(AutomationElement.IsContentElementProperty);
            value = cellElement.GetCurrentPropertyValue(AutomationElement.IsContentElementProperty);
            var p2 = cellElement.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
            p2.Invoke();
            //var wpfCell = Microsoft.VisualStudio.TestTools.UITesting.UITestControlFactory.FromNativeElement(cell, "UIA") as WpfControl;
            //wpfCell.点击();

            var editingCellElement = rowElement.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "专业名称"));

            var vp = editingCellElement.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
            vp.SetValue("DDDDDDDDDDDDDDDDDDDDD");

            //var edit = Microsoft.VisualStudio.TestTools.UITesting.UITestControlFactory.FromNativeElement(editingCellElement, "UIA") as WpfEdit;
            //edit.Text = "ddddddddddd";

            //var patterns = editingCell.GetSupportedPatterns();

            return;
        }
Пример #17
0
        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;
        }
Пример #18
0
        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;
            }
        }
Пример #19
0
        /// --------------------------------------------------------------------
        /// <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);
            }
        }
Пример #20
0
		public void GetPreviousSiblingTest ()
		{
			Condition buttonCondition = new PropertyCondition (AEIds.ControlTypeProperty, ControlType.Button);
			TreeWalker buttonWalker = new TreeWalker (buttonCondition);

			AssertRaises<ArgumentNullException> (
				() => buttonWalker.GetPreviousSibling (null),
				"passing null to TreeWalker.GetPreviousSibling");

			//VerifyGetPreviousSibling (buttonWalker, button7Element, null); // TODO: scrollbar button

			VerifyGetPreviousSibling (buttonWalker, button6Element, button7Element);
			VerifyGetPreviousSibling (buttonWalker, button5Element, button6Element);
			VerifyGetPreviousSibling (buttonWalker, button4Element, button5Element);
			VerifyGetPreviousSibling (buttonWalker, button3Element, button4Element);
			VerifyGetPreviousSibling (buttonWalker, button2Element, button3Element);

			// TODO: Test how for buttonWalker, GetPreviousSibling
			//       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 PropertyCondition (AEIds.ControlTypeProperty, ControlType.Group);
			TreeWalker groupWalker = new TreeWalker (groupCondition);

			VerifyGetPreviousSibling (groupWalker, groupBox3Element, null);
			VerifyGetPreviousSibling (groupWalker, groupBox2Element, groupBox3Element);
		}
Пример #21
0
		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);
		}
        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);
        }
Пример #23
0
		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);
		}
        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
        }
Пример #25
0
        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);
            }
        }
Пример #26
0
        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;
            }
        }
Пример #27
0
        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 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);
            }
        }
Пример #29
0
		public void NormalizeTest ()
		{
			Condition groupCondition = new PropertyCondition (AEIds.ControlTypeProperty, ControlType.Group);
			TreeWalker groupWalker = new TreeWalker (groupCondition);

			AssertRaises<ArgumentNullException> (
				() => groupWalker.Normalize (null),
				"passing null to TreeWalker.Normalize");

			Assert.AreEqual (groupBox1Element,
				groupWalker.Normalize (groupBox1Element),
				"If element matches, return it");

			Assert.AreEqual (groupBox1Element,
				groupWalker.Normalize (button2Element),
				"When element does not match, return first matching ancestor");

			// This is according to MSDN:
			// http://msdn.microsoft.com/en-us/library/system.windows.automation.treewalker.normalize.aspx
			//Assert.AreEqual (AutomationElement.RootElement,
			//        groupWalker.Normalize (button1Element),
			//        "When neither elment nor ancestors match, return RootElement");

			// This is how Microsoft actually implemented it:
			Assert.IsNull (groupWalker.Normalize (button1Element),
				"When neither element nor ancestors match, return null");

			Condition noNameCondition = new PropertyCondition (AEIds.NameProperty, string.Empty);
			TreeWalker noNameWalker = new TreeWalker (noNameCondition);

			Assert.AreEqual (AutomationElement.RootElement,
				noNameWalker.Normalize (button1Element),
				"When RootElement matches, it should be returned");

			Assert.AreEqual (AutomationElement.RootElement,
				TreeWalker.RawViewWalker.Normalize (AutomationElement.RootElement),
				"When RootElement matches, it should be returned");

			Assert.AreEqual (null,
				new TreeWalker (Condition.FalseCondition).Normalize (AutomationElement.RootElement),
				"When RootElement does not match, null should be returned");
		}
Пример #30
0
        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;
            }
        }
Пример #31
0
 public UIA2TreeWalker(UIA2Automation automation, UIA.TreeWalker nativeTreeWalker)
 {
     Automation       = automation;
     NativeTreeWalker = nativeTreeWalker;
 }