How can I add UI test screenshot to TFS Test Report as an attachment
Depending on the test framework you're using to run the UI tests:
- MsTest: TestContext.AddResultsFile(file)
- NUnit (3.8+): TestContext.AddTestAttachement(file)
- XUnit.NET: Not available at the moment.
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:");