[英]How to make sure that multiple elements of same class have been loaded on page using watir?
我正在用黃瓜和燕麥粥。 問題是參考下面的代碼:
When(/^I click on all 'Show more'$/) do
@browser.links(:class, "more-matches").each do |d|
if d.text == "Show more"
d.click
end
end
end
現在,當測試用例達到此步驟定義時,測試用例將顯示為已通過,而無需單擊使用@ browser.links(:class,“ more-matches”)捕獲的所有鏈接。 特定代碼未實現可能是因為ajax調用尚未完成,並且數組包含零個元素且未循環通過。 如果我在此步驟定義的開頭引入"sleep 2"
,該代碼將起作用。 誰能告訴我如何通過添加代碼來處理這種情況,以便ajax調用已完成並且數組成功保存所有元素並循環。 我也嘗試添加代碼:
if @browser.execute_script('return jQuery.active').to_i == 0
但效果不佳。 請提出一種步驟定義被執行且由於空數組而不會通過的方法。
使用Element#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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.