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

Timus
2 min readJun 13, 2021
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.

Steps:

  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.

GetTestStepName.java -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 GetTestStepName.java 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.

--

--