'Screenshot not getting attached in extent report

I am trying to execute a code wherein I want to attach screenshot to my extent report. Screenshot is getting generated locally but not getting generated in extent report. Below is the code snippet I tried.

else if (testResult.getStatus() == ITestResult.FAILURE) {
            r.test.log(Status.FAIL, "Test case failed is" + testResult.getName());
            r.test.log(Status.FAIL, "TEST CASE FAILED BECAUSE OF" + testResult.getThrowable());

            String screenshotpath=Test_Util.screenshotutil(driver, testResult.getName());
            System.out.println(screenshotpath);
            
            r.test.fail("details", MediaEntityBuilder.createScreenCaptureFromPath(screenshotpath).build());
        }

Below is the second thing I tried

else if (testResult.getStatus() == ITestResult.FAILURE) {
            r.test.log(Status.FAIL, "Test case failed is" + testResult.getName());
            r.test.log(Status.FAIL, "TEST CASE FAILED BECAUSE OF" + testResult.getThrowable());

            String screenshotpath=Test_Util.screenshotutil(driver, testResult.getName());
            System.out.println(screenshotpath);
            r.test.addScreenCaptureFromPath(screenshotpath);
        }

Below is my take screenshot method

public static String screenshotutil(WebDriver driver, String screenshotname) throws IOException {
        String datename = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());

        TakesScreenshot ts=(TakesScreenshot)driver;
        File source = ts.getScreenshotAs(OutputType.FILE);

        String destination="src/main/java/com/qa/report/Screenshots/"+screenshotname+datename+".png";

        File finaldestination=new File(destination);

        FileUtils.copyFile(source, finaldestination);
        return destination;


    }

In both the cases, the screenshot is not getting attached on test case failure. The output I am getting is as follows.Extent_report_with_media_entity_builder_code

The image attached is with media entity builder code. With only takeScreenshotCapturefrompath() method, the details string shown in the image is not displayed which is obvious as I have not provided that String. Also the dependency of extent report which I am using is as follows.

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

Please help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source