使用元素#wait_until_present
通常,您会知道应该存在多少个链接。因此,您可以等到出现预期数量的链接。
When(/^I click on all 'Show more'$/) do
# Wait for the expected number of links to appear
# (note that :index is zero-based, hence the minus 1)
expected_number = 5
@browser.link(:class => "more-matches",
:index => (expected_number-1)).wait_until_present
# Click the links
@browser.links(:class, "more-matches").each do |d|
if d.text == "Show more"
d.click
end
end
end
如果您不知道预期有多少链接,那么确保一致性会变得更加困难。但是,您可能只需检查是否存在至少一个链接即可。希望如果一个人在场,那么所有其他人都在场。
When(/^I click on all 'Show more'$/) do
# Wait until at least one link appears
@browser.link(:class => "more-matches").wait_until_present
# Click the links
@browser.links(:class, "more-matches").each do |d|
if d.text == "Show more"
d.click
end
end
end
使用浏览器#wait_until
另一种方法是使用wait_until
. 等待至少 5 个链接可以重写为:
When(/^I click on all 'Show more'$/) do
# Wait for the expected number of links to appear
expected_number = 5
@browser.wait_until do
@browser.links(:class => "more-matches").length >= expected_number
end
# Click the links
@browser.links(:class, "more-matches").each do |d|
if d.text == "Show more"
d.click
end
end
end