Search code examples
cucumberbddcucumber-java

Cucumber: repeat only one step several times


If I have a scenario outilne like this:

Scenario Outline: test
Given I am on page X
When I fill the <name> on field <fieldID>
And I click on Ok button
Then I should see something

Examples:
name   | fieldID   |
"Jhon" | "name1"|
"Max" | "name2" |
"Paul" | "name3"|

can I run just the "When" step 3 times and then click ok? or do I have to write all the 3 diferent steps? I need those 3 informations to click ok, is not like a login that I test 3 times with diferent login values


Solution

  • You can write your scenario also like:

    Scenario Outline: test
    Given I am on page X
    When I fill in the following names
     name   | fieldID |
     "Jhon" | "name1" |
     "Max"  | "name2" |
     "Paul" | "name3" |
    And I click on Ok button
    Then I should see something
    

    The table will then be supplied as an array to the steps implementation for your When statement.

    A question I could ask here is do the actual names really matter? If not, then you could also simply write When I fill in 3 names and just use the steps method to fill in some arbitrary names.