Reputation: 4805
I'm looking for suggestions or best practices here.
Sometimes I would like to give the same regex/phrase for 2 different steps using different prepositions. The case I see often is a phrase that makes sense in both Given
and in Then
steps.
Given
being used for a step that performs and action (to setup the test state).
And Then
being used for a step that verifies the state, but doesn't perform an action. Quite often I find the same phrasing works right for both. An example such phrase is "I am logged in"
Here is a contrived scenario
Scenario: Login is saved between sessions.
Given I am logged in
When I exit the application
And I start the application
Then I am logged in
Of course this doesn't work in the step definitions because cucumber doesn't differentiate between step predicates.
I've toyed with adding 'verify' to all colliding Then
steps, or adding a 'that' to all duplicated Given
steps. But nothing sits right.
Given that I am logged in.
...
Then verify I am logged in.
It works of course, but I feel I'm missing something that doesn't require having the same "extended predicate" for most of my steps.
Upvotes: 6
Views: 3408
Reputation: 601
I've had the same question, this is the answer I got:
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"
Upvotes: 1