Skip to content

Instantly share code, notes, and snippets.

@marcoslebron
Last active May 7, 2020 11:30
Show Gist options
  • Save marcoslebron/ca96949c7ea9bd45d82ea4afe0e4141e to your computer and use it in GitHub Desktop.
Save marcoslebron/ca96949c7ea9bd45d82ea4afe0e4141e to your computer and use it in GitHub Desktop.
I was trying to submit a form without button using just Capybara and Rspec
class Capybara::Session
def submit(element)
Capybara::RackTest::Form.new(driver, element.native).submit({})
end
end
#in the test
fill_in 'Search', with: 'dice'
form = find '#search-form' # find the form
page.submit form # use the new .submit method, pass form as the argument
@vivipoit
Copy link

This is awesome! I looked at both suggestions. Thanks!

I already have

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

in my spec/rails_helper.rb for other reasons, so I created a new file spec/support/submit_form.rb:

def submit_form(form_selector)
  form = find("form#{form_selector}")
  Capybara::RackTest::Form.new(page.driver, form.native).submit({})
end

Now my tests allow me to call things like:

submit_form '#my_form_id'
submit_form '.my_form_class'

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