Created
February 16, 2010 22:50
-
-
Save jarib/306034 to your computer and use it in GitHub Desktop.
wait in selenium-webdriver
This file contains 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
#!/usr/bin/env ruby | |
require "rubygems" | |
require "selenium-webdriver" | |
module Wait | |
extend self | |
def until(timeout = 10) | |
end_time = Time.now + timeout | |
while Time.now < end_time | |
begin | |
return if yield | |
rescue Selenium::WebDriver::Error::WebDriverError | |
end | |
sleep 0.1 | |
end | |
raise "timed out" | |
end | |
end | |
if __FILE__ == $0 | |
File.open("/tmp/test.html", "w") { |file| file << DATA.read } | |
driver = Selenium::WebDriver.for :firefox | |
driver.get "file:///tmp/test.html" | |
p [Time.now, 'page loaded'] | |
Wait.until do | |
driver.first(:id => 'hello').displayed? | |
end | |
p [Time.now, 'element found'] | |
Wait.until do | |
!driver.first(:id => 'hello').displayed? | |
end | |
p [Time.now, 'element gone'] | |
driver.quit | |
end | |
__END__ | |
<html> | |
<head> | |
<title>Slow loader</title> | |
<script> | |
function showElement () { | |
document.getElementById("hello").style.display = 'inline'; | |
} | |
function hideElement () { | |
document.getElementById("hello").style.display = 'none'; | |
} | |
function load () { | |
setTimeout(showElement, 5000) | |
setTimeout(hideElement, 10000) | |
} | |
</script> | |
</head> | |
<body onload='load()'> | |
<div id="hello" style="display: none;"> | |
hello | |
</div> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment