Last active
May 7, 2020 11:30
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
I found this very helpful. Thanks!
Rather than break open Capybara::Session
, I included the method in all feature specs like so:
module FormHelpers
def submit_form(form)
Capybara::RackTest::Form.new(page.driver, form.native).submit({})
end
end
RSpec.configure do |config|
config.include FormHelpers, type: :feature
end
Then to use it:
form = find("form[name=my_form]")
submit_form(form)
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
Did this work? I'm trying something similar, and it doesn't seem to work for me. The search query isn't returning the right result.