I want to access all the cucumber scenario steps in @before
hook. Is there a way to do this?
I have tried passing the cucumber scenario object in the before hook method but it only provides the basic info like scenario.getName()
, scenario.getId()
. What I require is something like getSteps()
which give me the List<String>
of all the steps of that particular scenario.
What I am looking for is something like this
@Before("@dev")
public void testcase(Scenario scenario){
for (Step a : scenario.getSteps()) {
System.out.println("scenario step = " + a.getText());
}
}
Basically I want the complete scenario information at the beginning of the test execution itself.
If I pass the argument of class cucumber.api.TestCase in the before method then I can access the getTestSteps()
method but that leads to below exception.
cucumber.runtime.CucumberException: When a hook declares an argument it must be of type cucumber.api.Scenario. public void com.thermofisher.bid.spa.kingfisher.ui.steps.Hooks.poc(cucumber.api.TestCase)
Try something like this:
@Before
public void setUp(Scenario scenario) throws Exception{
tags = (ArrayList<String>) scenario.getSourceTagNames();
System.out.println("At Hooks: " + scenario.getId());
Iterator ite = tags.iterator();
while (ite.hasNext()) {
String buffer = ite.next().toString();
if (buffer.startsWith("<tagOfATestCase>")) {
Field f = scenario.getClass().getDeclaredField("testCase");
f.setAccessible(true);
TestCase r = (TestCase) f.get(scenario);
List<PickleStepTestStep> testSteps = r.getTestSteps().stream().filter(x -> x instanceof PickleStepTestStep)
.map(x -> (PickleStepTestStep) x).collect(Collectors.toList());
for (PickleStepTestStep ts : testSteps) {
System.out.println(ts.getStepText());//will print your test case steps
}
}
}