满足条件时停止 until 循环

Stop `until`-loop when condition is met

我想确保以下测试不会陷入无限循环。我只想循环 运行 一定次数。

When /^I click the Settings link$/ do
  footer = @browser.div(:id, 'iwinbottombar')
  footer.wait_until_present
  unless footer.html.include?('Settings')
    throw Exception.new("Expected to see the settings button but it was not there")
  end
  until @browser.div(:id, 'dialogcontainer').div(:class, 'settings_browsebutton button clickable').present? do
    footer.a(:class, 'button_settings').when_present.click
  end
end

但我不确定如何限制代码来执行此操作。我想我需要一个计数器,但我不确定如何实现它。

如果我没听错,你想要类似 while/until 但最大重试次数:

10.times do
  break if condition_met?
  do_whatever
end