在运行时提取黄瓜步骤名称 [英] Extracting Cucumber Step name at runtime

查看:125
本文介绍了在运行时提取黄瓜步骤名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



<我可以看到StepDefinitionMatch类可以获取这些步骤,但是我不确定如何在运行时访问这些步骤。有什么帮助吗?

  public StepDefinitionMatch(List< Argument>参数,StepDefinition stepDefinition,字符串featurePath,步骤step,LocalizedXStreams localizedXStreams){
super(arguments,stepDefinition.getLocation(false));
this.stepDefinition = stepDefinition;
this.featurePath = featurePath;
this.step = step;
this.localizedXStreams = localizedXStreams;
}

解决方案

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

  @RunWith(Cucumber.class)
@CucumberOptions(
...
插件= {
...,
com。 mycompany.myproduct.AcceptanceStepNameLogger
} ...

AcceptanceStepNameLogger示例(在本例中,我只需记录PickleStepTestStep的步骤,而不是钩子):

  import banana.api。*; 
import banana.api .event。*;

公共类AcceptanceStepNameLogger实现ConcurrentEventListener {

@Override
public void setEventPublisher(EventPublisher Publisher){

Publisher .registerHandlerFor(TestStepStarted.class,new EventHandler< TestStepSta rted>(){
@Override
public void receive(TestStepStarted事件){
if(event.testStep instanceof PickleStepTestStep){
final PickleStepTestStep ev =(PickleStepTestStep)event.testStep ;
final String args = StringUtils.join(ev.getDefinitionArgument()。stream()。map(Argument :: getValue).toArray(),,);
字符串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(完成步骤: + testDescription);
}
}
});

}

}


I am trying to find out if there is an option to figure out the cucumber step currently getting executed, I am trying to perform certain action depending on the step name.

I can see StepDefinitionMatch class gets the steps, but I am not sure how can I access the steps at runtime. Any help? Adding a snapshot of the call stack if that helps.

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;
}

解决方案

You can implement a ConcurrentEventListener, setting it in the plugin section:

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

AcceptanceStepNameLogger example (in this case I only needed to log PickleStepTestStep steps, not hooks):

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);
                }
            }
        });

    }

}

这篇关于在运行时提取黄瓜步骤名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