Capture Screenshot in Extent Reports – Java
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:
import java.io.IOException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.ITestResult; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import com.relevantcodes.extentreports.ExtentReports; import com.relevantcodes.extentreports.ExtentTest; import com.relevantcodes.extentreports.LogStatus; public class CapturingScreenshot { ExtentReports extent; ExtentTest test; WebDriver driver; @BeforeTest public void init() { extent = new ExtentReports(System.getProperty("user.dir") + "/test-output/ExtentScreenshot.html", true); } @Test public void captureScreenshot() { test = extent.startTest("captureScreenshot"); driver = new FirefoxDriver(); driver.get("http://automationtesting.in"); String title = driver.getTitle(); Assert.assertEquals("Home - Automation Test", title); test.log(LogStatus.PASS, "Test Passed"); } @AfterMethod public void getResult(ITestResult result) throws IOException { if(result.getStatus() == ITestResult.FAILURE) { String screenShotPath = GetScreenShot.capture(driver, "screenShotName"); test.log(LogStatus.FAIL, result.getThrowable()); test.log(LogStatus.FAIL, "Snapshot below: " + test.addScreenCapture(screenShotPath)); } extent.endTest(test); } @AfterTest 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.
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; public class GetScreenShot { public static String capture(WebDriver driver,String screenShotName) throws IOException { TakesScreenshot ts = (TakesScreenshot)driver; File source = ts.getScreenshotAs(OutputType.FILE); String dest = System.getProperty("user.dir") +"\\ErrorScreenshots\\"+screenShotName+".png"; File destination = new File(dest); FileUtils.copyFile(source, destination); return dest; } }
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.