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.
I can see StepDefinitionMatch class gets the steps, but I am not sure how can I access the steps at runtime. Any help? Adding a snapshot of the call stack if that helps.
public StepDefinitionMatch(List<Argument> arguments, StepDefinition stepDefinition, String featurePath, Step step, LocalizedXStreams localizedXStreams) {
super(arguments, stepDefinition.getLocation(false));
this.stepDefinition = stepDefinition;
this.featurePath = featurePath;
this.step = step;
this.localizedXStreams = localizedXStreams;
}
Just wait for Cucumber 3.0. 0 release, you can access the step names using @AfterStep and @BeforeStep Annotations. What is the object that needs to be passed to the AfterStep and BeforeStep? The Scenario object getName() returns the description on the scenario.
Inside the step definition, you can use CucumberHelper. scenario. getName() . Based on this API you can use getID , getSourceTagNames , getStatus and getClass methods.
If Cucumber is telling you that your steps are undefined, when you have defined step definitions, this means that Cucumber cannot find your step definitions. You'll need to make sure to specify the path to your step definitions (glue path) correctly.
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<TestStepStarted>() {
@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<TestStepFinished>() {
@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);
}
}
});
}
}
If you are using cucumber-jvm version that is 5.6.0 or latest please follow below solution. since Reporter class is deprecated you have to implement ConcurrentEventListener and add this class to your TestRunner plugin section.
public class StepDetails implements ConcurrentEventListener {
public static String stepName;
public EventHandler<TestStepStarted> stepHandler = new EventHandler<TestStepStarted>() {
@Override
public void receive(TestStepStarted event) {
handleTestStepStarted(event);
}
};
@Override
public void setEventPublisher(EventPublisher publisher) {
publisher.registerHandlerFor(TestStepStarted.class, stepHandler);
}
private void handleTestStepStarted(TestStepStarted event) {
if (event.getTestStep() instanceof PickleStepTestStep) {
PickleStepTestStep testStep = (PickleStepTestStep)event.getTestStep();
stepName = testStep.getStep().getText();
}
}
}
later add this class to your plugin section of cucumberOptions of your runner file
@CucumberOptions(dryRun=false,plugin = {"<yourPackage>.StepDetails"})
you can get the stepname wherever you want just by calling the stepName variable
System.out.println(StepDetails.stepName);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With