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
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. Why Capture Screenshots in Cucumber? How to Capture Screenshots in Cucumber? This article will research how to set up the Cucumber Project and also excuse about guide screenshots in Cucumber. 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. 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. 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. 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. 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. Similarly, to add the Extent report library, add the following dependance script. 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. 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 Step 2: Create a Hooks file inside the Step Definitions folder 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. Read More: Step 4: Create a exam runner class file 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. 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. # Ask-and-Contributeabout this theme with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.How to take Screenshot of Failed Test Cases in Cucumber
Overview
Why is a Screenshot required in Cucumber Testing?
How to Set up a Cucumber Project
Java -version
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 {}& 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;
& 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;
@ CucumberOptions (plugin = {`` com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter: ''}) Test Management Reimagined with AI
How to take Screenshots of Failed Test Cases in Cucumber
@ 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 ''
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 ();}}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);}}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 {}# screenshot.dir=test-output/ screenshot.dir=target/ screenshot.rel.path= .. /
Related Guides
Automate This With SUSA
Test Your App Autonomously