Selenium webdriver:将屏幕截图导出到机器人框架日志文件

时间:2012-09-25 15:02:12

标签: java selenium-webdriver webdriver robotframework

我正在使用selenium webdriver和机器人框架,我遇到了以下问题:

我想在每次测试失败时制作屏幕截图并将此屏幕截图导出到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>

Okey,所以我认为应该很容易实现并将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 (因声誉不佳而无法发布图片)

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

1 个答案:

答案 0 :(得分:1)

尝试使用以下示例:

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