Created
May 11, 2016 07:10
-
-
Save daipresents/d3847e8d747d4f7bfe49a7eb0f9b9a20 to your computer and use it in GitHub Desktop.
WebDriverのWaitを使ってDOMを監視したり意図的に待つ方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @driver.manage.timeouts.implicit_wait = 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1) Test test_ | |
| Failure/Error: @driver.get(@base_url) | |
| Timeout::Error: | |
| Timeout::Error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| begin | |
| wait.until { | |
| value == 2 | |
| } | |
| rescue Selenium::WebDriver::Error::TimeOutError => e | |
| p e.message # => timed out after X seconds | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| verify(driver, "http://daipresents.com") { ~~ } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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