Skip to content

Instantly share code, notes, and snippets.

@beccasaurus
Created August 11, 2011 00:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save beccasaurus/1138616 to your computer and use it in GitHub Desktop.
Save beccasaurus/1138616 to your computer and use it in GitHub Desktop.
Capybara helper for calling evaluate_script with multiple lines of JavaScript, supporting short-circuit return statements, etc etc. Just wraps your JS in a function and then calls it.
# Given a multiline script, this will wrap the script in a function and then
# call it so Capybara's evaluate_script knows how to get the return value properly.
# By default, Capybara just prepends a 'return ' to your script.
#
# Usage:
#
# value = page.evaluate_multiline_script %{
# var foo = [];
# $('li').each(function(i, li) {
# foo[foo.length] = $(li).text();
# });
# return foo;
# }
#
class Capybara::Session
def evaluate_multiline_script javascript
evaluate_script "(function(){ #{javascript} })()"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment