Esempio 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, ISeleniumTestObject 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);
        }
Esempio n. 2
0
        /// <summary>
        /// Capture a screenshot during execution and associate to the testObject
        /// </summary>
        /// <param name="webDriver">The WebDriver</param>
        /// <param name="testObject">The test object to associate and log to</param>
        /// <param name="appendName">Appends a name to the end of a filename</param>
        /// <returns>Boolean if the save of the image was successful</returns>
        public static bool CaptureScreenshot(this IWebDriver webDriver, ISeleniumTestObject testObject, string appendName = "")
        {
            try
            {
                string path = string.Empty;

                if (testObject.Log is IHtmlFileLogger htmlLogger)
                {
                    htmlLogger.EmbedImage(((ITakesScreenshot)webDriver).GetScreenshot().AsBase64EncodedString);
                }
                else if (testObject.Log is IFileLogger fileLogger)
                {
                    // Calculate the file name
                    string fullpath  = fileLogger.FilePath;
                    string directory = Path.GetDirectoryName(fullpath);
                    string fileNameWithoutExtension = $"{Path.GetFileNameWithoutExtension(fullpath)}{appendName}";
                    path = CaptureScreenshot(webDriver, testObject, directory, fileNameWithoutExtension, GetScreenShotFormat());
                }
                else
                {
                    // Since this is not a file logger we will need to use a generic file name
                    path = CaptureScreenshot(webDriver, testObject, LoggingConfig.GetLogDirectory(), $"ScreenCap{appendName}", GetScreenShotFormat());
                }

                testObject.Log.LogMessage(MessageType.INFORMATION, $"Screenshot saved: {path}");
                return(true);
            }
            catch (Exception exception)
            {
                testObject.Log.LogMessage(MessageType.ERROR, $"Screenshot error: {exception}");
                return(false);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// To capture a page source during execution
        /// </summary>
        /// <param name="webDriver">The WebDriver</param>
        /// <param name="testObject">The TestObject to associate the file with</param>
        /// <param name="appendName">Appends a name to the end of a filename</param>
        /// <returns>Boolean if the save of the page source was successful</returns>
        public static bool SavePageSource(this IWebDriver webDriver, ISeleniumTestObject testObject, string appendName = "")
        {
            try
            {
                string path = string.Empty;

                // Check if we are using a file logger
                if (testObject.Log is IFileLogger logger)
                {
                    // Calculate the file name
                    string fullpath  = logger.FilePath;
                    string directory = Path.GetDirectoryName(fullpath);
                    string fileNameWithoutExtension = $"{Path.GetFileNameWithoutExtension(fullpath)}_PS{ appendName}";

                    path = SavePageSource(webDriver, testObject, directory, fileNameWithoutExtension);
                }
                else
                {
                    // Since this is not a file logger we will need to use a generic file name
                    path = SavePageSource(webDriver, testObject, LoggingConfig.GetLogDirectory(), $"PageSource{appendName}");
                }

                testObject.Log.LogMessage(MessageType.INFORMATION, $"Page Source saved: {path}");
                return(true);
            }
            catch (Exception exception)
            {
                testObject.Log.LogMessage(MessageType.ERROR, $"Page Source error: {exception}");
                return(false);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Get a unique file name that we can user for the accessibility HTML report
        /// </summary>
        /// <param name="testObject">The TestObject to associate the report with</param>
        /// <returns>A unique HTML file name, includes full path</returns>
        private static string GetAccessibilityReportPath(ISeleniumTestObject testObject)
        {
            string logDirectory = testObject.Log is IFileLogger log?Path.GetDirectoryName(log.FilePath) : LoggingConfig.GetLogDirectory();

            string reportBaseName = testObject.Log is IFileLogger logger ? $"{Path.GetFileNameWithoutExtension(logger.FilePath)}_Axe" : "AxeReport";
            string reportFile     = Path.Combine(logDirectory, $"{reportBaseName}.html");
            int    reportNumber   = 0;

            while (File.Exists(reportFile))
            {
                reportFile = Path.Combine(logDirectory, $"{reportBaseName}{reportNumber++}.html");
            }

            return(reportFile);
        }
Esempio n. 5
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, ISeleniumTestObject 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}");

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

            return(path);
        }
Esempio n. 6
0
 /// <summary>
 /// Initializes a new instance of the SeleniumSoftAssert class
 /// </summary>
 /// <param name="seleniumTestObject">The related Selenium test object</param>
 public SeleniumSoftAssert(ISeleniumTestObject seleniumTestObject)
     : base(seleniumTestObject.Log)
 {
     this.testObject = seleniumTestObject;
 }
Esempio n. 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LoginPageModel" /> class.
 /// </summary>
 /// <param name="testObject">The test object</param>
 public LoginPageModel(ISeleniumTestObject testObject) : base(testObject)
 {
 }
Esempio n. 8
0
 /// <summary>
 /// Run axe accessibility and log the results
 /// </summary>
 /// <param name="testObject">The test object which contains the web driver and logger you wish to use</param>
 /// <param name="throwOnViolation">Should violations cause and exception to be thrown</param>
 public static void CheckAccessibility(this ISeleniumTestObject testObject, bool throwOnViolation = false)
 {
     CheckAccessibility(testObject.WebDriver, testObject.Log, throwOnViolation);
 }
Esempio n. 9
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>
        /// <param name="reportTypes">What type of results do you want in the report</param>
        /// <returns>Path to the HTML report</returns>
        public static string CreateAccessibilityHtmlReport(this ISearchContext context, ISeleniumTestObject testObject, Func <AxeResult> getResults, bool throwOnViolation = false, ReportTypes reportTypes = ReportTypes.All)
        {
            // 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, reportTypes);
            }
            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 InvalidOperationException($"Accessibility violations, see: {report} for more details.");
            }

            // Throw exception if the accessibility check had any errors
            if (results.Error?.Length > 0)
            {
                throw new InvalidOperationException($"Accessibility check failure, see: {report} for more details.");
            }

            // Return report path
            return(report);
        }
