Automatically take a screenshot on failure of a Cucumber scenario

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:

7 Replies to “Automatically take a screenshot on failure of a Cucumber scenario”

  1. if you want at some specific location then execute code like this..

    if (scenario.isFailed()) {
    File scrFile = ((TakesScreenshot)dictionaryPage.getDriver()).getScreenshotAs(OutputType.FILE);

    try {
    FileUtils.copyFile(scrFile, new File(“C:/Users/Public/Downloads/TestScreenshot/Error.jpg”));
    } catch (IOException e) {
    // TODO Auto-generated catch block

Leave a Reply

Your email address will not be published. Required fields are marked *

4 − 2 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.