Skip to content

Instantly share code, notes, and snippets.

@KevinTriplett
Last active December 14, 2015 12:38
Show Gist options
  • Save KevinTriplett/5087744 to your computer and use it in GitHub Desktop.
Save KevinTriplett/5087744 to your computer and use it in GitHub Desktop.
Adding back the #wait_until to Capybara v2+ gem. Can probably be adapted for cucumber tests.
# add this file capybara_wait_until.rb to your /test directory
module Capybara
class Session
##
#
# Retry executing the block until a truthy result is returned or the timeout time is exceeded
#
# @param [Integer] timeout The amount of seconds to retry executing the given block
#
# this method was removed in Capybara v2 so adding it back if not already defined
#
unless defined?(wait_until)
def wait_until(timeout = Capybara.default_wait_time)
Capybara.send(:timeout, timeout, driver) { yield }
end
end
end
end
# Use it this way in /test/integration_test_helper.rb:
require 'capybara_wait_until'
module ActionController
class IntegrationTest
def wait_for_ajax_to_complete
# pass timeout in seconds if you need to override default_wait_time
page.wait_until { page.evaluate_script('jQuery.active === 0') }
end
end
end
@KevinTriplett
Copy link
Author

I really, really love Capybara and I also love Ruby for the ability to veto gem authors' choices. ;)

@Hengjie
Copy link

Hengjie commented Mar 6, 2013

I agree, I need something like this as well for a very special case of having to wait on Resque workers to finish processing before it updates the page via a push notification. Capybara just doesn't handle this 'special' case at all.

@mcfiredrill
Copy link

@josepjaume
Copy link

When you use find, capybara will be actually waiting for the element to appear (by using synchronize behind the scenes), so there's little reason to use wait_for here.

Moreover, this couples your acceptance tests to your underlying implementation and will be a pain to deal with when you decide to use websockets there, or just remove ajax calls for some reason.

@mhuggins
Copy link

mhuggins commented Jul 9, 2013

@josepjaume - can you give a code example on using synchronize? I get an error that the method doesn't exist.

@kyleheddon
Copy link

When I try to call wait_until, I get:
class or module required for rescue clause

@kyleheddon
Copy link

to be more specific:

Failure/Error: page.wait_until {
     TypeError:
       class or module required for rescue clause
     # ./spec/support/capybara/wait_until.rb:13:in `wait_until'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment