简体   繁体   English

Capybara :: ElementNotFound:无法找到可见字段

[英]Capybara::ElementNotFound: Unable to find visible field

Received following error for my Capybara test in terminal: 我的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)>

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

My view file 我的视图文件

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

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

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. 我做了一些研究,这是由于capybara 2x上的问题,大多数人通过添加可见:false来解决,但这个解决方案对我不起作用。

Appreciate for the help from the pro out there. 感谢专业人士的帮助。

You can't fill in fields that are non-visible, so passing visible: false to fill_in makes no sense. 您无法填写不可见的字段,因此将visible: false传递给fill_in是没有意义的。

The reason fill_in isn't finding your fields is because it finds fields by id, name, or associated label text. fill_in找不到您的字段的原因是因为它按ID,名称或关联的标签文本查找字段。 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. 您没有显示页面的实际HTML,但“用户名”和“密码”实际上不在标签元素中这一事实意味着您无法通过关联的标签文本找到这样的内容。 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. 你可以把文成<label>元素和准与相应的字段( for属性或包装),或者你可以选择字段的ID或名字FILL_IN。 Without the actual HTML it's impossible to say for sure, but something like 如果没有实际的HTML,就不可能肯定地说,但是类似的东西

fill_in 'user_username', with: 'Ali'
fill_in 'user_password', with: 'ali'

will probably work, matching on the fields ids, 可能会工作,匹配字段ID,

or 要么

fill_in 'user[username]', with: 'Ali'
fill_in 'user[password]', with: 'ali'

matching on the fields name attributes. 匹配字段名称属性。

I've resolved the same problem with that way: in some of my js code were `` instead of ''. 我用这种方式解决了同样的问题:在我的一些js代码中,``而不是''。 Browser can understand that syntax (it `` needs for interpolation), but Capybara couldn't. 浏览器可以理解语法(它需要插值),但Capybara不能。

When I've changed `` to '', the problem is gone. 当我改变``到''时,问题就消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM