Last active
June 13, 2021 16:57
-
-
Save gsumit1/9c12e567fa9d49b2c8bfc14b0ae94044 to your computer and use it in GitHub Desktop.
StepDefinition implements the various hooks to restricts the execution of scenarios which has failed steps along with custom exception message.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ctl.it.qa.sample.tests; | |
import cucumber.api.Scenario; | |
import cucumber.api.java.After; | |
import cucumber.api.java.Before; | |
import net.serenitybdd.core.Serenity; | |
import net.thucydides.core.annotations.Steps; | |
import java.lang.reflect.Field; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
import cucumber.api.TestCase; | |
import cucumber.api.java.AfterStep; | |
import cucumber.api.*; | |
public class StepDefinition { | |
final static Set<String> failedSteps = new HashSet<String>(); | |
@Before | |
public void beforeEveryScenario(Scenario scenario) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, alreadyFailedTestStep { | |
excludeScenarioWithFailedStep(scenario); | |
} | |
@AfterStep | |
public void trackStep(Scenario scenario) | |
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { | |
if (scenario.isFailed()) { | |
failedSteps.add(GetTestStepName.currentStepName); | |
} | |
} | |
@After | |
public void afterEveryScenario(Scenario scenario) { | |
} | |
void excludeScenarioWithFailedStep(Scenario scenario) throws NoSuchFieldException, SecurityException, | |
IllegalArgumentException, IllegalAccessException, alreadyFailedTestStep { | |
Set<String> tempScenarioSteps = new HashSet<String>(); | |
Field testCaseField = scenario.getClass().getDeclaredField("testCase"); | |
testCaseField.setAccessible(true); | |
TestCase tc = (TestCase) testCaseField.get(scenario); | |
Field testSteps = tc.getClass().getDeclaredField("testSteps"); | |
testSteps.setAccessible(true); | |
List<TestStep> teststeps = tc.getTestSteps(); | |
for (int i = 0; i < teststeps.size(); i++) { | |
try { | |
PickleStepTestStep pts = (PickleStepTestStep) teststeps.get(i); | |
tempScenarioSteps.add(pts.getStepText()); | |
} catch (Exception ignore) { | |
} | |
} | |
long count = failedSteps.stream().filter(tempstring -> { | |
return tempScenarioSteps.stream().anyMatch(tempstring2 -> { | |
return tempstring.equals(tempstring2); | |
}); | |
}).count(); | |
if (count > 0) | |
throw new alreadyFailedTestStep("This scenario contains already failed step"); | |
} | |
} | |
@SuppressWarnings("serial") | |
class alreadyFailedTestStep extends Exception { | |
public alreadyFailedTestStep(String s) { | |
super(s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment