Skip to content

Instantly share code, notes, and snippets.

@beccasaurus
Created August 12, 2011 22:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beccasaurus/1143135 to your computer and use it in GitHub Desktop.
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)
# 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