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 {  
     @After  
     public void embedScreenshot(Scenario scenario) {  
         if (scenario.isFailed()) {  
             try {  
                 byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);  
                 scenario.embed(screenshot, "image/png");  
             } catch (WebDriverException wde) {  
                 System.err.println(wde.getMessage());  
             } catch (ClassCastException cce) {  
                 cce.printStackTrace();  
             }  
         }  
     }  
 }

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