module Cucumber::Rails::Capybara::JavascriptEmulation

Public Class Methods

included(base) click to toggle source
# File lib/cucumber/rails/capybara/javascript_emulation.rb, line 7
def self.included(base)
  base.class_eval do
    alias_method :click_without_javascript_emulation, :click
    alias_method :click, :click_with_javascript_emulation
  end
end

Public Instance Methods

click_with_javascript_emulation(*) click to toggle source
# File lib/cucumber/rails/capybara/javascript_emulation.rb, line 14
def click_with_javascript_emulation(*)
  if link_with_non_get_http_method?
    ::Capybara::RackTest::Form.new(
      driver, js_form(element_node.document, self[:href], emulated_method)
    ).submit(self)
  else
    click_without_javascript_emulation
  end
end

Private Instance Methods

add_hidden_csrf_input(document, js_form) click to toggle source
# File lib/cucumber/rails/capybara/javascript_emulation.rb, line 75
def add_hidden_csrf_input(document, js_form)
  input = document.create_element('input')
  input['type'] = 'hidden'
  input['name'] = csrf_param
  input['value'] = csrf_token
  js_form.add_child(input)
end
add_hidden_method_input(document, js_form) click to toggle source
# File lib/cucumber/rails/capybara/javascript_emulation.rb, line 63
def add_hidden_method_input(document, js_form)
  input = document.create_element('input')
  input['type'] = 'hidden'
  input['name'] = '_method'
  input['value'] = emulated_method
  js_form.add_child(input)
end
csrf?() click to toggle source
# File lib/cucumber/rails/capybara/javascript_emulation.rb, line 26
def csrf?
  csrf_param_node && csrf_token_node
end
csrf_param() click to toggle source
# File lib/cucumber/rails/capybara/javascript_emulation.rb, line 34
def csrf_param
  csrf_param_node['content']
end
csrf_param_node() click to toggle source
# File lib/cucumber/rails/capybara/javascript_emulation.rb, line 30
def csrf_param_node
  element_node.document.at_xpath("//meta[@name='csrf-param']")
end
csrf_token() click to toggle source
# File lib/cucumber/rails/capybara/javascript_emulation.rb, line 42
def csrf_token
  csrf_token_node['content']
end
csrf_token_node() click to toggle source
# File lib/cucumber/rails/capybara/javascript_emulation.rb, line 38
def csrf_token_node
  element_node.document.at_xpath("//meta[@name='csrf-token']")
end
element_node() click to toggle source
# File lib/cucumber/rails/capybara/javascript_emulation.rb, line 93
def element_node
  native
end
emulated_method() click to toggle source
# File lib/cucumber/rails/capybara/javascript_emulation.rb, line 89
def emulated_method
  element_node['data-method']
end
get?(emulated_method) click to toggle source
# File lib/cucumber/rails/capybara/javascript_emulation.rb, line 71
def get?(emulated_method)
  same?(emulated_method, 'get')
end
js_form(document, action, emulated_method, method = 'POST') click to toggle source
# File lib/cucumber/rails/capybara/javascript_emulation.rb, line 46
def js_form(document, action, emulated_method, method = 'POST')
  js_form = document.create_element('form')
  js_form['action'] = action
  js_form['method'] = method

  add_hidden_method_input(document, js_form) unless same?(emulated_method, method)

  # rails will wipe the session if the CSRF token is not sent with non-GET requests
  add_hidden_csrf_input(document, js_form) if csrf? && !get?(emulated_method)

  js_form
end
same?(emulated_method, method) click to toggle source
# File lib/cucumber/rails/capybara/javascript_emulation.rb, line 59
def same?(emulated_method, method)
  emulated_method.casecmp(method).zero?
end