Capybara不会填写表格字段(虽然它找到了它们)

时间:2015-04-26 22:06:03

标签: ruby-on-rails ruby rspec capybara

我正在使用Ruby 2.2.1和Capybara 2.2.4开发Rails 4.2.1。写一个规范来注册用户,Capybara确实找到了这些字段(我没有得到ElementNotFound错误),但是当我尝试验证时,我发现所有字段都是空的并且在打开launchy之前告诉Capybara点击注册按钮显示所有字段为空。

规格:

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: "john@example.com"
    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错误:

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

编辑:根据要求,表格erb模板:

  <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>

生成的表格:

<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 个答案:

答案 0 :(得分:3)

我设法解决了这个问题。它是由强大的参数和设计引起的。我已将Devise设置为使用名称和用户名参数,即使在控制器中明确清除,它们也未被授权。这是我做的:

  1. 生成的设计控制器:

    $ rails g devise:controllers

  2. 将新的参数添加到controllers / user / registrations_controller.rb中的默认参数:

  3. #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. 将路由更改为首先通过此控制器,而不是路由中的默认设计.rb:

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

    2. 取消注释registrations_controller.rb中的第一行:

      before_filter :configure_sign_up_params, only: [:create]

    3. 这不是一个水豚问题,而是一个具有强大参数问题的设计。现在它已经完成了工作。