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.>
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() {
@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() {
@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);
}
}
});
}
}