public
Last active

Capybara RackTest extension for submitting a form without a button

  • Download Gist
README.markdown
Markdown
SubmitRackTestFormWithoutButton.rb
Ruby
1 2 3 4 5 6 7 8 9 10
module SubmitRackTestFormWithoutButton
def submit_form!
Capybara::RackTest::Form.new(driver, form).submit({})
end
end
 
Capybara::RackTest::Node.send :include, SubmitRackTestFormWithoutButton
 
# in your code ..
find('input-or-whatever').submit_form!

Hi,
I tried to used this code but without success:
I get undefined method `submit_form!' for #Capybara::Node::Element:...
actually by "Capybara::RackTest::Node.send :include, SubmitRackTestFormWithoutButton" the method submit_form! is added to the Node (not to the Element), but find return an Element
Some ideas how to fix it?

Hmm. I haven't tried running any code yet, but I remember that I copy/pasted the Capybara::RackTest::Form.new(driver, form).submit({}) code from on of Capybara's #click methods. I remember that, behind the scenes, Capybara obviously knew how to submit a form, but it ONLY exposed this via a button click.

Anyway ... I looked at the current version of node.rb and the Form submit code has been slightly tweaked and now looks like: Capybara::RackTest::Form.new(driver, form).submit(self)

That said, if you're getting an undefined method submit_form! for Element ... that's a different problem. You should get a Capybara::RackTest::Node as the result of calling #find(...). Are you using RackTest (as opposed to Selenium, Webkit, etc)?

# this is the RackTest driver's find method
def find(selector)
  browser.find(selector)
end

# and here's the code for brower.find
def find(selector)
  dom.xpath(selector).map { |node| Capybara::RackTest::Node.new(self, node) } # <--- notice the Node.new
end

# and here's the #click method of a RackTest::Node which determines whether you're clicking on an Anchor (which will visit the href) or a form submit/input button (which will submit the form, which is what you wanna do)
def click
  if tag_name == 'a'
    method = self["data-method"] if driver.options[:respect_data_method]
    method ||= :get
    driver.follow(method, self[:href].to_s)
  elsif (tag_name == 'input' and %w(submit image).include?(type)) or
      ((tag_name == 'button') and type.nil? or type == "submit")
    Capybara::RackTest::Form.new(driver, form).submit(self)
  end
end

Hope that helps? The only problem that I can think of is that you're not using the :rack_test driver? Lemme know if I can help any more (but I gotta run for now).

@alexdesi Yes, Capybara::Node::Element needs to be extended also see http://minimul.com/submitting-a-form-without-a-button-using-capybara.html

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.