我可以重复步骤而不重复使用 Cucumber 的步骤吗?

Can I repeat steps without repeating the steps using Cucumber?

我需要测试当用户多次重复一组操作时触发的行为。我想结束一个看起来像这样的场景:

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

而不是像这样的场景:

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

是否有标准的方法来执行此操作,还是我必须自己实施?

如果您可以控制步骤定义代码,请尝试这样的步骤 -

And I press Continue button and go back 5 times

用变量捕获步骤定义中的次数,并在循环中调用现有函数的次数。

要重复任意步骤而不必每次都写一个新步骤,请定义此步骤

When /^I repeat "([^"]*)" (\d+) times$/ do |steps, times|
  times.to_i.times do
    steps.split(/;\s*/).each do |step|
      step step
    end
  end
end

并像这样使用它:

When I repeat "press the Continue button; go back" 5 times

(不要在要重复的步骤名称中使用分号。)