Brazen
Brazen

Reputation: 295

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.

Upvotes: 0

Views: 174

Answers (1)

niharika_neo
niharika_neo

Reputation: 8531

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

Upvotes: 1

Related Questions