gpt4 book ai didi

cucumber - 在运行时提取 cucumber 步骤名称

转载 作者:行者123 更新时间:2023-12-04 21:31:19 24 4
gpt4 key购买 nike

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

我可以看到 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

最佳答案

你可以实现一个 ConcurrentEventListener,在插件部分设置它:

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

AcceptanceStepNameLogger 示例(在这种情况下,我只需要记录 PickleStepTestStep 步骤,而不是钩子(Hook)):
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);
}
}
});

}

}

关于cucumber - 在运行时提取 cucumber 步骤名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50401007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com