Search code examples
rubycucumber

Can I repeat steps without repeating the steps using Cucumber?


I need to test behaviour that triggers when a user repeats a set of actions many times. I would like to end up with a scenario that looks something like this:

Scenario: Nice way
  Given that I am on some screen
  When I enter something into some text field
  And I press the Continue button
  And I go back
  And I repeat the previous 2 steps 5 times
  Then the application should crash

Rather than a scenario that looks like this:

Scenario: Annoying way
  Given that I am on some screen
  When I enter something into some text field
  And I press the Continue button
  And I go back
  And I press the Continue button
  And I go back
  And I press the Continue button
  And I go back
  And I press the Continue button
  And I go back
  And I press the Continue button
  And I go back
  And I press the Continue button
  And I go back
  Then the application should crash

Is there a standard way of doing this or do I have to implement it myself?


Solution

  • If you have control over the step definition code try a step like this -

    And I press Continue button and go back 5 times
    

    Capture the number of times in the step definition with a variable and call existing functions in a loop for the number of times.

    It can be captured by something like:

    @And("^I press Continue button and go back (//d+) times")
    public void iPressContinueButtonAndGoBackNTimes(int n){
        //enter code here
    }