Skip to content

Instantly share code, notes, and snippets.

@kylev
Last active December 13, 2015 21:08
Show Gist options
  • Save kylev/4975478 to your computer and use it in GitHub Desktop.
Save kylev/4975478 to your computer and use it in GitHub Desktop.
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
@triskweline
Copy link

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.

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