Taking screenshot in selenium in cucumber

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.Here is the sample HTML report showing the captured screenshot.

Web development and Automation testing

solutions delivered!!