Question

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

Was it helpful?

Solution

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

OTHER TIPS

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 |
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top