How to call a step from another step in Cucumber-JVM

Eric Hartford picture Eric Hartford · Mar 27, 2014 · Viewed 12k times · Source

In Cucumber (the ruby version) you can easily call steps from other steps and thus build hierarchical libraries of steps making it easy to write the Gherkin feature specifications in the most generic terms.

However it is not readily apparent how to do this in Cucumber-JVM and I have been unable to find documentation for it.

Let me be clear I am not interested in calling the step implementation function directly because I don't want to have to know what its signature is, nor to change the call every time the implementation changes.

Rather, I want to pass an arbitrary string that will go through the regex matcher and automatically find the matching step and execute it. Just as the engine runs all steps.

simple example of what I would expect syntax to look like to define synonym "logout":

When("user logs out") { () =>
  d.executeScript("logout();")
}
When("logout") { () =>
  Step("user logs out")
}

Answer

Seb Rose picture Seb Rose · Apr 15, 2014

This functionality is not supported in Cucumber-JVM. (Note that the Cucumber Backgrounder document you link in your question describes using Steps within Steps as "an anti-pattern")

Essentially, we believe that Cucumber is a collaboration tool and that Gherkin is not a programming language.

You can see a longer discussion of how we arrived at this decision here