public void CaptureScreenshotPngFormat() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); string screenShotPath = SeleniumUtilities.CaptureScreenshot(WebDriver, TestObject, "TempTestDirectory", "TempTestFilePath", imageFormat: ScreenshotImageFormat.Png); Assert.IsTrue(File.Exists(screenShotPath), "Fail to find screenshot"); Assert.AreEqual(".Png", Path.GetExtension(screenShotPath), "The screenshot format was not in '.Png' format"); File.Delete(screenShotPath); }
public void SavedPageSourceTestObjectAssociation() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); string pageSourcePath = SeleniumUtilities.SavePageSource(WebDriver, TestObject, "TempTestDirectory", "TestObjAssocTest"); Assert.IsTrue(TestObject.ContainsAssociatedFile(pageSourcePath), "The saved page source wasn't added to the associated files"); File.Delete(pageSourcePath); }
public void WebElementToWebDriverUnwrappedDriver() { WebDriver.Navigate().GoToUrl(TestSiteAutomationUrl); IWebDriver driver = ((IWrapsDriver)WebDriver).WrappedDriver; IWebElement element = driver.FindElement(AutomationShowDialog1); IWebDriver basedriver = SeleniumUtilities.WebElementToWebDriver(element); Assert.AreEqual("OpenQA.Selenium.Chrome.ChromeDriver", basedriver.GetType().ToString()); }
public void SeleniumPageSourceFileIsCreatedDeprecated() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); #pragma warning disable CS0618 // Type or member is obsolete string pageSourcePath = SeleniumUtilities.SavePageSource(WebDriver, "TempTestDirectory", "OldSeleniumPSFile"); #pragma warning restore CS0618 // Type or member is obsolete Assert.IsTrue(File.Exists(pageSourcePath), "Failed to find Page Source"); File.Delete(pageSourcePath); }
public void CapturedScreenshotTestObjectAssociation() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); string pagePicPath = SeleniumUtilities.CaptureScreenshot(WebDriver, TestObject, "TempTestDirectory", "TestObjAssocTest"); Assert.IsTrue(TestObject.ContainsAssociatedFile(pagePicPath), "The captured screenshot wasn't added to the associated files"); File.Delete(pagePicPath); }
public void CaptureScreenshotNoExistingDirectoryDeprecated() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); #pragma warning disable CS0618 // Type or member is obsolete string screenShotPath = SeleniumUtilities.CaptureScreenshot(WebDriver, "TempTestDirectory", "OldCapScreenShotNoDir", SeleniumUtilities.GetScreenShotFormat()); #pragma warning restore CS0618 // Type or member is obsolete Assert.IsTrue(File.Exists(screenShotPath), "Fail to find screenshot"); File.Delete(screenShotPath); }
public void AccessibilityReadableResults() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); SeleniumUtilities.GetReadableAxeResults("TEST", this.WebDriver, this.WebDriver.Analyze().Violations, out string messages); Assert.IsTrue(messages.Contains("TEST check for"), "Expected header."); Assert.IsTrue(messages.Contains("Found 6 items"), "Expected to find 6 violations matches."); }
public void TryScreenshot() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); SeleniumUtilities.CaptureScreenshot(this.WebDriver, this.TestObject); string filePath = Path.ChangeExtension(((FileLogger)TestObject.Log).FilePath, ".png"); Assert.IsTrue(File.Exists(filePath), "Fail to find screenshot"); File.Delete(filePath); }
public void TryScreenshotDeprecated() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); #pragma warning disable CS0618 // Type or member is obsolete SeleniumUtilities.CaptureScreenshot(this.WebDriver, this.Log); #pragma warning restore CS0618 // Type or member is obsolete string filePath = Path.ChangeExtension(((FileLogger)this.Log).FilePath, ".Jpeg"); Assert.IsTrue(File.Exists(filePath), "Fail to find screenshot"); File.Delete(filePath); }
public void TryScreenshotImageFormat() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); SeleniumUtilities.CaptureScreenshot(this.WebDriver, this.TestObject); string filePath = Path.ChangeExtension(((IFileLogger)this.Log).FilePath, SeleniumUtilities.GetScreenShotFormat().ToString()); Assert.IsTrue(File.Exists(filePath), "Fail to find screenshot"); Assert.AreEqual(Path.GetExtension(filePath), "." + SeleniumUtilities.GetScreenShotFormat().ToString(), "The screenshot format was not in correct Format"); File.Delete(filePath); }
public void TryScreenshotWithHTMLFileLogger() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); // Create a console logger and calculate the file location HtmlFileLogger htmlFileLogger = new HtmlFileLogger(LoggingConfig.GetLogDirectory()); TestObject.Log = htmlFileLogger; // Take a screenshot SeleniumUtilities.CaptureScreenshot(this.WebDriver, this.TestObject, "Delete"); Stream fileStream = null; string logContents = string.Empty; // This will open the Log file and read in the text try { fileStream = new FileStream(htmlFileLogger.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); using (StreamReader textReader = new StreamReader(fileStream)) { fileStream = null; logContents = textReader.ReadToEnd(); } } finally { if (fileStream != null) { fileStream.Dispose(); } } // Find the base64 encoded string Regex pattern = new Regex("src='data:image/png;base64, (?<image>[^']+)'"); var matches = pattern.Match(logContents); // Try to convert the Base 64 string to find if it is a valid string try { Convert.FromBase64String(matches.Groups["image"].Value); } catch (FormatException) { Assert.Fail("image saves was not a Base64 string"); } finally { File.Delete(htmlFileLogger.FilePath); } }
public void SavePageSourceThrownExceptionDeprecated() { FileLogger tempLogger = new FileLogger { FilePath = "<>" // illegal file path }; WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); #pragma warning disable CS0618 // Type or member is obsolete bool successfullySaved = SeleniumUtilities.SavePageSource(WebDriver, tempLogger); #pragma warning restore CS0618 // Type or member is obsolete Assert.IsFalse(successfullySaved); }
public void SavePageSourceThrownException() { FileLogger tempLogger = new FileLogger { FilePath = "<>\0" // illegal file path }; TestObject.Log = tempLogger; WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); bool successfullySaved = SeleniumUtilities.SavePageSource(WebDriver, TestObject); Assert.IsFalse(successfullySaved); }
public void AccessibilityCheckVerbose() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); string filePath = ((FileLogger)Log).FilePath; SeleniumUtilities.CheckAccessibility(this.TestObject); string logContent = File.ReadAllText(filePath); Assert.IsTrue(logContent.Contains("Found 18 items"), "Expected to find 18 pass matches."); Assert.IsTrue(logContent.Contains("Found 50 items"), "Expected to find 52 inapplicable matches."); Assert.IsTrue(logContent.Contains("Found 6 items"), "Expected to find 6 violations matches."); Assert.IsTrue(logContent.Contains("Incomplete check for"), "Expected to find any incomplete matches."); }
public void AccessibilityCheckVerbose() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); string filePath = ((IFileLogger)Log).FilePath; SeleniumUtilities.CheckAccessibility(this.TestObject); string logContent = File.ReadAllText(filePath); SoftAssert.Assert(() => Assert.IsTrue(logContent.Contains("Found 20 items"), "Expected to find 20 pass matches.")); SoftAssert.Assert(() => Assert.IsTrue(logContent.Contains("Found 62 items"), "Expected to find 62 inapplicable matches.")); SoftAssert.Assert(() => Assert.IsTrue(logContent.Contains("Found 6 items"), "Expected to find 6 violations matches.")); SoftAssert.Assert(() => Assert.IsTrue(logContent.Contains("Found 0 items"), "Expected to find 0 incomplete matches.")); SoftAssert.FailTestIfAssertFailed(); }
public void TryScreenshotWithConsoleLogger() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); // Create a console logger and calculate the file location ConsoleLogger consoleLogger = new ConsoleLogger(); TestObject.Log = consoleLogger; string expectedPath = Path.Combine(LoggingConfig.GetLogDirectory(), "ScreenCapDelete.Png"); // Take a screenshot SeleniumUtilities.CaptureScreenshot(this.WebDriver, this.TestObject, "Delete"); // Make sure we got the screenshot and than cleanup Assert.IsTrue(File.Exists(expectedPath), $"Fail to find screenshot at {expectedPath}"); File.Delete(expectedPath); }
public void SeleniumPageSourceWithConsoleLoggerDeprecated() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); // Create a console logger and calculate the file location ConsoleLogger consoleLogger = new ConsoleLogger(); string expectedPath = Path.Combine(LoggingConfig.GetLogDirectory(), "PageSourceOldConsole.txt"); // Take a screenshot #pragma warning disable CS0618 // Type or member is obsolete SeleniumUtilities.SavePageSource(this.WebDriver, consoleLogger, "OldConsole"); #pragma warning restore CS0618 // Type or member is obsolete // Make sure we got the screenshot and than cleanup Assert.IsTrue(File.Exists(expectedPath), "Fail to find screenshot"); File.Delete(expectedPath); }
public void AccessibilityPassesCheckRespectsMessageLevel() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); string filePath = Path.GetDirectoryName(((IFileLogger)Log).FilePath); FileLogger fileLogger = new FileLogger(filePath, this.TestContext.TestName + ".txt", MessageType.INFORMATION); try { SeleniumUtilities.CheckAccessibilityPasses(TestObject.WebDriver, fileLogger, MessageType.SUCCESS); string logContent = File.ReadAllText(fileLogger.FilePath); SoftAssert.Assert(() => Assert.IsTrue(!logContent.Contains("Violations check"), "Did not expect violation check")); SoftAssert.Assert(() => Assert.IsTrue(!logContent.Contains("Inapplicable check"), "Did not expect inapplicable check")); SoftAssert.Assert(() => Assert.IsTrue(!logContent.Contains("Incomplete check"), "Did not expect incomplete check")); SoftAssert.Assert(() => Assert.IsTrue(logContent.Contains("Passes check"), "Did expect pass check")); } finally { File.Delete(fileLogger.FilePath); } }
public void SetTimeoutsGetWaitDriver() { var driver = WebDriverFactory.GetDefaultBrowser(); var newDriver = WebDriverFactory.GetDefaultBrowser(); try { SeleniumUtilities.SetTimeouts(driver); WebDriverWait wait = SeleniumConfig.GetWaitDriver(newDriver); Assert.AreEqual(wait.Timeout.TotalMilliseconds.ToString(), Config.GetValueForSection("SeleniumMaqs", "BrowserTimeout", "0")); } finally { try { driver?.KillDriver(); } finally { newDriver?.KillDriver(); } } }
/// <summary> /// Wait for the page to load /// </summary> /// <returns>True if the page finished loading</returns> /// <example> /// <code source = "../SeleniumUnitTesting/SeleniumUnitTest.cs" region="WaitUntilPageLoad" lang="C#" /> /// </example> public bool UntilPageLoad() { DateTime start = DateTime.Now; string source = string.Empty; IWebDriver driver = SeleniumUtilities.SearchContextToWebDriver(this.searchItem); do { // Give the system a second before checking if the page is updating Thread.Sleep(TimeSpan.FromSeconds(1)); try { // Find any element driver.FindElement(By.CssSelector("*")); // Get the page source string newSource = driver.PageSource; // Make sure the souce has not changed and it actully has content if (!string.IsNullOrEmpty(source) && source.Equals(newSource)) { return(true); } source = newSource; } catch { // Could be several exceptions - Don't really care as it may just be the page loading } }while ((DateTime.Now - start) < this.webDriverWait.Timeout); // Page was still loading return(false); }
public void AccessibilityCheckRespectsMessageLevel() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); string filePath = Path.GetDirectoryName(((IFileLogger)Log).FilePath); FileLogger fileLogger = new FileLogger(filePath, "LevTest.txt", MessageType.WARNING); try { SeleniumUtilities.CheckAccessibility(TestObject.WebDriver, fileLogger); string logContent = File.ReadAllText(fileLogger.FilePath); Assert.IsTrue(!logContent.Contains("Passes check for"), "Did not expect expected to check for pass matches."); Assert.IsTrue(!logContent.Contains("Inapplicable check for"), "Did not expect expected to check for inapplicable matches."); Assert.IsTrue(!logContent.Contains("Incomplete check for"), "Did not expected to find any incomplete matches."); Assert.IsTrue(logContent.Contains("Found 6 items"), "Expected to find 6 violations matches."); } finally { File.Delete(fileLogger.FilePath); } }
public void GetImageFormatFromConfig() { Assert.AreEqual(ScreenshotImageFormat.Png, SeleniumUtilities.GetScreenShotFormat(), "The Incorrect Image Format was returned, expected: " + Config.GetGeneralValue("ImageFormat")); }
/// <summary> /// Get the low level, none event firing, web driver /// </summary> /// <param name="searchContext">Web driver or element</param> /// <returns>the underlying web driver</returns> public static IWebDriver GetLowLevelDriver(ISearchContext searchContext) { IWebDriver driver = SeleniumUtilities.SearchContextToWebDriver(searchContext); return(driver is EventFiringWebDriver ? ((EventFiringWebDriver)driver).WrappedDriver : driver); }
public void CaptureScreenshotNoExistingDirectory() { WebDriver.Navigate().GoToUrl(TestSiteUrl); WebDriver.Wait().ForPageLoad(); string screenShotPath = SeleniumUtilities.CaptureScreenshot(WebDriver, TestObject, "TempTestDirectory", "CapScreenShotNoDir", SeleniumUtilities.GetScreenShotFormat()); Assert.IsTrue(File.Exists(screenShotPath), "Fail to find screenshot"); File.Delete(screenShotPath); }
/// <summary> /// Return the wait extension /// </summary> /// <param name="searchContext">Web driver or element</param> /// <returns>The wait extension</returns> public static Wait Wait(this ISearchContext searchContext) { IWebDriver driver = (searchContext is IWebDriver) ? (IWebDriver)searchContext : SeleniumUtilities.WebElementToWebDriver((IWebElement)searchContext); return(new Wait(searchContext, GetWaitDriver(driver))); }