Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Capybara RackTest extension for submitting a form without a button
module SubmitRackTestFormWithoutButton
def submit_form!, form).submit({})
Capybara::RackTest::Node.send :include, SubmitRackTestFormWithoutButton
# in your code ..

This comment has been minimized.

Copy link

alexdesi commented Jan 6, 2012

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?


This comment has been minimized.

Copy link
Owner Author

beccasaurus commented Jan 6, 2012

Hmm. I haven't tried running any code yet, but I remember that I copy/pasted the, 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:, 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)

# and here's the code for brower.find
def find(selector)
  dom.xpath(selector).map { |node|, node) } # <--- notice the

# 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"), form).submit(self)

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).


This comment has been minimized.

Copy link

minimul commented Apr 10, 2013

@alexdesi Yes, Capybara::Node::Element needs to be extended also see

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.