java 如何让所有的 cucumber 场景的步骤之前,钩?

7d7tgy0s  于 2023-03-28  发布在  Java
关注(0)|答案(5)|浏览(139)

我想访问@before钩子中的所有 cucumber 场景步骤。有没有办法做到这一点?
我尝试在before hook方法中传递cucumber场景对象,但它只提供了scenario.getName()scenario.getId()等基本信息。我需要的是getSteps(),它给予了该特定场景的所有步骤的List<String>
我要找的是这样的东西

@Before("@dev")
public void testcase(Scenario scenario){

    for (Step a : scenario.getSteps()) {
        System.out.println("scenario step = " + a.getText());
    }
}

基本上,我希望在测试执行本身开始时获得完整的场景信息。
如果我在before方法中传递cucumber.API.TestCase类的参数,那么我就可以访问getTestSteps()方法,但这会导致下面的异常。

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)
xxe27gdn

xxe27gdn1#

试试这样的方法:

@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

            }

        }

    }
ahy6op9u

ahy6op9u2#

** java **

参见cucumber-jvm/core/src/main/java/io/cucumber/core/runner/TestCase.java下的TestCase接口
https://github.com/cucumber/cucumber-jvm/blob/main/core/src/main/java/io/cucumber/core/runner/TestCase.java

testcase.getTestSteps();
  • 我知道这个问题与ruby无关,但我也想用ruby提供解决方案,这样可以帮助那些在ruby中寻找解决方案的人。
    **Ruby:**将给予所有步骤的列表。
scenario.source[1].steps
oiopk7p5

oiopk7p53#

使用Cucumber 6.10.3可以做到这一点,代码如下:

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

进口:

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;

依赖关系:(你只需要'cucumber-java8'或'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'
xsuvu9jc

xsuvu9jc4#

虽然很丑,但对我来说很好:

@Before
    public void beforeScenario(Scenario scenario) throws Exception {
        Field delegate = scenario.getClass().getDeclaredField("delegate");
        delegate.setAccessible(true);
        TestCaseState state = (TestCaseState) delegate.get(scenario);
        Field testCaseF = state.getClass().getDeclaredField("testCase");
        testCaseF.setAccessible(true);
        TestCase testCase = (TestCase) testCaseF.get(state);
        Field pickle = testCase.getClass().getDeclaredField("pickle");
        pickle.setAccessible(true);
        Pickle pickleState = (Pickle) pickle.get(testCase);
        List<Step> steps = pickleState.getSteps();
        //... your logic here
    }

如果需要进口:

import io.cucumber.core.backend.TestCaseState;
import io.cucumber.core.gherkin.Pickle;
import io.cucumber.core.gherkin.Step;
import io.cucumber.java.Before;
import io.cucumber.java.Scenario;
import io.cucumber.plugin.event.TestCase;

import java.lang.reflect.Field;
import java.util.List;
eimct9ow

eimct9ow5#

Cucumber不提供直接获取Hook中场景完整信息的方法,有Scenario接口,只能给予非常有限的信息,如场景名称等,下面是该接口的其他直接方法。

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

相关问题