我正在尝试找出是否有一个选项来确定当前正在执行的黄瓜步骤,我正在尝试根据步骤名称执行特定的操作。
我可以看到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;
}

发布于 2019-09-20 21:32:07
您可以实现一个ConcurrentEventListener,在插件部分中设置它:
@RunWith(Cucumber.class)
@CucumberOptions(
...
plugin = {
...,
"com.mycompany.myproduct.AcceptanceStepNameLogger"
}...AcceptanceStepNameLogger示例(在这种情况下,我只需要记录PickleStepTestStep步骤,而不是钩子):
import cucumber.api.*;
import cucumber.api.event.*;
public class AcceptanceStepNameLogger implements ConcurrentEventListener {
@Override
public void setEventPublisher(EventPublisher publisher) {
publisher.registerHandlerFor(TestStepStarted.class, new EventHandler<TestStepStarted>() {
@Override
public void receive(TestStepStarted event) {
if (event.testStep instanceof PickleStepTestStep) {
final PickleStepTestStep ev = (PickleStepTestStep) event.testStep;
final String args = StringUtils.join(ev.getDefinitionArgument().stream().map(Argument::getValue).toArray(), ",");
String testDescription = ev.getStepText() + " : " + ev.getStepLocation();
if (StringUtils.isNotBlank(args)) {
testDescription += (" : args = (" + args + ")");
}
System.out.println("STARTING STEP: " + testDescription);
}
}
});
publisher.registerHandlerFor(TestStepFinished.class, new EventHandler<TestStepFinished>() {
@Override
public void receive(TestStepFinished event) {
if (event.testStep instanceof PickleStepTestStep) {
PickleStepTestStep ev = (PickleStepTestStep) event.testStep;
final String testDescription = ev.getStepText() + " : " + ev.getStepLocation();
System.out.println("FINISHED STEP: " + testDescription);
}
}
});
}
}发布于 2018-05-19 03:02:23
获取运行时变量的一种方法是使用plugin选项。尽管访问Cucumber version1.2.5的StepDefinitionMatch变量似乎是对Reporter接口的滥用。
为此,创建一个实现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) {}
}用于存储StepDefinitionMatch变量的ThreadLocal类。
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();
}
}runner类中的自定义插件的声明
@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,则会有一些更改。PickleTestStep替换了StepDefinitionMatch。runner中的声明保持不变。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());
}输出与之前相同。
发布于 2019-06-05 00:26:30
这是我在“黄瓜4”中做的
创建一个事件监听器类,并将其作为插件添加到cucumber选项中:
public class TestStepWatch implements ConcurrentEventListener {
private EventHandler<TestStepStarted> stepHandler = new EventHandler<TestStepStarted>()
{
@Override
Public Void receive(TestStepStarted event)
{
handleTestStep(event)
}
private synchronized void handleTestStep(TestStepStarted event)
{
System.out.println("Running step:"+ event.testStep.getStepText());
}
}
}https://stackoverflow.com/questions/50401007
复制相似问题