java - Cucumber on JUnit Test Failure Hook
I generally take screenshots in the @After
hook which gets called after every cucumber scenario. @After
hook method takes a special Scenario
object so that you can embed your screenshots in your reports. Here is an example on how to take screenshots in the event your scenario fails,
@After public void tearDown(Scenario scenario) { try { if (scenario.isFailed()) { final byte[] screenshot = ((TakesScreenshot) driver) .getScreenshotAs(OutputType.BYTES); scenario.embed(screenshot, "image/png"); } } finally { driver.quit(); } }
If you are using Geb in front of Selenium, I think you could just make use of geb.Browser.report() to take the screenshot.