title: How to attach screenshot to Extent Report in java selenium
body: <p>I am trying to attach screenshot for failed testcases from my path to Extent Report but somehow i am not able to attach into it. </p>
  
  <p>I have tried my possible solution but it fails .
  I have used extent report version 3</p>
  
  <p>here is mine full code in separate extenreport class :</p>
  
  <pre><code>    package com.qa.ExtentReportListener;
  
  
      import java.io.IOException;
      import java.util.Calendar;
      import java.util.Date;
      import java.util.List;
      import java.util.Map;
  
      import org.testng.IReporter;
      import org.testng.IResultMap;
      import org.testng.ISuite;
      import org.testng.ISuiteResult;
      import org.testng.ITestContext;
      import org.testng.ITestResult;
      import org.testng.Reporter;
      import org.testng.xml.XmlSuite;
  
      import com.aventstack.extentreports.ExtentReports;
      import com.aventstack.extentreports.ExtentTest;
      import com.aventstack.extentreports.Status;
      import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
      import com.aventstack.extentreports.reporter.configuration.ChartLocation;
      import com.aventstack.extentreports.reporter.configuration.Theme;
      import com.crm.qa.util.TestUtil;
  
      public class ExtentTestNGIReporterListener implements IReporter {
  
          private static final String OUTPUT_FOLDER = "test-output/";
          private static final String FILE_NAME = "Extent.html";
  
          private ExtentReports extent;
          private ExtentTest test;
  
          public void generateReport(List&lt;XmlSuite&gt; xmlSuites, List&lt;ISuite&gt; suites, String outputDirectory) {
              init();
  
              for (ISuite suite : suites) {
                  Map&lt;String, ISuiteResult&gt; result = suite.getResults();
  
                  for (ISuiteResult r : result.values()) {
                      ITestContext context = r.getTestContext();
  
                      buildTestNodes(context.getFailedTests(), Status.FAIL);
                      buildTestNodes(context.getSkippedTests(), Status.SKIP);
                      buildTestNodes(context.getPassedTests(), Status.PASS);
  
                  }
              }
  
              for (String s : Reporter.getOutput()) {
                  extent.setTestRunnerOutput(s);
              }
  
              extent.flush();
          }
  
          private void init() {
              ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
              htmlReporter.config().setDocumentTitle("ExtentReports - Created by TestNG Listener");
              htmlReporter.config().setReportName("ExtentReports - Created by TestNG Listener");
              htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
              htmlReporter.config().setTheme(Theme.STANDARD);
  
              extent = new ExtentReports();
              extent.attachReporter(htmlReporter);
              extent.setReportUsesManualConfiguration(true);
          }
  
          private void buildTestNodes(IResultMap tests, Status status) {
  
  
              if (tests.size() &gt; 0) {
                  for (ITestResult result : tests.getAllResults()) {
                      test = extent.createTest(result.getMethod().getMethodName());
  
                      for (String group : result.getMethod().getGroups())
                          test.assignCategory(group);
  
                      if (result.getThrowable() != null) {
                          test.log(status, result.getThrowable());
                      }
                      else {
                          test.log(status, "Test " + status.toString().toLowerCase() + "ed");
                      }
  
                      test.getModel().setStartTime(getTime(result.getStartMillis()));
                      test.getModel().setEndTime(getTime(result.getEndMillis()));
                  }
              }
          }
  
          public void down(ITestResult result) throws IOException{
  
  
              if(result.getStatus()==ITestResult.FAILURE){
                  test.log(Status.FAIL, "TEST CASE FAILED IS "+result.getName()); //to add name in extent report
                  test.log(Status.FAIL, "TEST CASE FAILED IS "+result.getThrowable()); //to add error/exception in extent report
  
                  String screenshotPath = TestUtil.takeScreenshotAtEndOfTest();
                  test.fail("Test Case failed check screenshot below"+test.addScreenCaptureFromPath(screenshotPath));
                  //extentTest.log(Status.FAIL, MediaEntityBuilder.createScreenCaptureFromPath(screenshotPath).build()); //to add screenshot in extent report
                  //extentTest.fail("details").addScreenCaptureFromPath(screenshotPath);
              }
              else if(result.getStatus()==ITestResult.SKIP){
                  test.log(Status.SKIP, "Test Case SKIPPED IS " + result.getName());
              }
              else if(result.getStatus()==ITestResult.SUCCESS){
                  test.log(Status.PASS, "Test Case PASSED IS " + result.getName());
  
              }
          extent.flush();
          }
  
          private Date getTime(long millis) {
              Calendar calendar = Calendar.getInstance();
              calendar.setTimeInMillis(millis);
              return calendar.getTime();      
          }
      }
  </code></pre>
  
  <p>here is my util class which contain screenshot method:</p>
  
  <pre><code>public static String takeScreenshotAtEndOfTest() throws IOException {
      String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
      TakesScreenshot ts = (TakesScreenshot)driver;
      File source = ts.getScreenshotAs(OutputType.FILE);
      String destination = System.getProperty("user.dir") + "/screenshots/" +  dateName
              + ".png";
      File finalDestination = new File(destination);
      FileHandler.copy(source, finalDestination);
      return destination;
  }
  </code></pre>