3

I am going through some examples and some books in relation to Cucumber usage and it looks like @And annotation is never being caught by the regex. I mean it is a valid Gherkin keyword and it is being used in the scenarios but I can see no references to it in the actual code.

Why is that?

2 Answers 2

7

From The Cucumber Book:

Cucumber doesn’t actually care which of these keywords you use; the choice is simply there to help you create the most readable scenario.

Some people find Given, When, Then, And, and But a little verbose. There is an additional keyword you can use to start a step: * (an asterisk).

So you could write:

Scenario: Attempt withdrawal using stolen card
Given I have $100 in my account
But my card is invalid
When I request $50
Then my card should not be returned
And I should be told to contact the bank

Or:

Scenario: Attempt withdrawal using stolen card
Given I have $100 in my account
Given my card is invalid
When I request $50
Then my card should not be returned
Then I should be told to contact the bank

Or:

Scenario: Attempt withdrawal using stolen card
* I have $100 in my account
* my card is invalid
* I request $50
* my card should not be returned
* I should be told to contact the bank

To Cucumber, this is exactly the same scenario.

0

It happens, because @AND keyword is parsed like the keyword prior to it. So, in case your test looks like that

@When user logs in application
@And navigates to landing page

it's similar to the the case below

@When user logs in application
@When navigates to landing page

Actually, I stick to avoid using @AND keywords, because binding mismatch could occur if someone will insert a step which begins from different keyword. E.g.:

@When user logs in application
**@Then check that userpic is displayed**
@And navigates to landing page

In this example @AND keyword will be remapped to keyword @THEN.

1
  • But, as the accepted answer says, the keywords are irrelevant, so there is no "binding mismatch" even if somebody inserts a step with a different keyword above And.
    – Leponzo
    Commented Feb 14, 2021 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.