/// <summary> /// Attach an associated file to the text context /// </summary> private void AttachAssociatedFile(string path) { // You can only attach files to VS Unit tests so check that first if (this.IsVSTest()) { this.TestContext.AddResultFile(path); } else { NUnitTestContext.AddTestAttachment(path); } }
private string TakeScreenshot(string screenshotName) { int attempt = 0; while (attempt < 5) { attempt++; IntegrationTestBase.Log($"Saving error screenshot: {screenshotName} for test run {TestRunTimestamp}"); string path = CreatePngPath(screenshotName, this.GetType().Name); IntegrationTestBase.Log($"{path}"); try { Screenshot screen = this.Screenshooter.GetScreenshot(); screen.SaveAsFile(path, ScreenshotImageFormat.Png); TestContext.AddTestAttachment(path); return(path); } catch (UnhandledAlertException alertException) { IntegrationTestBase.Log($"Attempt {attempt} - Unexpected alert {screenshotName} for test run {TestRunTimestamp}.\r\n{alertException}"); IAlert unexpectedAlert = this.Driver.SwitchTo().Alert(); IntegrationTestBase.Log($"Attempt {attempt} - Alert text: [{unexpectedAlert?.Text}]"); unexpectedAlert?.Dismiss(); try { Screenshot screen = this.Screenshooter.GetScreenshot(); screen.SaveAsFile(path, ScreenshotImageFormat.Png); } catch (Exception screenshotError) { IntegrationTestBase.Log($"Attempt {attempt} - Error while saving screenshot after dismissing alert: {screenshotError}"); } } catch (Exception screenshotError) { IntegrationTestBase.Log($"Attempt {attempt} - Error while saving screenshot: {screenshotError}"); } } return("Failed to take screenshot"); }
public void NoneExistentFileThrowsFileNotFoundException() { Assert.That(() => TestContext.AddTestAttachment("NotAFile.txt"), Throws.InstanceOf <FileNotFoundException>()); }
public void InvalidFilePathsThrowsArgumentException(string filePath) { Assert.That(() => TestContext.AddTestAttachment(filePath), Throws.InstanceOf <ArgumentException>()); }
public void FilePathAndDescriptionDoesNotThrow() { Assert.That(() => TestContext.AddTestAttachment(_tempFilePath, "Description"), Throws.Nothing); }
public void FilePathOnlyDoesNotThrow() { Assert.That(() => TestContext.AddTestAttachment(_tempFilePath), Throws.Nothing); }