Cucumber feature files are throwing an error (multiple step definitions declared) if I try to have two similar step definitions but with different annotations -
@Then("^element with id \"([^\"]*)\" is displayed$")
&
@Given("^element with id \"([^\"]*)\" is displayed$")
So far I've fooled it by putting an extra space in the @Then definition (look carefully at @Then, after the regex).
But this is not good practice.
I want the @Given, because it sets up up my scenario, and I definitely need the @Then.
How do I get passed this?
When cuking a Given should set up state and a Then should do assertions on content. Your Given is badly constructed because it is about asserting whats on the page. You should rewrite it so it describes WHAT you need to do to ensure that a particular element can be seen.
For example if your Given is matching
Given "element with id 'product_foo' is displayed"
That Given should be reworded in the scenario to read
Given 'there is a product foo'
Realistically you can't implement
Given("^element with id \"([^\"]*)\" is displayed$")
for general usage in even a small application, because it would have to know how to create everything that could possibly be displayed in your application in any context. You might see the id product_foo on the product page, or when its been ordered, or in your basket. The first just requires a product, but the second requires a previous order and the third requires a session with the product in the basket.Step Definitions have to be unique for Cucumber to know what to execute. In addition, the Given/When/Then keywords are technically interchangeable. They're for the readability of the feature file, but not linked to the implementation. So if both steps (Given and Then) do the same thing, there is technically no problem; you should be able to use the same step definition from your feature file, preceded by either 'Given' or 'Then' keyword. That said, you might want to consider rewriting your step definitions to describe the intended behavior instead of the implementation, e.g. "Given an element with id xxx"