Cucumber: How to stop the execution of scenarios which has failed steps
In this article, we will implement the solution to stop the Cucumber scenarios from execution, if any of the steps failed previously.
- 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.
We need to call this class in the plugin as a package name.GetTestStepName from the test runner class as follows.
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.
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
The entire class is as follows
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
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.