Search code examples
cucumbercucumber-jvmgherkincucumber-java

Java+Cucumber: get the current tag/abverb being executed


I am trying to print the current step being executed in Cucumber. I am using a custom formatter to print the step definition. However, I also want to print the current adverb (Given, When, Then, And...) that is being executed. I might be missing something as well, is it possible in Cucumber? Here is my code:

Formatter:

public class MyCucumberFormatter implements ConcurrentEventListener {

    @Override
    public void setEventPublisher(EventPublisher publisher) {
        publisher.registerHandlerFor(TestStepStarted.class, runStartedHandler);
    }

    private EventHandler<TestStepStarted> runStartedHandler = new EventHandler<TestStepStarted>() {
        @Override
        public void receive(TestStepStarted event) {
            startReport(event);
        }
    };

    private void startReport(TestStepStarted event) {
        if (!(event.testStep instanceof PickleStepTestStep)) {
            return;
        }
        PickleStepTestStep testStep = (PickleStepTestStep) event.testStep;
        log("Step: " + testStep.getStepText());
    }
}

Example scenario:

Scenario: Test user life cycle: create user, activate and delete
  Given A valid admin logs in
  When Admin creates new user
  And User is activated
  Then User should successfully login

Right now, it prints as:

    A valid admin logs in
    Admin creates new user
    User is activated
    User should successfully login

I want it to print as:

Given A valid admin logs in
When Admin creates new user
And User is activated
Then User should successfully login


Solution

  • You can't do this in v4.x yet but you can do this in v5.0.0-RC1 by going through pickleStepTestStep.getStep().getKeyWord()