Skip to content
Advertisement

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

I’m logging the currently executing Scenario in a hook like so:

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

But, how do I get the name of the Step? I mean, the Given, When, Then lines.

Advertisement

Answer

You would need to implement the ConcurrentEventListener interface and setup the handlers for the event you are looking for, in your case the TestStepStarted event

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

Then implement the method handTestStepStarted

private void handleTestStepStarted(TestStepStarted testStepStartedEvent) {
    if(testStepStartedEvent.getTestStep() instanceof PickleStepTestStep) {
        PickleStepTestStep pickleStepTestStep = (PickleStepTestStep)testStepStartedEvent.getTestStep();
        //do what you want with it
    } 
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement