Taking screenshot and embeding in the Cucumber report

When working in a selenium automation project, we often need to take a screenshot on test failure. Selenium API provides a way to take screenshot with the help of takesScreenshot interface.

Cucumber allows you to embed that screenshot in the Cucumber reports.

Here is the simple code.

@After("@selenium")
    public void killBrowser(Scenario scenario){
        if (scenario.isFailed()) {
         scenario.embed(((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES), "image/png");
        }
        driver.close();
        driver.quit();
    }

Below image shows where to find the HTML report in Maven project.

Cucumber report location in Maven project

Cucumber report location in Maven project

Here is the sample HTML report showing the captured screenshot.

Embeding the screenshot in Cucumber report

Embeding the screenshot in Cucumber report

You may also like...