Guest User

Untitled

a guest
Apr 25th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. module RailsJavascriptEmulation
  2. def self.included(base)
  3. base.class_eval do
  4. alias_method :click_without_rails_javascript_emulation, :click
  5. end
  6. end
  7.  
  8. def click_with_rails_javascript_emulation
  9. if tag_name == 'a' and node['onclick'] =~ /m\.setAttribute\('name', '_method'\)/
  10. method = node['onclick'].match(/m\.setAttribute\('value', '([^']*)'\)/)[1]
  11.  
  12. js_form = node.document.create_element('form')
  13. js_form['action'] = self[:href]
  14. js_form['method'] = 'POST'
  15.  
  16. input = node.document.create_element('input')
  17. input['type'] = 'hidden'
  18. input['name'] = '_method'
  19. input['value'] = method
  20. js_form.add_child(input)
  21.  
  22. Capybara::Driver::RackTest::Form.new(driver, js_form).submit(self)
  23. else
  24. click_without_rails_javascript_emulation
  25. end
  26. end
  27. end
  28.  
  29. class Capybara::Driver::RackTest::Node
  30. include RailsJavascriptEmulation
  31. end
  32.  
  33. Before('@emulate_rails_javascript') do
  34. Capybara::Driver::RackTest::Node.class_eval do
  35. alias_method :click, :click_with_rails_javascript_emulation
  36. end
  37. end
  38.  
  39. After('@emulate_rails_javascript') do
  40. Capybara::Driver::RackTest::Node.class_eval do
  41. alias_method :click, :click_without_rails_javascript_emulation
  42. end
  43. end
Add Comment
Please, Sign In to add comment