-->

Extent Report: Not able to see the screenshots on

2019-05-31 20:30发布

问题:

I am able generate the extent reports with screenshots on my local machine. But when i mail the reports to someone else, or open the html on a differant machine, the screenshots are not visible. It says that the path is invalid.

While attaching the screenshot, i am giving the path of my local machine. And it is searching the same path on other machine too. I tried zipping the html and pics in one folder too.

Please help me how to attach the screenshots into html file without local machine dependency.

回答1:

You can do this by using base64 conversion of the obtained screenshots. Use the following code in your framework and try it.

public static String addScreenshot() {
    File scrFile = ((TakesScreenshot) BasePage.driver).getScreenshotAs(OutputType.FILE);
    String encodedBase64 = null;
    FileInputStream fileInputStreamReader = null;
    try {
        fileInputStreamReader = new FileInputStream(scrFile);
        byte[] bytes = new byte[(int)scrFile.length()];
        fileInputStreamReader.read(bytes);
        encodedBase64 = new String(Base64.encodeBase64(bytes));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "data:image/png;base64,"+encodedBase64;
}


回答2:

I faced same issue.

Share the folder in which you are storing the screenshots with everyone and return same path in below method.

public static String getScreenshot(WebDriver oDriver, String ScreenShotName) throws IOException
    {
        String dateName=new SimpleDateFormat("YYYYMMDDHHMMSS").format(new Date());
        TakesScreenshot ts=(TakesScreenshot)oDriver;
        File source=ts.getScreenshotAs(OutputType.FILE);
        String destination=System.getProperty("user.dir")+"/FailedScreenshots/"+ScreenShotName+dateName+".png";
        File finalDestination=new File(destination);
        FileUtils.copyFile(source, finalDestination);

        String Imagepath="file://Machinename/FailedScreenshots/"+ScreenShotName+dateName+".png";
        return Imagepath;
    }

Hope this helps!