Skip to content

Instantly share code, notes, and snippets.

@dnagir
Created February 1, 2012 08:00
Show Gist options
  • Save dnagir/1715880 to your computer and use it in GitHub Desktop.
Save dnagir/1715880 to your computer and use it in GitHub Desktop.
Fail fast with Capybara
require 'capybara/util/timeout'
# Rewriting the https://github.com/jnicklas/capybara/blob/master/lib/capybara/util/timeout.rb
# Instead of raising timeout error, print the response info
module Capybara
class << self
##
# Provides timeout similar to standard library Timeout, but avoids threads
#
def timeout(seconds = 1, driver = nil, error_message = nil, &block)
start_time = Time.now
result = nil
until result
return result if result = yield
delay = seconds - (Time.now - start_time)
if delay <= 0
raise TimeoutError, error_message || fail_fast_timeout_message!(driver)
end
driver && driver.wait_until(delay)
sleep(0.05)
end
end
def fail_fast_timeout_message!(driver)
"timed out. status=#{driver.status_code}, body:\n#{driver.body}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment