문제

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

도움이 되었습니까?

해결책

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

다른 팁

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 |
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top