2
votes

I'm developing a Rails 4.2.1 using Ruby 2.2.1 and Capybara 2.2.4. Writing a spec to sign up users, Capybara does find the fields (I'm not getting the ElementNotFound Error) but when I try to validate I get a failure that all fields were empty and also opening launchy before telling Capybara to click the sign up button shows all fields empty.

Spec:

require 'rails_helper'

RSpec.feature "Sign Up Users", type: :feature do
  scenario "saves user to the database with valid data" do
    visit root_path
    click_link "Sign Up"

    find("#user_name").set("John Doe")
    fill_in "user_username", with: "johndoe"
    fill_in "user_email", with: "[email protected]"
    fill_in "user_password", with: "helloworld"
    fill_in "user_password_confirmation", with: "helloworld"

    save_and_open_page

    click_button "Create Account"

    expect(page).to have_text('Account successfully created! Welcome!')
    expect(page).to have_link('Log Out')
    expect(page).not_to have_link('Sign In')
    expect(page).not_to have_link('Sign Up')
  end

  scenario "does not save user with invalid data and re-renders sign up page" do
    visit root_path
    click_link "Sign In"

    click_link "Sign In"
    expect(page).to render(:signin)
  end
end

RSpec error:

Failures:

  1) Sign Up Users saves user to the database with valid data
     Failure/Error: expect(page).to have_text('Account successfully created! Welcome!')
       expected to find text "Account successfully created! Welcome!" in "Menu Sign In Sign Up TUDO | PRESENTES | FESTA | NOIVO | NOIVA Sign Up Please review the problems below: * Namecan't be blank * Usernamecan't be blank * Email * Password * Password confirmation Log in"
     # ./spec/features/sign_up_users_spec.rb:18:in `block (2 levels) in <top (required)>'

Finished in 0.97155 seconds (files took 2.09 seconds to load)
10 examples, 2 failures, 2 pending

EDIT: As requested, the form erb template:

  <div class="form-inputs">
    <%= f.input :name, required: true, autofocus: true %>
    <%= f.input :username, required: true %>
    <%= f.input :email, required: true %>
    <%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @validatable) %>
    <%= f.input :password_confirmation, required: true %>
  </div>

And the generated form:

