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.
Thanks to Aniket (Coding-Yogi) https://github.com/coding-yogi
One way to get hold of the runtime variables is to use the plugin option. Though it seems like an abuse of the
Reporter
interface to access theStepDefinitionMatch
variable for Cucumber version 1.2.5.For this create a custom class which implements the
Reporter
interface.The
ThreadLocal
class to store theStepDefinitionMatch
variable.The declaration for custom plugin in the runner class
Finally accessing the
StepDefinitionMatch
variable in the step definition classThe console output gives the following
You are using a pretty old Cucumber version, if you upgrade to Cucumber 2 there will be some changes. The
StepDefinitionMatch
replaced byPickleTestStep
. The declaration in the runner remains the same. TheThreadLocal
class change the stored class toPickleTestStep
.The custom formatter becomes
Accessing the StepDefinitionMatch variable in the step definition class
The output is the same as before.