Cucumber: repeat only one step several times

7.3k Views Asked by At

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

3

There are 3 best solutions below

1
On BEST ANSWER

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.

0
On

You don't need 3 different steps as scenario outline will automatically generates different test depends on data in "Examples:". In your example SpecFlow will generated 3 different test as you have 3 rows in "Examples:". Big story short, You need only one scenario and it will execute n many times, where n is the number of rows in "Examples:".

0
On

You have to write three steps with different parameter and if you use scenario outline then, all steps are repeated for each scenario. As per your requirement, you can try the following steps.

Scenario: test
   Given I am on page X
   When  I fill the "John" on field "name1"
   When  I fill the "Max" on field "name2"
   When  I fill the "Paul" on field "name3"
   And   I click on Ok button
   Then  I should see something