Esempio n. 10
0
 /// <summary>
 /// Create a HTML accessibility report for a specific web element and all of it's children
 /// </summary>
 /// <param name="webDriver">The WebDriver</param>
 /// <param name="testObject">The TestObject to associate the report with</param>
 /// <param name="element">The WebElement you want to use as the root for your accessibility scan</param>
 /// <param name="throwOnViolation">Should violations cause and exception to be thrown</param>
 /// <param name="reportTypes">What type of results do you want in the report</param>
 /// <returns>Path to the HTML report</returns>
 public static string CreateAccessibilityHtmlReport(this IWebDriver webDriver, ISeleniumTestObject testObject, IWebElement element, bool throwOnViolation = false, ReportTypes reportTypes = ReportTypes.All)
 {
     return(CreateAccessibilityHtmlReport(element, testObject, () => webDriver.Analyze(element), throwOnViolation, reportTypes));
 }
Esempio n. 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LazyElement" /> class
 /// </summary>
 /// <param name="testObject">The base Selenium test object</param>
 /// <param name="driver">The web driver</param>
 /// <param name="locator">The 'by' selector for the element</param>
 /// <param name="userFriendlyName">A user friendly name, for logging purposes</param>
 public LazyElement(ISeleniumTestObject testObject, IWebDriver driver, By locator, [CallerMemberName] string userFriendlyName = null) : base(testObject, driver, () => driver.GetWaitDriver(), locator, userFriendlyName)
 {
 }
Esempio n. 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BaseSeleniumPageModel"/> class.
 /// </summary>
 /// <param name="testObject">The Selenium test object</param>
 protected BaseSeleniumPageModel(ISeleniumTestObject testObject)
 {
     this.TestObject       = testObject;
     this.WebDriver        = testObject.WebDriver;
     this.lazyElementStore = new Dictionary <string, LazyElement>();
 }
Esempio n. 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HomePageModel" /> class.
 /// </summary>
 /// <param name="testObject">The selenium test object</param>
 public HomePageModel(ISeleniumTestObject testObject) : base(testObject)
 {
 }