在终端中我的Capybara测试的以下错误:
Failures:
1) Users User create new user
Failure/Error: fill_in 'Username', with: 'Ali', visible: false
Capybara::ElementNotFound:
Unable to find field "Username" that is not disabled within #<Capybara::Node::Element tag="form" path="/html/body/form">
# ./spec/features/users_spec.rb:31:in `block (4 levels) in <top (required)>'
# ./spec/features/users_spec.rb:30:in `block (3 levels) in <top (required)>
我在辣椒症中的测试代码:
require 'rails_helper'
RSpec.feature "Users", type: :feature do
describe "User", :type => :feature do
it "create new user" do
visit '/signup'
within('form') do
fill_in 'Username', with: 'Ali', visible: false
fill_in 'Password', with: 'ali', visible: false
end
click_button 'Submit'
expect(page).to have_content 'User successfully created.'
end
end
end
我的视图文件
<h1>Create New User</h1>
<%= form_for :user, url: '/users' do |f| %>
Username: <%= f.text_field :username %>
Password: <%= f.password_field :password %>
Uplaod your photo: <%= f.file_field :image %>
<%= f.submit "Submit" %>
<% end %>
和我的控制器:
def create
user = User.new(user_params)
if user.save
redirect_to '/users/index', notice: 'User successfully created.'
else
redirect_to '/signup'
end
end
我做了一些研究,这是由于Capybara 2X上的问题,大多数人通过添加可见的方法解决了:False,但该解决方案对我无效。
感谢Pro的帮助。
您无法填写不可访问的字段,因此将visible: false
传递给fill_in
是没有道理的。
fill_in
找不到字段的原因是因为它通过ID,名称或关联的标签文本找到字段。您不会显示页面的实际HTML,但是"用户名"one_answers"密码"实际上并不是在标签元素中,这意味着您无法通过关联的标签文本找到,因此无法正常工作。您可以将文本放入<label>
元素中,并与各个字段(for
属性或包装(相关联,或者可以选择填充ID或名称填充的字段。没有实际的html,就无法确定,但是类似
fill_in 'user_username', with: 'Ali'
fill_in 'user_password', with: 'ali'
可能会起作用,在字段ID上匹配,
或
fill_in 'user[username]', with: 'Ali'
fill_in 'user[password]', with: 'ali'
在字段名称属性上匹配。
我以这种方式解决了同样的问题:在我的一些JS代码中,是``而不是''。浏览器可以理解该语法(``插值需要(,但Capybara却做不到。
当我更改为``''时,问题就消失了。