在运行时

时间:2018-05-17 21:51:09

标签: cucumber cucumber-jvm cucumber-java

我试图找出是否有一个选项可以找出当前正在执行的黄瓜步骤,我正在尝试根据步骤名称执行某些操作。

我可以看到StepDefinitionMatch类获取了这些步骤,但我不确定如何在运行时访问这些步骤。有帮助吗?如果有帮助,请添加调用堆栈的快照。

public StepDefinitionMatch(List<Argument> arguments, StepDefinition stepDefinition, String featurePath, Step step, LocalizedXStreams localizedXStreams) {
    super(arguments, stepDefinition.getLocation(false));
    this.stepDefinition = stepDefinition;
    this.featurePath = featurePath;
    this.step = step;
    this.localizedXStreams = localizedXStreams;
}

enter image description here

6 个答案:

答案 0 :(得分:0)

只需等待Cucumber 3.0.0版本,您就可以使用@AfterStep和@BeforeStep Annotations访问步骤名称。

  

https://github.com/cucumber/cucumber-jvm/blob/master/CHANGELOG.md   https://github.com/cucumber/cucumber-jvm/pull/1323

感谢Aniket(Coding-Yogi)https://github.com/coding-yogi

答案 1 :(得分:0)

获取运行时变量的一种方法是使用插件选项。虽然看似滥用Reporter界面来访问 Cucumber 1.2.5版StepDefinitionMatch变量。

为此创建一个实现Reporter接口的自定义类。

public class CustomFormatter implements Reporter{

    public  CustomFormatter() { }

    @Override
    public void before(Match match, Result result) {}

    @Override
    public void result(Result result) {}

    @Override
    public void after(Match match, Result result) {}

    @Override
    public void match(Match match) {
        ThreadLocalStepDefinitionMatch.set((StepDefinitionMatch)match);
    }

    @Override
    public void embedding(String mimeType, byte[] data) {}

    @Override
    public void write(String text) {}
}

用于存储ThreadLocal变量的StepDefinitionMatch类。

public class ThreadLocalStepDefinitionMatch {

    private static final ThreadLocal<StepDefinitionMatch> threadStepDefMatch = new InheritableThreadLocal<StepDefinitionMatch>();

    private ThreadLocalStepDefinitionMatch() {
    }

    public static StepDefinitionMatch get() {
        return threadStepDefMatch.get();
    }

    public static void set(StepDefinitionMatch match) {
        threadStepDefMatch.set(match);
    }

    public static void remove() {
        threadStepDefMatch.remove();
    }
}

跑步者类

中的自定义插件声明
@CucumberOptions(plugin = { "pretty", "html:report", "json:reports.json",
        "rerun:target/rerun.txt", "cusform.CustomFormatter" }

最后访问步骤定义类

中的StepDefinitionMatch变量
@When("^user gets count from \"([^\"]*)\"$")
    public void userGetsCountFromAndStores(String arg) {
        StepDefinitionMatch match = ThreadLocalStepDefinitionMatch.get();
        System.out.println(match.getStepName());
        System.out.println(match.getPattern());
        System.out.println(match.getArguments());
    }

控制台输出提供以下内容

user gets count from "Car1"
^user gets count from "([^"]*)"$
[Car1]

你使用的是非常古老的Cucumber版本,如果升级到Cucumber 2,会有一些变化。 StepDefinitionMatch替换为PickleTestStep。跑步者的声明保持不变。 ThreadLocal类将存储的类更改为PickleTestStep

自定义格式化程序

public class CustomFormatter implements Formatter {

    public CustomFormatter() {}

    private EventHandler<TestStepStarted> stepStartedHandler = new EventHandler<TestStepStarted>() {
        @Override
        public void receive(TestStepStarted event) {
            handleTestStepStarted(event);
        }
    };

    @Override
    public void setEventPublisher(EventPublisher publisher) {
        publisher.registerHandlerFor(TestStepStarted.class, stepStartedHandler);
    }

    private void handleTestStepStarted(TestStepStarted event) { 
        if(event.testStep instanceof PickleTestStep) {
            ThreadLocalPickleStep.set((PickleTestStep)event.testStep);
        }

    }
}

访问步骤定义类

中的StepDefinitionMatch变量
@When("^user gets count from \"([^\"]*)\"$")
    public void userGetsCountFromAndStores(String arg) {

        System.out.println(ThreadLocalPickleStep.get().getStepText());
        System.out.println(ThreadLocalPickleStep.get().getPattern());
        System.out.println(ThreadLocalPickleStep.get().getDefinitionArgument());
    }

输出与以前相同。

答案 2 :(得分:0)

这是我在黄瓜4中所做的

创建一个事件侦听器类,并将其添加为黄瓜选项中的插件:

import 'dart:io';
import 'dart:convert';


// ...

new File('/data/user/0/com.example.foo/app_flutter/config.json')
    .readAsString()
    .then((fileContents) => JSON.decode(fileContents))
    .then((jsonData) {
        // do whatever you want with the data
    });

答案 3 :(得分:0)

您可以实现ConcurrentEventListener,在插件部分进行设置:

header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');

AcceptanceStepNameLogger示例(在这种情况下,我只需要记录PickleStepTestStep步骤,而不是钩子):

@RunWith(Cucumber.class)
@CucumberOptions(
...
plugin = {
   ...,
   "com.mycompany.myproduct.AcceptanceStepNameLogger"
}...

答案 4 :(得分:0)

下面的代码可用于获取当前的步骤方法名称

new Throwable().getStackTrace()[0].getMethodName()

示例: 给定用户位于登录页面上

@Given(“ ^用户在登录页面上$”)     公共无效user_is_on_Login_page()抛出Throwable {

}

new Throwable()。getStackTrace()[0] .getMethodName()=>它将返回当前步骤方法名称为“ user_is_on_Login_page”

答案 5 :(得分:0)

如果您使用的黄瓜-jvm版本是5.6.0或最新版本,请遵循以下解决方案。 由于不推荐使用Reporter类,因此您必须实现ConcurrentEventListener并将该类添加到TestRunner插件部分。

public class StepDetails implements ConcurrentEventListener {
    public static String stepName;

    public EventHandler<TestStepStarted> stepHandler = new EventHandler<TestStepStarted>() {
        @Override
        public void receive(TestStepStarted event) {
            handleTestStepStarted(event);
        }

    };

    @Override
    public void setEventPublisher(EventPublisher publisher) {
        publisher.registerHandlerFor(TestStepStarted.class, stepHandler);
    }

    private void handleTestStepStarted(TestStepStarted event) {
        if (event.getTestStep() instanceof PickleStepTestStep) {
            PickleStepTestStep testStep = (PickleStepTestStep)event.getTestStep();
            stepName = testStep.getStep().getText();
        }


    }
}

稍后将此类添加到您的运行程序文件的cucumberOptions的插件部分中

@CucumberOptions(dryRun=false,plugin = {"<yourPackage>.StepDetails"})

您只需调用stepName变量即可在任意位置获取步骤名

System.out.println(StepDetails.stepName);