Capybara::ElementNotFound: Unable to find visible field

时光怂恿深爱的人放手 提交于 2019-12-07 04:47:05

问题


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

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.

Appreciate for the help from the pro out there.


回答1:


You can't fill in fields that are non-visible, so passing visible: false to fill_in makes no sense.

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'

will probably work, matching on the fields ids,

or

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

matching on the fields name attributes.




回答2:


I've resolved the same problem with that way: in some of my js code were `` instead of ''. Browser can understand that syntax (it `` needs for interpolation), but Capybara couldn't.

When I've changed `` to '', the problem is gone.



来源:https://stackoverflow.com/questions/48938918/capybaraelementnotfound-unable-to-find-visible-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!