HJW
HJW

Reputation: 23443

Same Step Definitions in Cucumber Gherkins

I have the following Gherkins:

Given i go to the URL
And i enter the <NRIC or FIN> as NRIC / FIN
And i select the option to proceed Next
And i select the first available available Handset Color
And i select the option to proceed Next
And i enter <Full Name> as Name
When i select the option to proceed Next

The "I select the option to proceed Next" appears three times. How should this be written as in the Step Definitions Java class file?

Upvotes: 1

Views: 1233

Answers (2)

James McCalden
James McCalden

Reputation: 3866

As well as the Declarative/Imperative question have a think about what requirement you're describing. I find it's helpful to include the Scenario: in the examples for this reason. It's very unusual to have that much detail in a Given step

Are you testing the creation of an order (bit of a guess)? If so then your enter/select steps should be the Whens in the scenario, so something like:

Scenario: Create a new order
    Given I have gone to the URL
    When I enter the NRIC/FIN: <NRIC/FIN> 
     And I choose the first available Handset Colour
     And I enter <Full Name> as Name
    Then my new order should be confirmed (or whatever)

However, if you're creating the order simply to set up the scenario to test something else then you can cheat and just state that the order should exist:

Scenario: Check the status of an order
    Given that I have created an order:
       | NRIC/FIN | Colour | Full Name |
       | xxxxx    | Red    | John Doe  |
    When I check the status of my order
    Then I should see a new order...

It's up to the automation whether it clicks through the screens to create that order or just inserts it into the database, all that matters is that by the time you get to the Whens the order should exist.

In an ideal BDD world the Gherkin would be written before the implementation, but it often doesn't work that way. I still find it useful to try and pretend I'm writing the Features in that ideal world. Asking "How would I have written this before we started development?" can help to separate the actual requirements (I can enter an order) from implementation details (I click Next after entering each item of data).

Upvotes: 3

Bala
Bala

Reputation: 11234

When writing cucumber scenarios you can adopt either a imperative or declarative style. I would prefer to write the same thing as below.

Given i go to the URL
And i enter NRIC / FIN
And i select the first available Handset Color
And i enter <Full Name> as Name
Then I should see that

So it depends on who is going to read your scenarios. A link worth reading is imperative - declarative

Upvotes: 1

Related Questions