Hello I am trying to capture screenshot of failed test cases using selenium webdriver with Maven and TestNG with following code, it does generate the screenshot in my project but doesn't get included inside the report what do I need to change in the code?

 WebDriver driver;
ExtentReports extent;
ExtentTest logger;
public void setUp() {
    extent = new ExtentReports("C:\\Users\\IdeaProjects\\p\\test-output\\report.html", true);
    extent.loadConfig(new File("C:\\Users\\IdeaProjects\\p\\test-output\\extent-config.xml"));
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
public void testExtentReports() {
    logger = extent.startTest("testExtentReports");
public void tearDown(ITestResult result) throws IOException {
    if(result.getStatus() == ITestResult.FAILURE) {
        logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getName());
        logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getThrowable());
        String screenshotPath = TestMine.getScreenshot(driver, result.getName());
        logger.log(LogStatus.FAIL, logger.addScreenCapture(screenshotPath));
    } else if(result.getStatus() == ITestResult.SKIP) {
        logger.log(LogStatus.SKIP, "Test Case Skipped is "+result.getName());
public static String getScreenshot(WebDriver driver, String screenshotName) throws IOException {
    String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
    TakesScreenshot ts = (TakesScreenshot) driver;
    File source = ts.getScreenshotAs(OutputType.FILE);
    // after execution, you could see a folder "FailedTestsScreenshots" under src folder
    String destination = "ss/" + screenshotName+dateName+".png";
    File finalDestination = new File(destination);
    FileUtils.copyFile(source, finalDestination);
    return destination;
public void endReport() {

1 Answer 1


It seems that you are correctly capturing the screenshot in the method getScreenshot and appending the screenshot path to the ExtentReports logger object.

However, the issue is that you are saving the screenshot in your project directory (ss/) instead of inside the ExtentReports HTML report.

You can resolve this issue by changing the destination of your screenshot file. Instead of saving it in the project directory, you can save it inside the ExtentReports HTML report using the following code:

String destination = System.getProperty("user.dir") + "\\test-output\\Screenshots\\" + screenshotName + dateName + ".png";

This will save the screenshot in the Screenshots folder inside the test-output folder of your project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.