硒网络驱动程序:将屏幕截图导出到机器人框架日志文件



我正在使用硒网络驱动程序和机器人框架,我有以下问题:

我想在每次测试失败时制作屏幕截图,并将此屏幕截图导出到log.html文件。

制作屏幕截图是一件容易的事情:

    String path;
    try {
        WebDriver augmentedDriver = new Augmenter().augment(driver);
        File source = ((TakesScreenshot) augmentedDriver)
                .getScreenshotAs(OutputType.FILE);
        path = "./screenshots/" + source.getName();
        FileUtils.copyFile(source, new File(path));
    } catch (IOException e) {
        path = "Failed to capture screenshot: " + e.getMessage();
    }

但问题是将屏幕截图导出为 html。

在Selenium RC中,带有屏幕截图的html部分如下所示:

<tbody>
    <tr>
        <td class="time">15:25:44.968</td>
        <td class="fail level">FAIL</td>
        <td class="message">Value of text field 'xpath=//input' should have been '' but was 'VpomRihh3Xa' Screenshot: </td>
    </tr>
    <tr>
        <td colspan="3">
            <img src="./screenshots/screenshot175324738088103861.png">
        </td>
    </tr>
</tbody>

好的,所以我认为这应该是一个易于实现的,并将我的captureScreenshot()函数扩展到这个:

private String captureScreen() {
    String path;
    try {
        WebDriver augmentedDriver = new Augmenter().augment(driver);
        File source = ((TakesScreenshot) augmentedDriver)
                .getScreenshotAs(OutputType.FILE);
        path = "./screenshots/" + source.getName();
        FileUtils.copyFile(source, new File(path));
    } catch (IOException e) {
        path = "Failed to capture screenshot: " + e.getMessage();
    }
    StringBuilder builder = new StringBuilder();
    builder.append("n<tr><td colspan="3"><img src="").append(path).append(""></tr></td>");
    System.out.println(builder.toString());
    return "";
}

但问题是,这种实现对于我的需求来说是不可接受的。它看起来不错,但我得到的只是标签内的一些文本,这些文本不会显示为图像。

为了更好地理解它,这是我得到的屏幕截图:

http://gyazo.com/5d7dec1e05443786b5d390054edad3e8(由于声誉低,无法发布图片)

所以问题是 - 如何将屏幕截图导入机器人框架日志.html文件?

尝试使用以下示例:

 System.out.println("*HTML* <img src="testScreenshot.png" width="800px">");

最新更新