3

We want to attach screenshots to the Test Attachment in Azure pipeline. Currently, we use

  • .NET 4.5.2
  • Selenium.WebDriver 3.141
  • Selenium.Chrome.WebDriver
  • Nunit 3.12.0
  • Specflow.Nunit 2.4.0

It is similar with the follwing example but we use NUnit rather than MSTest https://learn.microsoft.com/en-us/azure/devops/pipelines/test/collect-screenshots-and-video?view=azure-devops#collect-screenshots-logs-and-attachments

When run the program in VS2017, the screenshots are accessible from the test report. Also, we can see the screenshots in the azure build output.

Here is the code:

string fileName = string.Format("Screenshot_" + DateTime.Now.ToString("dd-MM-yyyy-hhmm-ss") + ".jpg");

var artifactDirectory = Directory.GetCurrentDirectory();

ITakesScreenshot takesScreenshot = _driver as ITakesScreenshot;

if (takesScreenshot != null)
{
    var screenshot = takesScreenshot.GetScreenshot();

    string screenshotFilePath = Path.Combine(artifactDirectory, fileName);

    screenshot.SaveAsFile(screenshotFilePath, ScreenshotImageFormat.Jpeg);
    TestContext.AddTestAttachment(screenshotFilePath, "screenshot");
    Console.WriteLine($"Screenshot: {new Uri(screenshotFilePath)}");
}

Visual Studio Test step in Azure pipeline

Visual Studio Test step

upload test attachments

After the build runs, there is no attachment

no attachments

Any help would be appreciated.

Tung Nguyen
  • 574
  • 8
  • 16
  • You can try to use private agent to run and see if the results are the same. In addition ,you can set `system.debug=true` to get more detailed log,in the vstest log,you can see the `attachment location` which vstest looks for attachments in. –  Feb 27 '20 at 05:40
  • Only difference in the Azure step between yours and mine is the _Test results folder path. I have _$(Agent.TempDirectory)\TestResults_ which is the default one. Could you try with it? – Jonah Feb 27 '20 at 09:02

1 Answers1

2

Coming in a bit late on this one, but who knows, this might be helpful to someone running into similar issues.

I cannot see your entire code, but the reason this might happen could be because you're trying to save your attachment in your OneTimeSetup or OneTimeTeardown methods. Quoting from the documentation:

Within a OneTimeSetUp or OneTimeTearDown method, the context refers to the fixture as a whole

...which basically means that using TestContext methods or properties is not allowed within OneTimeSetup or OneTimeTeardown (even though NUnit/Visual Studio won't complain if you do so! Which adds to the confusion)

So make sure you add the test's attachments from within your TearDown or test case. AFAIK, there is no way to save attachments from TestFixture context.

harveyAJ
  • 833
  • 1
  • 11
  • 26