How to Embed screenshots in Cucumber JVM

15 December, 2015
I wanted to embed the screenshot to the cucumber report whenever a test fails. The Cucumber JVM implementation highly simplifies this task. We can use the Execution Hooks to make this generic to all the tests.

 public class ScreenshotHook {  
     public void embedScreenshot(Scenario scenario) {  
         if (scenario.isFailed()) {  
             try {  
                 byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);  
                 scenario.embed(screenshot, "image/png");  
             } catch (WebDriverException wde) {  
             } catch (ClassCastException cce) {  

Am defining an embedScreenshot method which is annotated with cucumber.annotation.After: methods annotated as such will be executed after each cucumber scenario.

No comments:

Post a Comment