How to take Screenshot of Failed Test Cases in Cucumber

On This Page Why is a Screenshot command in Cucumber Testing?June 15, 2026 · 6 min read · Testing Guide

How to take Screenshot of Failed Test Cases in Cucumber

Cucumber Testing Framework is among the most wide used quiz fabric for Advanced BDD Test Automation. Capturing screenshots for failed scenario in Cucumber tests helps testers speedily identify issues and debug effectively. This feature integrate easily with Selenium WebDriver and ameliorate visibility of test effect in reports.

Overview

Why Capture Screenshots in Cucumber?

  • It helps to visualize test failures rather of relying alone on log.
  • It also speeds up source cause analysis during debugging.

How to Capture Screenshots in Cucumber?

  • Set up the project using the Cucumber framework
  • Implement @ After hook in Cucumber to detect failure.
  • If a scenario fails, initiation screenshot capture utilize WebDriver.
  • Define the folder for the screenshot to be placed within.

This article will research how to set up the Cucumber Project and also excuse about guide screenshots in Cucumber.

Why is a Screenshot required in Cucumber Testing?

While using Cucumber, it is very significant to know why the test is getting failed. One utilitarian way is to include a screenshot of failed stride in the test performance report whenever some scenario is failed. Screenshots help in canvas where the execution is facing problems and improve the debugging process.

Screenshots provide best quality and flexibility while debugging failed scenarios.

How to Set up a Cucumber Project

Let ’ s first set up the labor using the Cucumber framework:

Step 1: Install IDE and Set up Java

You can install any IDE, however, employEclipse IDEin this model. Also, install the latest version of Java on your scheme. To see that Java is installed on your system, run a quick command in the terminus to ascertain the Java version.

Java -version

Step 2: Create a new Maven Project

To create a Maven project, you must install the Maven Plugin. However, in the modish versions of Eclipse, it comes installed already.

Step 3: Add Maven dependencies to your project

In this pace, you have to add several dependencies to your project. Navigate to thepom.xmlfile in the brochure structure and so add the dependencies mentioned below. You can get these dependencies fromMaven Repository.

  • Cucumber Java
  • Cucumber JUnit
  • JUnit
  • Selenium Java

Step 4: Create a lineament file

Navigate tosrc/test/resourcesin the booklet construction and then create afeaturefolder. Also, add a feature file in the lineament folder there.

Step 5: Install the Cucumber plugin

You can easy install the Cucumber plugin from the Eclipse marketplace. The Cucumber plugin lets the codebase cognise that you ’ re creating a Cucumber undertaking.

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Step 6: Create Step Definitions

To create Step definitions, navigate to thesrc/test/javapacket and so add a step definition booklet and file there.

Step 7: Create a Runner Class

Create a classfile in theStep Definitionsfolder and add therunner classscript. Therunner classwill look something like this.

significance org.junit.runner.RunWith; meaning io.cucumber.junit.Cucumber; significance io.cucumber.junit.CucumberOptions; @ RunWith (Cucumber.class) @ CucumberOptions (features= '' src/test/resources/Features '', glue= {`` StepDefinitions ''}) public class TestRunner {}

Step 8: Set up .properties file

An extent propertiesfile is necessary for your project because it helps in activating the failed tryout reports. Before creating the file, you must add a few dependencies, which are thegrasshopper extent account adapterplugin and theExtent study library.

Read More:

To set up these dependency, add the next scripts inside the pom.xml file.

For the grasshopperplugin, the following dependency is expect.

& lt;! -- https: //mvnrepository.com/artifact/tech.grasshopper/extentreports-cucumber6-adapter -- & gt; & lt; dependency & gt; & lt; groupId & gt; tech.grasshopper & lt; /groupId & gt; & lt; artifactId & gt; extentreports-cucumber6-adapter & lt; /artifactId & gt; & lt; version & gt; 2.1.0 & lt; /version & gt; & lt; /dependency & gt;

Similarly, to add the Extent report library, add the following dependance script.

