0
votes

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)
4
What's the full name of Scenario class?Qingfei Yuan
cucumber.api.Scenario.classDEVYANSH MITTAL
Have you tried with TestCase.getTestSteps?supputuri
Cucumber will not give you this information. Serenity may have the information available but it would be non-trivial to obtain. What problem are you trying to solve?John Smart
I am going so second John on this. If it's for reporting purposes have a look at the plugin infra structure.M.P. Korstanje

4 Answers

1
votes

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

            }

        }

    }
0
votes

Cucumber does not provide you any direct method which grab complete information of a scenario inside the Hook. There is Scenario interface which can give you very limited information like scenario name and below are the rest direct methods of this interface.

public interface Scenario {
    Collection<String> getSourceTagNames();
    Result.Type getStatus();
    boolean isFailed();
    void embed(byte[] data, String mimeType);
    void write(String text);
    String getName();
    String getId();
    String getUri();
    List<Integer> getLines();
}

enter image description here

0
votes

Java:

Refer to the TestCase interface under cucumber-jvm/core/src/main/java/io/cucumber/core/runner/TestCase.java

https://github.com/cucumber/cucumber-jvm/blob/main/core/src/main/java/io/cucumber/core/runner/TestCase.java

testcase.getTestSteps();

I know the question is not related to ruby, but want to provide the solution in ruby too so that it might help someone looking for the solution in ruby.

Ruby: Will give all the list of steps.

scenario.source[1].steps
0
votes

With Cucumber 6.10.3 it is possible to do it, with the following code:

Field f = scenario.getClass().getDeclaredField("delegate");
f.setAccessible(true);
io.cucumber.core.backend.TestCaseState sc = (io.cucumber.core.backend.TestCaseState) f.get(scenario);          
Field f1 = sc.getClass().getDeclaredField("testCase");
f1.setAccessible(true);
io.cucumber.plugin.event.TestCase testCase = (io.cucumber.plugin.event.TestCase) f1.get(sc);
        
List<PickleStepTestStep> testSteps = testCase.getTestSteps().stream().filter(x -> x instanceof PickleStepTestStep).map(x -> (PickleStepTestStep) x).collect(Collectors.toList());

for (PickleStepTestStep ts : testSteps) {
       System.out.println(ts.getStep().getKeyword() + ts.getStep().getText());
}

Imports:

import io.cucumber.java8.Scenario;
import io.cucumber.plugin.event.PickleStepTestStep;
import io.cucumber.core.backend.TestCaseState;
import java.lang.reflect.Field;
import java.util.stream.Collectors;

Dependencies: (you only need 'cucumber-java8' or 'cucumber-java')

compile group: 'io.cucumber', name: 'cucumber-java8', version: '6.10.3'
compile group: 'io.cucumber', name: 'cucumber-java', version: '6.10.3'
compile group: 'io.cucumber', name: 'cucumber-core', version: '6.10.3'