2

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

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

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

4

2 回答 2

1

我面临同样的问题。

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

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

希望这可以帮助!

于 2019-01-25T13:00:16.537 回答
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;
}
于 2018-04-11T06:42:49.610 回答