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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With