Skip to content

Instantly share code, notes, and snippets.

@jarib
Created February 16, 2010 22:50
Show Gist options
  • Save jarib/306034 to your computer and use it in GitHub Desktop.
Save jarib/306034 to your computer and use it in GitHub Desktop.
wait in selenium-webdriver
#!/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