'screen shot is not displayed in extent report

The screen shot is taken and stored in the folder. but its not displaying for the failed test. displayed as corrupted image.

Java Code:

    public synchronized void onTestFailure(ITestResult result) {
                System.out.println((result.getMethod().getMethodName() + " failed!"));
                test.get().fail(MarkupHelper.createLabel(result.getName()+ " - Test failed due to below issue/error: ", ExtentColor.RED));
                test.get().fail(result.getThrowable());
                
                //Take screenshot and allow automatic saving of media files relative to the report
                //Extentreports log and screenshot operations for failed tests.
                try {
                    File src=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    //          String base64Screenshot = "data:image/png;base64,"+((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
                    String path=prop.getProperty("Screenshot_Folder")+System.currentTimeMillis()+".png";
                    File destination=new File(path);
                    FileUtils.copyFile(src, destination);
                    
                    test.get().fail("Below is Screen Shot of Error Page/Pop-up: ", MediaEntityBuilder.createScreenCaptureFromPath(path).build());
                    //test.get().fail("Below is Screen Shot of Error Page/Pop-up: ", MediaEntityBuilder.createScreenCaptureFromBase64String(base64Screenshot).build());
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("Screen-capture has been taken but not attached to Extent report");
                }
    
            }

below is the property file.

AutomationReport_Folder = D://Shared//V1core_automation
ExtentReport_Folder = D://Shared//V1core_automation//ExtentReports//
Screenshot_Folder = D://Shared//V1core_automation//ExtentReports//Screenshots//

Method for screen shot

public static String getScreenshot(WebDriver driver) {

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

        String path=System.getProperty("user.dir")+"/Screenshots/"+System.currentTimeMillis()+".png";

        File destination=new File(path);

        try 
        {
            FileUtils.copyFile(src, destination);
        } catch (IOException e) 
        {
            System.out.println("Capture Failed "+e.getMessage());
        }

        return path;

    }


Solution 1:[1]

Replace this line in your code:

test.get().fail("Below is Screen Shot of Error Page/Pop-up: ", MediaEntityBuilder.createScreenCaptureFromPath(path).build());

With

MediaEntityBuilder.addScreenCaptureFromPath(path, result.getMethod().getMethodName());

and see it works.

Solution 2:[2]

I had a similar requirement to store screenshots in base64 as reports will be shared with multiple stakeholders across my team. The following solution worked well for me.

Screenshot.java

public static String getScreenshot(WebDriver driver) {
        String screenshotBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
        return screenshotBase64;
    }

TestListener.java

@Override
    public void onTestFailure(ITestResult result) {
        test.fail("Test case Failed");
        test.fail("Failed step: ",MediaEntityBuilder.createScreenCaptureFromBase64String("data:image/png;base64,"+Screenshot.getScreenshot(driver)).build());
        test.fail(result.getThrowable());
    }

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Huzefa Kazi
Solution 2 vijay atm