If you are using Cucumber to automate testing your web application, it can be useful to include a screenshot in Cucumber’s report whenever a scenario fails.
Cucumber itself doesn’t provide functionality for taking screenshots, but it does make it possible to embed them in the report. If you are using Cucumber together with a Selenium WebDriver, which can capture screens, it’s easy as pie. All you have to do is paste the following lines of Java code into your Cucumber steps definition:
1 2 3 4 5 6 7 8 |
@After public void tearDown(Scenario scenario) { if (scenario.isFailed()) { // Take a screenshot... final byte[] screenshot = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.BYTES); scenario.embed(screenshot, "image/png"); // ... and embed it in the report. } } |