Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save daipresents/d3847e8d747d4f7bfe49a7eb0f9b9a20 to your computer and use it in GitHub Desktop.

Select an option

Save daipresents/d3847e8d747d4f7bfe49a7eb0f9b9a20 to your computer and use it in GitHub Desktop.
WebDriverのWaitを使ってDOMを監視したり意図的に待つ方法
@driver.manage.timeouts.implicit_wait = 10
# verify { @driver.find_element(:xpath, "/html/body/div/div").text.should == 1 } のように使う
def verify(&blk)
# 10秒待ちながら確認を続ける
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
begin
wait.until {
begin
yield
rescue ExpectationNotMetError
# RSpecのshouldなどはマッチしないとExpectationNotMetErrorを投げるので
# 待っている間はもみけす
end
}
ensure
# 最後にもう一回実行しなければ検証が成功したのかどうかわからない
yield
end
rescue ExpectationNotMetError => e
# 検証失敗の場合は結果を配列に突っ込む
@verification_errors << e
end
1) Test test_
Failure/Error: @driver.get(@base_url)
Timeout::Error:
Timeout::Error
begin
wait.until {
value == 2
}
rescue Selenium::WebDriver::Error::TimeOutError => e
p e.message # => timed out after X seconds
end
verify(driver, "http://daipresents.com") { ~~ }
wait.until {
begin
yield
rescue ExpectationNotMetError
driver.get(url)
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment