Bfcm
Bfcm

Reputation: 2746

Selenium webdriver: Export screenshot to robot framework log file

I'm using selenium webdriver together with robot framework and I have the following problem:

I want to make a screenshot every time my tests fail and export this screenshot to log.html file.

Making screenshot is quity an easy thing:

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

But the problem is to export the screenshot as html.

In selenium RC the html part with screenshot looks like this:

<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, so I thought that should be an easy to implement and extended my captureScreenshot() function to this:

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

}

But the problem is, that this implementation is not acceptable for my needs. It looks good, but all I get is some text inside tag, that will be not displayed as an image.

To understand it better here is the screenshot from what I get:

http://gyazo.com/5d7dec1e05443786b5d390054edad3e8 (can't post image due to low reputation)

So the question is - how to get screenshot imported to robot framework log.html file?

Upvotes: 0

Views: 1275

Answers (1)

PavloSI
PavloSI

Reputation: 88

Try to use following example:

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

Upvotes: 1

Related Questions