问题
I am using Watir to browse pages and take screenshots of some pages in my application.
However, getting a page from my server takes a while, and I get Timeout::Error.
To fix this, I used a wait in my Watir browser instance, to check to see if a div with id 'content' exists, and to make it wait until it exists. However, it takes some time, and the page is loaded in the Watir browser. But after it is loaded, I get the Timeout::Error in my main browser window.
Here's my code:
@pages = Pages.all
browser = Watir::Browser.new
@pages.each do |page|
page_url = app_root_url + 'pages/' + page.id.to_s
browser.goto page_url
Watir::Waiter::wait_until { browser.div(:id, 'content').exists? }
file_save_path = pages_screenshot_path.to_s + page.id.to_s + '.png'
browser.driver.save_screenshot(file_save_path)
end
browser.close
Each page contains a div with id 'content'. Still, it's not waiting I guess.
回答1:
The default wait time for Watir::Waiter.wait_until is 60 seconds (checking every half second until 60). You can specify a higher value like so:
Watir::Waiter.wait_until(120) { code code code }
You can find more specifics here: http://wiki.openqa.org/display/WTR/How+to+wait+with+Watir
For watir-webdriver, you can use Watir::Wait.methods:
Watir::Wait.until(120) { code code code }
回答2:
I moved this process to run in the background using delayed_job gem, and it works fine!
来源:https://stackoverflow.com/questions/8401885/timeouterror-in-rails-application-using-watir