gpt4 book ai didi

ruby-on-rails - Capybara::ElementNotFound:无法找到可见字段

转载 作者:行者123 更新时间:2023-12-03 20:46:33 25 4
gpt4 key购买 nike

在终端中进行 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)>

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

我的 View 文件
<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 %>

还有我的 Controller :
  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 来解决,但是这个解决方案对我不起作用。

感谢专业人士的帮助。

最佳答案

你不能填写不可见的字段,所以通过 visible: falsefill_in没有意义。

原因fill_in没有找到您的字段是因为它按 id、名称或关联的标签文本查找字段。您没有显示页面的实际 HTML,但事实上“用户名”和“密码”实际上不在标签元素中,这意味着您无法通过关联的标签文本进行查找,因此这将不起作用。您可以将文本放入 <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'

匹配字段名称属性。

关于ruby-on-rails - Capybara::ElementNotFound:无法找到可见字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48938918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com