Skip to content

Instantly share code, notes, and snippets.

@padi
Forked from beccasaurus/evaluate_multiline_script.rb
Created September 11, 2017 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save padi/0b92da2d0e71cb23217805c28c63702f to your computer and use it in GitHub Desktop.
Save padi/0b92da2d0e71cb23217805c28c63702f 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