Created
August 12, 2011 22:25
-
-
Save beccasaurus/1143135 to your computer and use it in GitHub Desktop.
Capybara helper for defining a JavaScript function in a page (globally, attached to window)
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
# Given the name and body for a JavaScript function, this defines the | |
# function globally in the page. | |
# | |
# This is most useful for making helper JavaScript functions that you can | |
# call later, eg. foo = evaluate_script('myFunction()') | |
# | |
# Usage: | |
# | |
# page.define_function :whatever, %{ | |
# var foo = 'hello'; | |
# return foo; | |
# } | |
# | |
# result = page.evaluate_script('whatever()') | |
# | |
class Capybara::Session | |
def define_function name, body | |
execute_script %{ | |
window[#{name.to_s.inspect}] = function() { | |
#{body} | |
}; | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment