Last active
December 13, 2015 21:08
-
-
Save kylev/4975478 to your computer and use it in GitHub Desktop.
Wait for jQuery to load before running Capybara tests. http://www.kylev.com/2013/02/17/capybara-synchronize-and-monkeys-that-climb-ivory-towers/
This file contains hidden or 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
module Capybara | |
class Session | |
# `execute_script` variant that first waits for jQuery's $ to be defined. | |
def execute_jquery(script) | |
synchronize_javascript("$ !== 'undefined'") | |
execute_script(script) | |
end | |
# `evaluate_script` variant that first waits for jQuery's $ to be defined. | |
def evaluate_jquery(script) | |
synchronize_javascript("$ !== 'undefined'") | |
evaluate_script(script) | |
end | |
# Use Capybara's waiting mechanism until a JavaScript expression evaluates to true. | |
def synchronize_javascript(script) | |
current_node.synchronize do | |
raise ExpectationNotMet.new("Script wasn't true") unless evaluate_script(script) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Checkout the
patiently do ... end
block helper from Spreewald. It does something similar, but in a more generic form. Because once you're over this particular problem, you will have fun with asynchronous AJAX calls etc.