public void PerfTimerCollectionFilesAreAddedToAssociatedFiles() { // get log path string logFilePath = ((IFileLogger)this.Log).FilePath; this.PerfTimerCollection.StartTimer("testTimer"); this.PerfTimerCollection.StopTimer("testTimer"); this.PerfTimerCollection.Write(this.Log); string perfTimerLogPath = Path.Combine(LoggingConfig.GetLogDirectory(), this.PerfTimerCollection.FileName); this.Teardown(); // test that performance timer file path is written to the log using (StreamReader sr = File.OpenText(logFilePath)) { string[] lines = File.ReadAllLines(logFilePath); for (int x = 0; x < lines.Length - 1; x++) { if (lines[x] == "GENERIC:\tList of Associated Files: ") { Assert.AreEqual(perfTimerLogPath, lines[x + 1]); } } } }
public void SoftAssertIsFalseTest() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertUnitTests.SoftAssertIsFalseTest", MessageType.GENERIC, true)); softAssert.IsFalse(2 == 1, "Test"); softAssert.FailTestIfAssertFailed(); }
public void HomePageWithComplexReport() { string reportPath = Path.Combine(LoggingConfig.GetLogDirectory(), "HomePageWithComplexReport.html"); string rawResults = Path.Combine(LoggingConfig.GetLogDirectory(), "HomePageWithComplexReport.json"); // Get to home page LoginPageModel page = new LoginPageModel(this.TestObject); page.OpenLoginPage(); page.LoginWithValidCredentials(Config.GetGeneralValue("User"), Config.GetGeneralValue("Pass")); // Setup custom rules AxeBuilder builder = new AxeBuilder(WebDriver) .Exclude("#HomePage") .WithOutputFile(rawResults) .DisableRules("landmark-one-main", "page-has-heading-one"); // Reprot WebDriver.CreateAxeHtmlReport(builder.Analyze(), reportPath); // Check if there were any violations if (!File.ReadAllText(reportPath).Contains("Violation: 0")) { TestObject.AddAssociatedFile(reportPath); TestObject.AddAssociatedFile(rawResults); Assert.Fail($"Failed violation check see {reportPath} for more details."); } }
/// <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); } }
/// <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); } }
public void AcceptNUnitAsserts() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertUnitTests.AcceptNUnitAsserts")); softAssert.Assert(() => NUnit.Framework.Assert.AreEqual("a", "a"), "1"); softAssert.FailTestIfAssertFailed(); }
/// <summary> /// To capture a screenshot during execution /// </summary> /// <param name="webDriver">The WebDriver</param> /// <param name="log">The logger being used</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> /// <example> /// <code source = "../SeleniumUnitTesting/SeleniumUnitTest.cs" region="CaptureScreenshot" lang="C#" /> /// </example> public static bool CaptureScreenshot(this IWebDriver webDriver, Logger log, string appendName = "") { try { string path = string.Empty; // Check if we are using a file logger if (!(log is FileLogger)) { // Since this is not a file logger we will need to use a generic file name path = CaptureScreenshot(webDriver, LoggingConfig.GetLogDirectory(), "ScreenCap" + appendName, GetScreenShotFormat()); } else { // Calculate the file name string fullpath = ((FileLogger)log).FilePath; string directory = Path.GetDirectoryName(fullpath); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullpath) + appendName; path = CaptureScreenshot(webDriver, directory, fileNameWithoutExtension, GetScreenShotFormat()); } log.LogMessage(MessageType.INFORMATION, "Screenshot saved: " + path); return(true); } catch (Exception exception) { log.LogMessage(MessageType.ERROR, "Screenshot error: {0}", exception.ToString()); return(false); } }
/// <summary> /// To capture a page source during execution /// </summary> /// <param name="appiumDriver">The AppiumDriver</param> /// <param name="testObject">The TestObject to associate the file and log 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 AppiumDriver <IWebElement> appiumDriver, AppiumTestObject testObject, string appendName = "") { try { string path = string.Empty; // Check if we are using a file logger if (!(testObject.Log is FileLogger)) { // Since this is not a file logger we will need to use a generic file name path = SavePageSource(appiumDriver, testObject, LoggingConfig.GetLogDirectory(), "PageSource" + appendName); } else { // Calculate the file name string fullpath = ((FileLogger)testObject.Log).FilePath; string directory = Path.GetDirectoryName(fullpath); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullpath) + "_PS" + appendName; path = SavePageSource(appiumDriver, testObject, directory, fileNameWithoutExtension); } testObject.Log.LogMessage(MessageType.INFORMATION, "Page Source saved: " + path); return(true); } catch (Exception exception) { testObject.Log.LogMessage(MessageType.ERROR, "Page Source error: {0}", exception.ToString()); return(false); } }
public void SoftAssertFailTest() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertUnitTests.SoftAssertFailTest")); softAssert.AreEqual("Yes", "No", "Utilities Soft Assert", "Message is not equal"); softAssert.AreEqual("Yes", "NoAgain", "Utilities Soft Assert 2"); softAssert.FailTestIfAssertFailed(); }
public void CapturesVSAssertFail() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertUnitTests.RespectVSFailsFails")); softAssert.Assert(() => Assert.AreEqual("a", "b"), "2"); softAssert.FailTestIfAssertFailed(); }
public void SoftAssertAssertFailsWithPassingAction() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertAssertFailsWithPassingAction")); softAssert.AssertFails(() => Assert.IsTrue(true), typeof(AggregateException), "assertName"); softAssert.FailTestIfAssertFailed(); }
public void CapturesNUnitAssertFail() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertUnitTests.RespectNUnitFails")); softAssert.Assert(() => NUnit.Framework.Assert.AreEqual("a", "b"), "1"); softAssert.FailTestIfAssertFailed(); }
public void SoftAssertAssertMethodWithFailureMessage() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertAssertMethodWithFailureMessage")); softAssert.Assert(() => Assert.Fail(), "SoftAssertName", "Failure Message"); softAssert.FailTestIfAssertFailed(); }
public void TestHierarchicalHtmlFileLogger(string logLevel, Dictionary <string, int> levels) { HtmlFileLogger logger = new HtmlFileLogger(LoggingConfig.GetLogDirectory(), this.GetFileName("TestHierarchicalHtmlFileLogger" + logLevel, "html"), MessageType.GENERIC, true); this.TestHierarchicalLogging(logger, logger.FilePath, logLevel, levels); File.Delete(logger.FilePath); }
public void SoftAssertActionWithEmptyAssertionName() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertActionWithEmptyAssertionName")); softAssert.Assert(() => Assert.IsTrue(true), string.Empty); softAssert.FailTestIfAssertFailed(); }
public void SoftAssertManuallySetExpectedAssertsFails() { SoftAssert softAssert = new SoftAssert( new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertManuallySetExpectedAssertsFails")); softAssert.AddExpectedAsserts("one"); softAssert.FailTestIfAssertFailed(); }
public void FileLoggerConstructorCreateDirectory() { FileLogger logger = new FileLogger(Path.Combine(LoggingConfig.GetLogDirectory(), "FileLoggerCreateDirectory"), "FileLoggerCreateDirectory", MessageType.GENERIC, true); logger.LogMessage(MessageType.WARNING, "Test to ensure that the file in the created directory can be written to."); Assert.IsTrue(File.ReadAllText(logger.FilePath).Contains("Test to ensure that the file in the created directory can be written to.")); File.Delete(logger.FilePath); Directory.Delete(Path.GetDirectoryName(logger.FilePath)); }
public void SoftAssertIsTrueTestFailure() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertUnitTests.SoftAssertFailTest", MessageType.GENERIC, true)); softAssert.IsTrue(1 == 2, "Test"); softAssert.IsTrue(1 == 2, "Test1"); softAssert.IsTrue(true, "Test2"); softAssert.FailTestIfAssertFailed(); }
public void SoftAssertDidFailCheck() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertUnitTests.SoftAssertIsTrueTest", MessageType.GENERIC, true)); softAssert.IsTrue(true, "Test1"); Assert.IsFalse(softAssert.DidSoftAssertsFail()); softAssert.IsTrue(1 == 2, "Test2"); Assert.IsTrue(softAssert.DidSoftAssertsFail()); }
/// <summary> /// Checks to see if the folder exists /// </summary> /// <param name="createIfNotExist">Should we create the folder if it does not exist</param> /// <returns>Boolean if folder exists</returns> public static bool DoesFolderExist(bool createIfNotExist = true) { bool exists = Directory.Exists(Path.GetDirectoryName(LoggingConfig.GetLogDirectory())); if (!exists && createIfNotExist) { Directory.CreateDirectory(Path.GetDirectoryName(LoggingConfig.GetLogDirectory())); } return(exists); }
public void SoftAssertVerifyCheckForFailures() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertUnitTests.SoftAssertVerifyCheckForFailures")); softAssert.AreEqual("Yes", "Yes", "Utilities Soft Assert", "Message is not equal"); softAssert.FailTestIfAssertFailed(); Assert.IsTrue(softAssert.DidUserCheck()); softAssert.AreEqual("Yes", "Yes", "Utilities Soft Assert", "Message is not equal"); Assert.IsFalse(softAssert.DidUserCheck()); }
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 TestHierarchicalConsoleLogger(string logLevel, Dictionary <string, int> levels) { // Calculate a file path string path = Path.Combine(LoggingConfig.GetLogDirectory(), this.GetFileName("TestHierarchicalConsoleLogger" + logLevel, "txt")); // Pipe the console to this file using (ConsoleCopy consoleCopy = new ConsoleCopy(path)) { ConsoleLogger console = new ConsoleLogger(); this.TestHierarchicalLogging(console, path, logLevel, levels); } File.Delete(path); }
/// <summary> /// Cleanup the files generated /// </summary> /// <param name="loggingFolderExistsBeforeRun">If the folder existed before the test, we keep it</param> public static void Cleanup(bool loggingFolderExistsBeforeRun) { string directory = Path.GetDirectoryName(LoggingConfig.GetLogDirectory()); try { if (!loggingFolderExistsBeforeRun) { Directory.Delete(directory, true); } else { if (Directory.Exists(directory)) { string unitTestBaseName = "*UtilitiesUnitTesting.*"; foreach (FileInfo f in new DirectoryInfo(directory).GetFiles(unitTestBaseName + ".txt")) { f.Delete(); } foreach (FileInfo f in new DirectoryInfo(directory).GetFiles(unitTestBaseName + ".rtf")) { f.Delete(); } foreach (FileInfo f in new DirectoryInfo(directory).GetFiles(unitTestBaseName + ".png")) { f.Delete(); } foreach (FileInfo f in new DirectoryInfo(directory).GetFiles(unitTestBaseName + ".html")) { f.Delete(); } foreach (FileInfo f in new DirectoryInfo(directory).GetFiles(unitTestBaseName + ".xml")) { f.Delete(); } } } } catch (Exception e) { Console.WriteLine(StringProcessor.SafeFormatter("Problems occured while cleaning up after tests:\r\n{0}{1}{2}", e.Message, "\r\n\r\n", e.StackTrace)); } }
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 LoginSubPageSubElementWithReport() { string reportPath = Path.Combine(LoggingConfig.GetLogDirectory(), "LoginSubPageWithReport.html"); LoginPageModel page = new LoginPageModel(this.TestObject); page.OpenLoginPage(); var jumbotron = WebDriver.FindElement(By.CssSelector(".jumbotron")) as IWrapsElement; WebDriver.CreateAxeHtmlReport(jumbotron.WrappedElement, reportPath); if (!File.ReadAllText(reportPath).Contains("Violation: 0")) { TestObject.AddAssociatedFile(reportPath); Assert.Fail($"Failed violation check see {reportPath} for more details."); } }
public void LoginPageWithReport() { string reportPath = Path.Combine(LoggingConfig.GetLogDirectory(), "LoginPageWithReport.html"); LoginPageModel page = new LoginPageModel(this.TestObject); page.OpenLoginPage(); AxeResult result = WebDriver.Analyze(); WebDriver.CreateAxeHtmlReport(result, reportPath); if (!string.IsNullOrEmpty(result.Error) || result.Violations.Length != 0) { TestObject.AddAssociatedFile(reportPath); Assert.Fail($"Failed error and/or violation check see {reportPath} for more details."); } }
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); }
/// <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, SeleniumTestObject testObject, string appendName = "") { try { string path = string.Empty; var htmlLogger = testObject.Log as HtmlFileLogger; var fileLogger = testObject.Log as FileLogger; // Check if we are using an HTMl logger if (htmlLogger != null) { var writer = new StreamWriter(htmlLogger.FilePath, true); // Since this is a HTML File logger we need to add a card with the image in it writer.WriteLine(StringProcessor.SafeFormatter( "<div class='collapse col-12 show' data-logtype='IMAGE'><div class='card'><div class='card-body'><h6 class='card-subtitle mb-1'>2020-01-16 18:57:47.184-05:00</h6></div><a class='pop'><img class='card-img-top rounded' src='data:image/png;base64, {0}'style='width: 200px;'></a></div></div>", ((ITakesScreenshot)webDriver).GetScreenshot().AsBase64EncodedString)); writer.Flush(); writer.Close(); } // Check if we are using a file logger else if (fileLogger != null) { // Calculate the file name string fullpath = ((FileLogger)testObject.Log).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.ToString()}"); return(false); } }
public void SoftAssertFailsInvalidTest() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertUnitTests.SoftAssertFailsInvalidTest")); softAssert.AssertFails(() => this.MethodThrowsNullException(), typeof(NotImplementedException), "Assert Method Throws Explicit Exception", "Failed to assert that method threw a NotImplementedException"); int one = 1; softAssert.AssertFails( () => { one = 0; var result = 9 / one; }, typeof(NullReferenceException), "Assert dividing by zero throws a null reference", "Failed to assert that we couldn't divide by zero"); softAssert.FailTestIfAssertFailed(); }