<fieldset>
      <legend>Sign Up</legend>
      <form novalidate="novalidate" class="simple_form 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="U+FOvfLux/Oi9jlTO1lfX7CmuOlRSZamCym3EAAJUW/HA78aVtkeqZgr0EToLIegaa+RH9LHHXIfmOlR5MCXBw==">


      <div class="form-inputs">
        <div class="input string required user_name"><label class="string required" for="user_name"><abbr title="required">*</abbr> Name</label><input class="string required" autofocus="autofocus" type="text" name="user[name]" id="user_name" style="cursor: auto; background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABHklEQVQ4EaVTO26DQBD1ohQWaS2lg9JybZ+AK7hNwx2oIoVf4UPQ0Lj1FdKktevIpel8AKNUkDcWMxpgSaIEaTVv3sx7uztiTdu2s/98DywOw3Dued4Who/M2aIx5lZV1aEsy0+qiwHELyi+Ytl0PQ69SxAxkWIA4RMRTdNsKE59juMcuZd6xIAFeZ6fGCdJ8kY4y7KAuTRNGd7jyEBXsdOPE3a0QGPsniOnnYMO67LgSQN9T41F2QGrQRRFCwyzoIF2qyBuKKbcOgPXdVeY9rMWgNsjf9ccYesJhk3f5dYT1HX9gR0LLQR30TnjkUEcx2uIuS4RnI+aj6sJR0AM8AaumPaM/rRehyWhXqbFAA9kh3/8/NvHxAYGAsZ/il8IalkCLBfNVAAAAABJRU5ErkJggg==); background-attachment: scroll; background-position: 100% 50%; background-repeat: no-repeat;"></div>
        <div class="input string required user_username"><label class="string required" for="user_username"><abbr title="required">*</abbr> Username</label><input class="string required" type="text" name="user[username]" id="user_username"></div>
        <div class="input email required user_email"><label class="email required" for="user_email"><abbr title="required">*</abbr> Email</label><input class="string email required" type="email" value="" name="user[email]" id="user_email"></div>
        <div class="input password required user_password field_with_hint"><label class="password required" for="user_password"><abbr title="required">*</abbr> Password</label><input class="password required" type="password" name="user[password]" id="user_password" style="background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACIUlEQVQ4EX2TOYhTURSG87IMihDsjGghBhFBmHFDHLWwSqcikk4RRKJgk0KL7C8bMpWpZtIqNkEUl1ZCgs0wOo0SxiLMDApWlgOPrH7/5b2QkYwX7jvn/uc//zl3edZ4PPbNGvF4fC4ajR5VrNvt/mo0Gr1ZPOtfgWw2e9Lv9+chX7cs64CS4Oxg3o9GI7tUKv0Q5o1dAiTfCgQCLwnOkfQOu+oSLyJ2A783HA7vIPLGxX0TgVwud4HKn0nc7Pf7N6vV6oZHkkX8FPG3uMfgXC0Wi2vCg/poUKGGcagQI3k7k8mcp5slcGswGDwpl8tfwGJg3xB6Dvey8vz6oH4C3iXcFYjbwiDeo1KafafkC3NjK7iL5ESFGQEUF7Sg+ifZdDp9GnMF/KGmfBdT2HCwZ7TwtrBPC7rQaav6Iv48rqZwg+F+p8hOMBj0IbxfMdMBrW5pAVGV/ztINByENkU0t5BIJEKRSOQ3Aj+Z57iFs1R5NK3EQS6HQqF1zmQdzpFWq3W42WwOTAf1er1PF2USFlC+qxMvFAr3HcexWX+QX6lUvsKpkTyPSEXJkw6MQ4S38Ljdbi8rmM/nY+CvgNcQqdH6U/xrYK9t244jZv6ByUOSiDdIfgBZ12U6dHEHu9TpdIr8F0OP692CtzaW/a6y3y0Wx5kbFHvGuXzkgf0xhKnPzA4UTyaTB8Ph8AvcHi3fnsrZ7Wore02YViqVOrRXXPhfqP8j6MYlawoAAAAASUVORK5CYII=); background-attachment: scroll; background-position: 100% 50%; background-repeat: no-repeat;"></div>
        <div class="input password required user_password_confirmation"><label class="password required" for="user_password_confirmation"><abbr title="required">*</abbr> Password confirmation</label><input class="password required" type="password" name="user[password_confirmation]" id="user_password_confirmation" style="cursor: auto; background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACIUlEQVQ4EX2TOYhTURSG87IMihDsjGghBhFBmHFDHLWwSqcikk4RRKJgk0KL7C8bMpWpZtIqNkEUl1ZCgs0wOo0SxiLMDApWlgOPrH7/5b2QkYwX7jvn/uc//zl3edZ4PPbNGvF4fC4ajR5VrNvt/mo0Gr1ZPOtfgWw2e9Lv9+chX7cs64CS4Oxg3o9GI7tUKv0Q5o1dAiTfCgQCLwnOkfQOu+oSLyJ2A783HA7vIPLGxX0TgVwud4HKn0nc7Pf7N6vV6oZHkkX8FPG3uMfgXC0Wi2vCg/poUKGGcagQI3k7k8mcp5slcGswGDwpl8tfwGJg3xB6Dvey8vz6oH4C3iXcFYjbwiDeo1KafafkC3NjK7iL5ESFGQEUF7Sg+ifZdDp9GnMF/KGmfBdT2HCwZ7TwtrBPC7rQaav6Iv48rqZwg+F+p8hOMBj0IbxfMdMBrW5pAVGV/ztINByENkU0t5BIJEKRSOQ3Aj+Z57iFs1R5NK3EQS6HQqF1zmQdzpFWq3W42WwOTAf1er1PF2USFlC+qxMvFAr3HcexWX+QX6lUvsKpkTyPSEXJkw6MQ4S38Ljdbi8rmM/nY+CvgNcQqdH6U/xrYK9t244jZv6ByUOSiDdIfgBZ12U6dHEHu9TpdIr8F0OP692CtzaW/a6y3y0Wx5kbFHvGuXzkgf0xhKnPzA4UTyaTB8Ph8AvcHi3fnsrZ7Wore02YViqVOrRXXPhfqP8j6MYlawoAAAAASUVORK5CYII=); background-attachment: scroll; background-position: 100% 50%; background-repeat: no-repeat;"></div>
      </div>

      <div class="form-actions">
      <input type="submit" name="commit" value="Create Account" class="button button expand">
      </div>
</form>        <a href="/users/sign_in">Log in</a><br>






    </fieldset>
1
Hi and welcome to Stack Overflow. Can you tell me (edit your question and add it there): how do you know that i's finding the username? is it possible that it's finding more than one (a common failure-mode for filling in fields)? Is there anything else you can show us that will help us to help you? (remember, don't reply in the comments (formatting is awful) - edit your question and make it part of the question. I'm going to go ahead and remove your gemfile - as it isn't likely to be the issue and is making this much more complicated looking than it needs to be. - Taryn East
eg - the relevant html of the form (including the actual field ids) would be useful, I think (though please not the whole page). Possibly even the erb template for the form. - Taryn East
Thanks for the welcome and the help. I think it is finding the fields because earlier I got the "ElementNotFound" error. Then I created the fields and pointed to the ids. Each time it would not find the next element. Ex. It wouldn't find name, I created name, it wouldn't find email, etc. That's why I think it is seeing the fields, it's just not filling them up... - Hugo Carlos

1 Answers

3
votes

I've managed to fix it. It was caused by Strong Params and Devise. I've set Devise to work with name and username parameters and they were not being authorized, even when explicitly cleared in controller. Here is what I did:

  1. Generated Devise Controllers:

    $ rails g devise:controllers

  2. Added the new params to the default ones in controllers/user/registrations_controller.rb:

#You can put the params you want to permit in the empty array.

def configure_account_update_params
  puts "configure_account_update_params"
  devise_parameter_sanitizer.for(:account_update) << :name
  devise_parameter_sanitizer.for(:account_update) << :username
end
  1. Changed the routes to go first through this controller instead of the default Devise one in routes.rb:

    devise_for :users, :controllers => { :registrations => "user/registrations" }

  2. Uncommented the first line in registrations_controller.rb:

    before_filter :configure_sign_up_params, only: [:create]

It was not a Capybara issue, but a Devise with Strong Params problem. Now it's working perfeclty.