Exemplo n.º 1
0
        /// <summary>
        /// This searches the UI tree to find an element with the given tag.
        /// </summary>
        /// <param name="browserElement">The browser element to search. </param>
        /// <param name="controlType">The tag to search for</param>
        /// <param name="searchStrategy">An alternative search strategy for elements which are not found by their controltype</param>
        /// <param name="foundControlTypes">A list of all control types found on the page, for error reporting</param>
        /// <returns>The elements found which match the tag given</returns>
        public static List <IUIAutomationElement> SearchChildren(
            IUIAutomationElement browserElement,
            string controlType,
            Func <IUIAutomationElement, bool> searchStrategy,
            out HashSet <string> foundControlTypes)
        {
            var uia = new CUIAutomation8();

            var walker   = uia.RawViewWalker;
            var tosearch = new List <Tuple <IUIAutomationElement, int> >();
            var toreturn = new List <IUIAutomationElement>();

            foundControlTypes = new HashSet <string>();

            //We use a 0 here to signify the depth in the BFS search tree. The root element will have depth of 0.
            tosearch.Add(new Tuple <IUIAutomationElement, int>(browserElement, 0));
            var automationElementConverter = new ElementConverter();

            while (tosearch.Any(e => e.Item2 < RECURSIONDEPTH))
            {
                var current      = tosearch.First().Item1;
                var currentdepth = tosearch.First().Item2;

                var convertedRole = automationElementConverter.GetElementNameFromCode(current.CurrentControlType);
                foundControlTypes.Add(convertedRole);

                if (searchStrategy == null ? convertedRole.Equals(controlType, StringComparison.OrdinalIgnoreCase) : searchStrategy(current))
                {
                    toreturn.Add(current);
                }
                else
                {
                    for (var child = walker.GetFirstChildElement(current); child != null; child = walker.GetNextSiblingElement(child))
                    {
                        tosearch.Add(new Tuple <IUIAutomationElement, int>(child, currentdepth + 1));
                    }
                }

                tosearch.RemoveAt(0);
            }
            return(toreturn);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This handles most of the work of the test cases.
        ///
        /// N.B. all the test case results are returned in pairs, since we need to be
        /// able to give half scores for certain results.
        /// </summary>
        /// <param name="testData">An object which stores information about the
        /// expected results</param>
        /// <param name="driverManager"></param>
        /// <returns></returns>
        internal override IEnumerable <TestCaseResult> TestElement(TestData testData)
        {
            //Find the browser
            var browserElement = EdgeA11yTools.FindBrowserDocument(0);

            if (browserElement == null)
            {
                return(Fail(testData._TestName, "Unable to find the browser"));
            }

            //Find elements using ControlType or the alternate search strategy
            HashSet <string> foundControlTypes;
            var testElements = EdgeA11yTools.SearchChildren(browserElement, testData._ControlType, testData._SearchStrategy, out foundControlTypes);

            if (testElements.Count == 0)
            {
                return(Fail(testData._TestName, testData._SearchStrategy == null ?
                            "Unable to find the element, found these instead: " + foundControlTypes.Aggregate((a, b) => a + ", " + b):
                            "Unable to find the element using the alternate search strategy"));
            }

            new CUIAutomation8().AddStructureChangedEventHandler(browserElement, TreeScope.TreeScope_Subtree, null, new StructureChangedHandler());

            string result = "";
            //This is used if the test passes but there is something to report
            string note             = null;
            var    elementConverter = new ElementConverter();

            //If necessary, check localized control type
            if (testData._LocalizedControlType != null)
            {
                foreach (var element in testElements)
                {
                    if (!element.CurrentLocalizedControlType.Equals(testData._LocalizedControlType, StringComparison.OrdinalIgnoreCase))
                    {
                        var error = "\nElement did not have the correct localized control type. Expected:" +
                                    testData._LocalizedControlType + " Actual:" + element.CurrentLocalizedControlType;
                        if (!result.Contains(error))
                        {
                            result += error;
                        }
                    }
                }
            }

            //If necessary, check landmark and localized landmark types
            if (testData._LandmarkType != null)
            {
                foreach (var element in testElements)
                {
                    var five = element as IUIAutomationElement5;
                    var convertedLandmark = elementConverter.GetElementNameFromCode(five.CurrentLandmarkType);
                    var localizedLandmark = five.CurrentLocalizedLandmarkType;

                    if (convertedLandmark != testData._LandmarkType)
                    {
                        var error = "\nElement did not have the correct landmark type. Expected:" +
                                    testData._LandmarkType + " Actual:" + convertedLandmark + "\n";
                        if (!result.Contains(error))
                        {
                            result += error;
                        }
                    }

                    if (localizedLandmark != testData._LocalizedLandmarkType)
                    {
                        var error = "\nElement did not have the correct localized landmark type. Expected:" +
                                    testData._LocalizedLandmarkType + " Actual:" + localizedLandmark + "\n";
                        if (!result.Contains(error))
                        {
                            result += error;
                        }
                    }
                }
            }

            //If necessary, naming and descriptions
            //This is done "out of order" since the keyboard checks below invalidate the tree
            if (testData._requiredNames != null || testData._requiredDescriptions != null)
            {
                result += CheckElementNames(testElements,
                                            testData._requiredNames ?? new List <string>(),
                                            testData._requiredDescriptions ?? new List <string>());
            }

            //If necessary, check keboard accessibility
            var tabbable = EdgeA11yTools.TabbableIds(_driverManager);

            if (testData._KeyboardElements != null && testData._KeyboardElements.Count > 0)
            {
                foreach (var e in testData._KeyboardElements)
                {
                    if (!tabbable.Contains(e))
                    {
                        result += "\nCould not access element with id: '" + e + "' by tab";
                    }
                }
            }

            try
            {
                //If necessary, check any additional requirements
                if (testData._AdditionalRequirement != null)
                {
                    testElements = EdgeA11yTools.SearchChildren(browserElement, testData._ControlType, testData._SearchStrategy, out foundControlTypes);
                    var additionalRequirementResult = testData._AdditionalRequirement(testElements, _driverManager, tabbable).Trim();
                    if (additionalRequirementResult != "")
                    {
                        result += "\n" + additionalRequirementResult;
                    }
                }
            }
            catch (Exception ex)
            {
                result += "\nCaught exception during test execution, ERROR: " + ex.Message + "\nCallStack:\n" + ex.StackTrace;
            }

            if (result != "")
            {
                return(Half(testData._TestName, result.Trim()));
            }

            return(Pass(testData._TestName, note));
        }