Capture Screenshot in Extent Reports – CSharp
Capture Screenshot in Extent Reports will discuss about capturing the screenshot of a particular failure step in the HTML report. In our previous blog we have seen how to log the messages in the report. Same way here we will capture the screenshot for a particular failure step. The screenshot will give the information about on which screen the script got failed during the execution. Then we can easily identify the issue by looking into the report.
For this we need to use the Log() method of ExtentTest class. To log the screenshot in the report we need to pass the path of the screenshot to the log method as a parameter. So, before logging the screenshot to the report we need to capture the screenshot of that particular failure method and place it in a folder from there we need to get the path of the screenshot file and pass it to the log method.
Below is the sample program to capture screenshot in the report:
using NUnit.Framework; using NUnit.Framework.Interfaces; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using RelevantCodes.ExtentReports; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ExtentReportsDemo { [TestFixture] public class CapturingScreenshot { ExtentReports extent; ExtentTest test; IWebDriver driver; [OneTimeSetUp] public void Init() { string path = System.Reflection.Assembly.GetCallingAssembly().CodeBase; string actualPath = path.Substring(0, path.LastIndexOf("bin")); string projectPath = new Uri(actualPath).LocalPath; string reportPath = projectPath + "Reports\\ExtentScreenshot.html"; extent = new ExtentReports(reportPath, true); } [Test] public void CaptureScreenshot() { test = extent.StartTest("CaptureScreenshot"); driver = new FirefoxDriver(); driver.Navigate().GoToUrl("http://automationtesting.in"); string title = driver.Title; Assert.AreEqual("Home - Automation Test", title); test.Log(LogStatus.Pass, "Test Passed"); } [TearDown] public void GetResult() { var status = TestContext.CurrentContext.Result.Outcome.Status; var stackTrace = "<pre>" + TestContext.CurrentContext.Result.StackTrace + "</pre>"; var errorMessage = TestContext.CurrentContext.Result.Message; if (status == TestStatus.Failed) { string screenShotPath = GetScreenShot.Capture(driver, "ScreenShotName"); test.Log(LogStatus.Fail, stackTrace + errorMessage); test.Log(LogStatus.Fail, "Snapshot below: " + test.AddScreenCapture(screenShotPath)); } extent.EndTest(test); } [OneTimeTearDown] public void Endreport() { driver.Close(); extent.Flush(); extent.Close(); } } }
In the above program we have written test.Log method to log the screenshot into the extent reports. This information will logged in the report with time stamp.
Below is the program to capture the screenshot and it will place the screenshot in a particular folder and will return the path of the screenshot. Will use that path to capture the screenshot in the HTML report for a failure step.
using OpenQA.Selenium; using System; using System.Collections.Generic; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ExtentReportsDemo { public class GetScreenShot { public static string Capture(IWebDriver driver, string screenShotName) { ITakesScreenshot ts = (ITakesScreenshot)driver; Screenshot screenshot = ts.GetScreenshot(); string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase; string finalpth = pth.Substring(0, pth.LastIndexOf("bin")) + "ErrorScreenshots\\" + screenShotName + ".png"; string localpath = new Uri(finalpth).LocalPath; screenshot.SaveAsFile(localpath, ImageFormat.Png); return localpath; } } }
Above program will return the path of the screenshot.
Below is the output of the above program:
Please watch the YouTube video of this blog for better understanding.