如何获取当前正在执行的场景步骤的名称?

How do I get the name of the Scenario step currently executing?

我正在像这样在挂钩中记录当前正在执行的场景:

@Before(order=2)
public void logScenarioStarting(Scenario scenario) {
  logger.debug("Starting scenario {}.", scenario.getName();
}

但是,如何获取步骤的名称?我的意思是,GivenWhenThen 行。

您需要实现 ConcurrentEventListener 接口并为您要查找的事件设置处理程序,在您的情况下为 TestStepStarted 事件

@Override
public void setEventPublisher(EventPublisher publisher) {
    publisher.registerHandlerFor(TestStepStarted.class, this::handleTestStepStarted);
    publisher.registerHandlerFor(TestStepFinished.class, this::handleTestStepFinished);
    
}

然后实现方法handTestStepStarted

private void handleTestStepStarted(TestStepStarted testStepStartedEvent) {
    if(testStepStartedEvent.getTestStep() instanceof PickleStepTestStep) {
        PickleStepTestStep pickleStepTestStep = (PickleStepTestStep)testStepStartedEvent.getTestStep();
        //do what you want with it
    } 
}