Skip to content

Instantly share code, notes, and snippets.

@beccasaurus
Created May 24, 2011 19:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beccasaurus/989533 to your computer and use it in GitHub Desktop.
Save beccasaurus/989533 to your computer and use it in GitHub Desktop.
Capybara RackTest extension for submitting a form without a button
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!
@alexdesi
Copy link

alexdesi commented Jan 6, 2012

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?

@beccasaurus
Copy link
Author

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

@minimul
Copy link

minimul commented Apr 10, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment