Does anyone know if cucumber/Java supports the functionality of multiple steps to access the same stepDefinition?
What I am trying to achieve is to use as a Given a step that at the moment is used as a When (in another more complex test where the current When becomes just a precondition)
In my previous experience (Specflow/.Net) I could use something like
[Given(@"the user added a (.*) to the cart")]
[When(@"the user adds a (.*) to the cart")]
public void WhenTheUserAddsATotheCart(string product){
////code that adds to the cart
}
If I use the same methodology as in Specflow, a DuplicateStepDefinitionException is thrown. Because Cucumber doesn't take the key words (Given, When etc.) into consideration when matching the Steps with the step definitions. Cucumber only looks for a regEx in the step definitions so I would need to have only one step definition (Given OR When). Meaning
[When(@"the user adds a (.*) to the cart")]
will also match
[Given(@"the user adds a (.*) to the cart")]
My problem is that we use the Given at past tense and the When at present tense.
So the question is: how can I use the same implementation for two step definitions?
The only solution I see is to extract the contains of the method that implements the Given into another method and call it under both Given step definition and under When.Something like:
[Given(@"the user added a (.*) to the cart")]
public void GivenTheUserAddedATotheCart(string product){
addToTheCart(product);
}
[When(@"the user adds a (.*) to the cart")]
public void WhenTheUserAddsATotheCart(string product){
addToTheCart(product);
}
public void addToTheCart(String product){
////code that adds to the cart
}
... but I don't really find this a nice solution