& lt;! -- https: //mvnrepository.com/artifact/com.aventstack/extentreports -- & gt; & lt; dependency & gt; & lt; groupId & gt; com.aventstack & lt; /groupId & gt; & lt; artifactId & gt; extentreports & lt; /artifactId & gt; & lt; version & gt; 5.0.4 & lt; /version & gt; & lt; /dependency & gt;

Now, make a.propertiesfile in thesrc/test/resources. Secondly, add the next textbook in the plugin section of your test, so that the runner class of Cucumber can realize that you want to initiate a report with the help of a sure transcriber.

@ CucumberOptions (plugin = {`` com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter: ''})

Test Management Reimagined with AI

Join our live webinar to discover how top QA teams boost test creation fastness by 90 % utilize AI.

How to take Screenshots of Failed Test Cases in Cucumber

Let ’ s now take a practical implementation on how to take screenshots for failed trial cases in Cucumber. In this illustration, we will create an account on Facebook. However, while make that, we will write a wrong stride to get a failed examination. This will demonstrate how to guide screenshots of failed trial cases in Cucumber.

Step 1: Create a Feature file

@ SmokeTest Feature: login facebook Scenario: login facebook and create account Given open safari browser And navigate to Facebook URL And user enters firstname as `` BrowserStack User ''

Step 2: Create a Hooks file inside the Step Definitions folder

package stepDefinitions; importee org.openga. selenium. OutputType: public course Hooks extends BaseClass {@ After (order = 1) public void takeScraenshotOnFailure (Scenario scenario) {if (scenario.isFailed ()) {TakesScreenshot ts = (TakesScreenshot) driver; byte [] src = ts.getScreenshotAs (QutputType.BYTES); scenario.attach (src, `` image/png '', `` screenshot '');}} @ After (order = 0) public void tearDown () {driver.close ();}}

Here, order = 1 denotes that this measure will be conducted first, and so the next steporder = 2will take place, thence fold the browser after the screenshot is taken. This step covers the main syntax of conduct screenshots of failed tests in Cucumber.

Step 3: Create Page Class file

This step leads us to the with the assistant of Java.

package pages; signification org.openqa.selenium.By; [] public class Login_Page {Utils utils= new Utils (); By elements_link = By.xpath (`` '//div [@ class='category-cards '] /div [1] /div/div [3] /h5 ''); By Forms_link = By.xpath (`` //div [@ class='category-cards'div [2] /div/div [3] /h5 ”); By textField_firstName = By.xpath (`` //input [@ name= ‘ firstname123 ’] ”) public void click_elements () {utils. clickUsingAction (elements_link);} public void click Forms () {utils.click (Forms_link);} public void EnterFirstName (String firstName) {utils.sendKeys (textField_firstName, firstName);}}

Read More:

Step 4: Create a exam runner class file

bundle runner; import org.junit.AfterClass; [] I @ RuniWith (Cucumber.class) @ CucumberOptions (features= '' ./src/test/resources/Features/ '', glue= {tepDefinitions ''}, tages = “ @ SmokeTest '', plugin = {`` fairly '', `` html: target/cucumber-reports '', '' com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter: ''}, monochrome=true) public class TestRunner {}

Step 5: Define the position for the screenshot

To place the screenshot in some location, you must define the location of the image using the following codification.

# screenshot.dir=test-output/ screenshot.dir=target/ screenshot.rel.path= .. /

Place this codification inside theextent.propertiesfile in the features folder.

After execute all the above stairs. You can see the screenshot of the instance where the step failed. The screenshot gets available inside the folder where you have directed it in theextent.properties file.

On a closing tone

Screenshots become very useful while study and debug your tests. In the above example, we have used Cucumber along with to demonstrate the process. Cucumber offers various useful features, one of which is the use of the BDD (Behavior Driven Development) technique. Cucumber itself doesn ’ t offer the functionality to take screenshots, however, adding a few lines of Java codification into the step definition does the work, get it lots more efficient in debug.

For better accuracy is recommended to test on so as to take into report. Testing on tools like allows you to test your website on 3000+ device browser combinations, which helps render a seamless user experience.

Tags
30,000+ Views

# Ask-and-Contributeabout this theme with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free