Capture Screenshot in Extent Reports – Java – Version3
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.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.ExtentTest; import com.aventstack.extentreports.Status; import com.aventstack.extentreports.markuputils.ExtentColor; import com.aventstack.extentreports.markuputils.MarkupHelper; import com.aventstack.extentreports.reporter.ExtentHtmlReporter; public class CapturingScreenshot { ExtentHtmlReporter htmlReporter; ExtentReports extent; ExtentTest test; WebDriver driver; @BeforeTest public void config() { htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") +"/test-output/MyOwnReport.html"); extent = new ExtentReports(); extent.attachReporter(htmlReporter); } @Test public void captureScreenshot() { test = extent.createTest("captureScreenshot"); System.setProperty("webdriver.gecko.driver", "/KRISHNA VOLUME/drivers/geckodriver"); driver = new FirefoxDriver(); driver.get("http://automationtesting.in"); String title = driver.getTitle(); Assert.assertEquals("Home - Automation Test", title); } @AfterMethod public void getResult(ITestResult result) throws IOException { if (result.getStatus() == ITestResult.FAILURE) { String screenShotPath = GetScreenShot.capture(driver, "screenShotName"); test.log(Status.FAIL, MarkupHelper.createLabel(result.getName()+" Test case FAILED due to below issues:", ExtentColor.RED)); test.fail(result.getThrowable()); test.fail("Snapshot below: " + test.addScreenCaptureFromPath(screenShotPath)); } else if(result.getStatus() == ITestResult.SUCCESS) { test.log(Status.PASS, MarkupHelper.createLabel(result.getName()+" Test Case PASSED", ExtentColor.GREEN)); } else { test.log(Status.SKIP, MarkupHelper.createLabel(result.getName()+" Test Case SKIPPED", ExtentColor.ORANGE)); test.skip(result.getThrowable()); } extent.flush(); } @AfterTest public void endReport() { driver.quit(); } }
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.