Capybara :: ElementNotFound:无法找到可见字段 [英] Capybara::ElementNotFound: Unable to find visible field
问题描述
我在终端的Capybara测试中收到以下错误:
Received following error for my Capybara test in terminal:
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)>
我在水豚中的测试代码:
My test code in capybara:
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
我进行了一些研究,这是由于水豚2x上的问题所致,大多数人通过添加visible来解决此问题:错误,但该解决方案对我无效。
I did some research, this was due to the issue on capybara 2x, and most peoples solved by adding visible: false, but this solution couldn't works on me.
感谢专业人士的帮助。
推荐答案
您不能填写不可见的字段,因此,将 visible:false
传递给 fill_in
没有任何意义。
You can't fill in fields that are non-visible, so passing visible: false
to fill_in
makes no sense.
fill_in
找不到您的字段的原因是因为它按ID,名称或相关标签文本查找字段。您没有显示页面的实际HTML,但是标签元素中实际上没有显示用户名和密码,这意味着您无法通过关联的标签文本进行查找,因此无法使用。您可以将文本放入< label>
元素中,并与相应的字段关联( for
属性或自动换行),或者您可以选择要通过ID或名称填写的字段。如果没有实际的HTML,就无法确定,但是类似
The reason fill_in
isn't finding your fields is because it finds fields by id, name, or associated label text. You don't show the actual HTML of your page but the fact that 'Username' and 'Password' aren't actually in label elements would mean that you can't find by associated label text so that won't work. You could put the text into <label>
elements and associate with the respective fields (for
attribute or wrapping), or you could select the fields to fill_in by id or name. Without the actual HTML it's impossible to say for sure, but something like
fill_in 'user_username', with: 'Ali'
fill_in 'user_password', with: 'ali'
可能会起作用,与字段ID匹配,
will probably work, matching on the fields ids,
或
fill_in 'user[username]', with: 'Ali'
fill_in 'user[password]', with: 'ali'
匹配字段名称属性。
这篇关于Capybara :: ElementNotFound:无法找到可见字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!