Pavlo Osokin
Pavlo Osokin

Reputation: 41

How can I add UI test screenshot to TFS Test Report as an attachment

I'm using Visual Studio 2017, Visual Studio Test Tools and VSTFS v.15. I managed to take screenshots using Selenium and currently store them in 'TestLogsDir' folder. When I run UI tests locally I can easily locate my screenshots after each test run. But after running nightly build on a Virtual Machine 'TestLogsDir' is being deleted. Obviously, I can store screenshots somewhere else, but the thing is how can I attach the screenshot to a corresponding test case in TFS Test Report?

Upvotes: 3

Views: 3733

Answers (2)

Chandan Sadhukhan
Chandan Sadhukhan

Reputation: 11

Below code, I used to take a screenshot, save it in the working directory and, update the file to TFS build.

using NUnit.Framework;
using System.Drawing;
using System.IO;

    Screenshot screenshot = ((ITakesScreenshot)Webdriver).GetScreenshot();

    string screenshotFile = Path.Combine(TestContext.CurrentContext.WorkDirectory, "screenshot.png");
    screenshot.SaveAsFile(screenshotFile, ScreenshotImageFormat.Png);

    TestContext.AddTestAttachment(screenshotFile, "Error Snapshot:");

Upvotes: 0

jessehouwing
jessehouwing

Reputation: 114887

Depending on the test framework you're using to run the UI tests:

Upvotes: 5

Related Questions