Exemplo n.º 1
0
        /// <summary>
        /// To capture Page Source during execution
        /// </summary>
        /// <param name="webDriver">The WebDriver</param>
        /// <param name="testObject">The TestObject to associate the file with</param>
        /// <param name="directory">The directory file path</param>
        /// <param name="fileNameWithoutExtension">Filename without extension</param>
        /// <returns>Path to the log file</returns>
        public static string SavePageSource(this IWebDriver webDriver, SeleniumTestObject testObject, string directory, string fileNameWithoutExtension)
        {
            // Save the current page source into a string
            string pageSource = webDriver.PageSource;

            // Make sure the directory exists
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            // Calculate the file name
            string path = Path.Combine(directory, fileNameWithoutExtension + ".txt");

            // Create new instance of Streamwriter and Auto Flush after each call
            StreamWriter writer = new StreamWriter(path, false)
            {
                AutoFlush = true
            };

            // Write page source to a new file
            writer.Write(pageSource);
            writer.Close();
            testObject.AddAssociatedFile(path);
            return(path);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a HTML accessibility report
        /// </summary>
        /// <param name="context">The scan context, this is either a web driver or web element</param>
        /// <param name="testObject">The TestObject to associate the report with</param>
        /// <param name="getResults">Function for getting the accessibility scan results</param>
        /// <param name="throwOnViolation">Should violations cause and exception to be thrown</param>
        public static void CreateAccessibilityHtmlReport(this ISearchContext context, SeleniumTestObject testObject, Func <AxeResult> getResults, bool throwOnViolation = false)
        {
            // If we are using a lazy element go get the raw element instead
            LazyElement raw = context as LazyElement;

            if (raw != null)
            {
                context = ((LazyElement)context).GetRawExistingElement();
            }

            // Check to see if the logger is not verbose and not already suspended
            bool restoreLogging = testObject.Log.GetLoggingLevel() != MessageType.VERBOSE && testObject.Log.GetLoggingLevel() != MessageType.SUSPENDED;

            AxeResult results;
            string    report = GetAccessibilityReportPath(testObject);

            testObject.Log.LogMessage(MessageType.INFORMATION, "Running accessibility check");

            try
            {
                // Suspend logging if we are not verbose or already suspended
                if (restoreLogging)
                {
                    testObject.Log.SuspendLogging();
                }

                results = getResults();
                context.CreateAxeHtmlReport(results, report);
            }
            finally
            {
                // Restore logging if we suspended it
                if (restoreLogging)
                {
                    testObject.Log.ContinueLogging();
                }
            }

            // Add the report
            testObject.AddAssociatedFile(report);
            testObject.Log.LogMessage(MessageType.INFORMATION, $"Ran accessibility check and created HTML report: {report} ");

            // Throw exception if we found violations and we want that to cause an error
            if (throwOnViolation && results.Violations.Length > 0)
            {
                throw new ApplicationException($"Accessibility violations, see: {report} for more details.");
            }

            // Throw exception if the accessibility check had any errors
            if (results.Error.Length > 0)
            {
                throw new ApplicationException($"Accessibility check failure, see: {report} for more details.");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// To capture a screenshot during execution
        /// </summary>
        /// <param name="webDriver">The WebDriver</param>
        /// <param name="testObject">The test object to associate the screenshot with</param>
        /// <param name="directory">The directory file path</param>
        /// <param name="fileNameWithoutExtension">Filename without extension</param>
        /// <param name="imageFormat">Optional Screenshot Image format parameter; Default imageFormat is PNG</param>
        /// <returns>Path to the log file</returns>
        public static string CaptureScreenshot(this IWebDriver webDriver, SeleniumTestObject testObject, string directory, string fileNameWithoutExtension, ScreenshotImageFormat imageFormat = ScreenshotImageFormat.Png)
        {
            Screenshot screenShot = ((ITakesScreenshot)webDriver).GetScreenshot();

            // Make sure the directory exists
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            // Calculate the file name
            string path = Path.Combine(directory, fileNameWithoutExtension + "." + imageFormat.ToString());

            // Save the screenshot
            screenShot.SaveAsFile(path, imageFormat);
            testObject.AddAssociatedFile(path);

            return(path);
        }