Search code examples
ruby-on-railsrspeccapybararspec-rails

Capybara Finding Button but Not Submitting Form


I'm writing a feature test on a user signup form and using Capybara with rspec to test it. Capybara is successfully finding the button, but the test results come back and show that the text it's finding on the page is from the page the form is on, not the page that loads after. I can't seem to find out why the form isn't submitting via the test, I've tried several ways of clicking on the button and each time it finds the button but it doesn't trigger the form to be sent.

Capybara Test Failure Error

Failures:

  1) submitting a new user creates a new user
     Failure/Error: expect(page).to have_content("Traid with harold_houdini!")
       expected to find text "Traid with harold_houdini!" in "Sign In Sign in with Google Sign In Sign in with Google First name: Last name: Birthday: Gender: Female Male Username: You can change your username whenever you'd like. Email: Password: Password confirmation: Address: Secondary address: City: State: Zip code: Country: What do you have to offer? What are you looking for?"
     # /usr/local/rvm/gems/ruby-2.3.4/gems/given_core-3.8.0/lib/given/rspec/monkey.rb:21:in `handle_matcher'
     # ./spec/features/users/new_spec.rb:16:in `block (2 levels) in <top (required)>'

Finished in 1.03 seconds (files took 3.33 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/features/users/new_spec.rb:5 # submitting a new user creates a new user

Capybara Test

require 'capybara/rspec'
require 'rails_helper'

describe "submitting a new user" do
    it "creates a new user" do
        visit '/users/new'
        within ("#new_user") do
            fill_in "user[first_name]", with: "Harold"
            fill_in "user[last_name]", with: "Houdini"
            fill_in "user[username]", with: "harold_houdini"
            fill_in "user[email]", with: "harold_houdini@gmail.com"
            fill_in "user[password]", with: "password"
            fill_in "user[password_confirmation]", with: "password_confirmation"
        end
        page.find("#new-user-form").click
        expect(page).to have_content("Traid with harold_houdini!")
    end
end

Rails Helper

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)

abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.use_transactional_fixtures = true
  # config.include Capybara::DSL
  # Capybara.default_max_wait_time = 5


  config.infer_spec_type_from_file_location!

  config.filter_rails_from_backtrace!

end

User Form Partial (Rendered)

<form class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="8CLe05BTkyXtB0f7HKRjqfcRhFNSdbrxg7DQeBLvxCNzLeCpPWyeK82V4VnIUjJfSvgmjYGiTCYLGtriDZV9kA==">

   ...........

    <div>
      <input type="submit" name="commit" value="submit" id="new-user-form">
    </div>

</form>

Solution

  • @hashrocket was able to catch the error, I simply did not have my correct password/password combination combo. After updating, I changed my button clicker to click_button("new-user-form") which located the button by id and successfully submitted.