如何使用黄瓜Java获取场景步骤?

问题描述

我想在运行黄瓜方案之前获得此方案步骤,并且我有一个测试结果json文件,我将在该json文件中添加正在运行方案的步骤,但是现在我无法获得步骤,您能帮我吗?

解决方法

在您的StepDefinition类中使用它:

@BeforeStep
public void doSomethingBeforeStep(Scenario scenario) throws Exception {
    testScenario = scenario.getName().toString();
    scenarioObj = scenario;
    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());

    PickleStepTestStep currentStepDef = stepDefs.get(currentStepIndex);
    testCase = currentStepDef.getStepText();
}

@AfterStep
public void doSomethingAfterStep(Scenario scenario) {
    currentStepDefIndex += 1;
}

贷记this帖子。还有API doc

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...