Search code examples
ruby-on-railstestingrspecbddcapybara

Capybara - Submit a form without button


I am trying to submit a form without button using just Capybara and Rspec (no Cucumber or Selenium, I know there is already a question about that).

I've seen there is a gist to add a method to submit a form without button:

module SubmitRackTestFormWithoutButton
  def submit_form!
    Capybara::RackTest::Form.new(driver, form).submit({})
  end
end
Capybara::RackTest::Node.send :include, SubmitRackTestFormWithoutButton

https://gist.github.com/989533, but I've not gotten it to work and I left a comment on it:

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

Do you have some idea to work out that gist, or some other solution to submit a form without button ?

Thanks


Solution

  • Although it's possible to achieve what you want using capybara, the easier and more practical solution is to put a submit button on the form.

    There is no reason to not have a button on the form, it's bad accessibility to not have a form and users that do not have a GUI or are using screen readers will have trouble submitting the form otherwise.

    If you don't want the form button to be visible, can I suggest using some CSS to make it hidden:

    <input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;">