Created
August 11, 2011 00:07
-
-
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.
This file contains 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
# 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