Cucumber: How to stop the execution of scenarios which has failed steps

Kaziranga National Park-Assam

In this article, we will implement the solution to stop the Cucumber scenarios from execution, if any of the steps failed previously.


  1. Keep track of the status of each of the steps of the scenarios.
    We can achieve this using the cucumber hook @AfterStep

The method trackStep accepts Scenario as a parameter that keeps track of the status of the step i.e., pass or fails.

We can get the current step, by implementing the Cucumber plugin to do that we have to create a class GetTestStepName. This class implements the CocurrentEventListener. -this class provides you the currently executing step name.

We need to call this class in the plugin as a package name.GetTestStepName from the test runner class as follows.

Use of the class in the plugin.

2. Finally, we will implement the excludeScenarioWithFailedStep method for the @Before hook. This method store all the steps of the scenarios in the temp set and then compare them with the previously failed steps.

Logic to implement the exclusion of the failed step

While comparing, if any match finds it throws a custom error message i.e., This scenario contains already failed step

The custom exception class looks like below

Custom Exception class

The entire class is as follows

StepDefinition implements the various hooks to restricts the execution of scenarios which has failed steps along with custom exception message.

During the execution of the scenarios, the scenarios which have previously failed steps will simply fail them with a message:

This scenario contains already failed step

JUnit execution results

About author Sumit: QA for 15 years and passionate about Test Automation, Service Virtualization, Web service, DevOps & ETL-BI Testing. Currently working for a leading telecom company CenturyLink as an offshore Automation COE lead. Please connect with him on LinkedIn.

Looking to create XPath, Script, and Analysis of the existing automation scripts instantly you can use the web extension tool TruePath.




Test Automation Enthusiastic.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Angular Library Series - Building and Packaging

How to build an infinite loader component in ReactJS.

How to write asynchronous code in Javascript (and Meteor)

Backstage with “new” keyword in JS

A basic JavaScript Fetch(GET)

Screamin’ Speed with WebAssembly

Start Thinking In React Way

Getting Started with NuxtJS: A Vue Beginner — Part 2

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Test Automation Enthusiastic.

More from Medium

5 Research-Based Insights to Improve Your Salesforce CRM Performance — Forcetalks

Checked Out: Curtain Calls Part 2

Eat that Frog with a Pomodoro!