【问题标题】:Extent Report: Not able to see the screenshots on other machine范围报告:无法在其他机器上看到屏幕截图
【发布时间】:2026-01-13 07:45:01
【问题描述】:

我可以在我的本地机器上生成带有屏幕截图的范围报告。 但是当我将报告邮寄给其他人,或在不同的机器上打开 html 时,屏幕截图不可见。它说路径无效。

在附上屏幕截图时,我给出了我本地机器的路径。它也在其他机器上搜索相同的路径。 我也尝试将 html 和图片压缩到一个文件夹中。

请帮助我如何在不依赖本地机器的情况下将屏幕截图附加到 html 文件中。

【问题讨论】:

  • 你好@Nishant,你有没有找到任何解决方案。如果是,请告诉我。我也面临同样的问题

标签: java selenium webdriver extent


【解决方案1】:

您可以通过对获得的屏幕截图使用 base64 转换来做到这一点。 在您的框架中使用以下代码并尝试一下。

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;
}

【讨论】:

  • 此代码工作正常,但在范围报告中,当我单击嵌入的图像时,它会显示类似这样的内容。 user-images.githubusercontent.com/16231354/…
  • 是的,缩略图视图没问题,但是当点击屏幕截图时会变形。
【解决方案2】:

我遇到了同样的问题。

与所有人共享您存储屏幕截图的文件夹,并在下面的方法中返回相同的路径。

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;
    }

希望这会有所帮助!

【讨论】: