水豚不会填写表单字段(尽管找到了它们) [英] Capybara won't fill in form fields (although it finds them)

查看:56
本文介绍了水豚不会填写表单字段(尽管找到了它们)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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: "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模板:

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>

生成的格式:

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


推荐答案

我已经设法解决了。由Strong Params和Devise引起。我将Devise设置为使用名称和用户名参数,即使在控制器中明确清除它们也未得到授权。这是我所做的:

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. 生成的设备控制器:

  1. Generated Devise Controllers:

$ rails g devise:控制器

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

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

#您可以将要允许的参数放入空数组。

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

  1. Changed the routes to go first through this controller instead of the default Devise one in routes.rb:

devise_for:users,:controllers => ; {:registrations => 用户/注册}

未注释registrations_controller.rb中的第一行:

Uncommented the first line in registrations_controller.rb:

before_filter:configure_sign_up_params,仅:[:create]

这不是水豚问题,而是具有强参数的设计问题。现在它可以正常工作了。

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

这篇关于水豚不会填写表单字段(尽管找到了它们)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