Reputation: 2821
Is it possible to loop ONLY identified steps in cucumber scenario
For instance in the following example if I want to loop other steps except first. Because first step should run ONLY once
Scenario Outline: submit guess
Given I am logged as Admin
And the secret code is <code>
When I guess <guess>
Then the mark should be <mark>
Examples: all colors correct
| code | guess | mark |
| r g y c | r g y c | bbbb |
| r g y c | r g c y | bbww |
| r g t g | r g w e | bbpp |
Is this possible in cucumber? Currently it runs all the steps four times as per the parameter values.
Thanks
Upvotes: 3
Views: 15798
Reputation: 11244
It can be easily achieved using variables that act like a flag. Something like this
Before do
if !$ran_once
$ran_once = false
end
end
Given(/^I am logged as Admin$/) do
do something unless $ran_once
$ran_once = true
end
Refer https://docs.cucumber.io/cucumber/api/#hooks
Upvotes: 2
Reputation: 21
A Scenario Outline is meant to run the whole "scenario" but with different data each time. While you could setup a flag in the before hook, and then flag it the first time the step is run, you are leaking test state. This is generally a bad practice.
Instead it would be better to just use a scenario that uses a table for the validation step:
Scenario: Submitting guesses
Given I am logged as Admin
Then my guesses display the correct colors:
| code | guess | mark |
| r g y c | r g y c | bbbb |
| r g y c | r g c y | bbww |
| r g t g | r g w e | bbpp |
Upvotes: 2