extent-framework / extentreports-java

Extent Reporting Library, Java
http://extentreports.com
Apache License 2.0
224 stars 127 forks source link

Images not loaded for the failed Results in the extent Reports #251

Open Yoganand1995 opened 4 years ago

Yoganand1995 commented 4 years ago

Hi ,

Below is the code I'm using to get the extent reports for my framework and the images are not loaded in the html reports and please guide me on the issue.

 <dependency>
      <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
      <version>5.0.3</version>
    </dependency>

Base class method to get the screenshot


`  public String getScreenshot(String MethodName, WebDriver FailedDriver) throws IOException {
        TakesScreenshot ScrShot = (TakesScreenshot) FailedDriver;
        File Source = ScrShot.getScreenshotAs(OutputType.FILE);
        String DesFile = System.getProperty("user.dir")+"\\reports\\Screenshots\\"+MethodName+".jpg";
        FileUtils.copyFile(Source,new File(DesFile).getAbsoluteFile());
        return DesFile;
    }`

Extent Report class

`public class ExtentReport {
    public static ExtentReports extent;
    public static ExtentReports getReport(){
        String path = System.getProperty("user.dir") + "\\reports\\HtmlReports\\index.html";
        ExtentSparkReporter reporter = new ExtentSparkReporter(path);
        reporter.config().setReportName("Web Automation Results");
        reporter.config().setDocumentTitle("Test Results");
        extent = new ExtentReports();
        extent.attachReporter(reporter);
        extent.setSystemInfo("Tester","Yogananda");
        return extent;
    }
}`

TestNG Listener Method for the fail

`public void onTestFailure(ITestResult result) {
        Test.fail(result.getThrowable());
        WebDriver FailedDriver = null;
        String MethodName = result.getMethod().getMethodName();
        try {
            FailedDriver= (WebDriver)result.getTestClass().getRealClass()
                          .getDeclaredField("Driver").get(result.getInstance());
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Test.addScreenCaptureFromPath(getScreenshot(MethodName,FailedDriver),MethodName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }`

screenshot of the issue image issue

thanhlelgg commented 4 years ago

I ran into this as well, there's no image attached at all

surajgupta7175 commented 4 years ago

To fix this, kindly get

  1. the instance of getExtentHtmlReport(); and getExtentReport();
  2. attach reporter
  3. ExtentTest testlog=Reporter.getCurrentStep();
  4. Run screenshot code
  5. testlog.info(addLink, MediaEntityBuilder.createScreenCaptureFromPath(fullPath).build());
  6. flush the extent report

There You GO!!

Screenshot 2020-09-11 at 9 34 56 PM