JAVA-如何使用小黄瓜语法(给出,何时,然后和)获得完整的测试步骤

时间:2019-06-30 23:26:44

标签: java cucumber bdd cucumber-jvm cucumber-java

所以我需要在小黄瓜语法之后获得测试步骤说明

Feature: User trades stocks   Scenario: User requests a sell before close of trading
Given I have 100 shares of MSFT stock
   And I have 150 shares of APPL stock
   And the time is before close of trading

所以我真正需要的是获得

I have 100 shares of MSFT stock

I have 150 shares of APPL stock

the time is before close of trading

1 个答案:

答案 0 :(得分:0)

这是黄瓜v4.3.1的代码解决方案(在serviceHook类中)。

PickleStepTestStep currentStep;
private int counter = 0;

@BeforeStep
public void getStepName(Scenario scenario) throws Exception {

    Field f = scenario.getClass().getDeclaredField("testCase");
    f.setAccessible(true);
    TestCase r = (TestCase) f.get(scenario);

    List<PickleStepTestStep> stepDefs = r.getTestSteps()
            .stream()
            .filter(x -> x instanceof PickleStepTestStep)
            .map(x -> (PickleStepTestStep) x)
            .collect(Collectors.toList());

    currentStep = stepDefs.get(counter);

        System.out.println(currentStep.getStepText());

    }

@AfterStep
public void afterStep(Scenario scenario) {
    counter += 1;
}