Ejemplo n.º 1
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 <UIAControlType> 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.Select(ct => ct.ToString()).Aggregate((a, b) => a + ", " + b) :
                            "Unable to find the element using the alternate search strategy"));
            }

            var moreInfo = new StringBuilder();

            //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;
                        moreInfo.Append(error);
                    }
                }
            }

            //If necessary, check landmark and localized landmark types
            if (testData.LandmarkType != UIALandmarkType.Unknown)
            {
                foreach (var element in testElements)
                {
                    var five = element as IUIAutomationElement5;
                    var convertedLandmark = GetLandmarkTypeFromCode(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";
                        moreInfo.Append(error);
                    }

                    if (localizedLandmark != testData.LocalizedLandmarkType)
                    {
                        var error = "\nElement did not have the correct localized landmark type. Expected:" +
                                    testData.LocalizedLandmarkType + " Actual:" + localizedLandmark + "\n";
                        moreInfo.Append(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)
            {
                moreInfo.Append(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))
                    {
                        moreInfo.Append("\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);
                    if (additionalRequirementResult.Result != ResultType.Pass)
                    {
                        moreInfo.AppendLine(additionalRequirementResult.MoreInfo);
                    }
                }
            }
            catch (Exception ex)
            {
                moreInfo.Append("\nCaught exception during test execution, ERROR: " + ex.Message + "\nCallStack:\n" + ex.StackTrace);
            }

            var moreInfoString = moreInfo.ToString();

            if (moreInfoString != "")
            {
                return(Half(testData.TestName, moreInfoString.Trim()));
            }

            return(Pass(testData.TestName));
        }
Ejemplo 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));
        }