How to get all the cucumber scenario steps in before hook?

白昼怎懂夜的黑 提交于 2021-01-07 06:35:53

问题


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)

回答1:


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

            }

        }

    }



回答2:


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();
}




回答3:


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


来源:https://stackoverflow.com/questions/56296348/how-to-get-all-the-cucumber-scenario-steps-in-before-hook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!