Search code examples
rspecruby-on-rails-5capybara

Unable to find fields that is not disabled


So I am currently doing some tests using capybara and rspec, I am having this error messgae

Users Validation failed: First name can't be blank, Last name can't be blank, System generated password can't be blank Capybara starting Puma... * Version 3.11.4 , codename: Love Song * Min threads: 0, max threads: 4 * Listening on tcp://127.0.0.1:42509 Unable to find visible field "first_name" that is not disabled Creates a new User

Below is my _form.html.haml code :

panel-body
  = simple_form_for @user do |f|
    .form-inputs
      .col-md-6
        = f.input :first_name, label: I18n.t('user.first_name')
        = f.input :middle_name, label: I18n.t('user.middle_name')
        = f.input :last_name, label: I18n.t('user.last_name')
        = f.input :username, label: I18n.t('user.username')

      .col-md-6
        - if current_user.role? "Administrator"
          = f.input :user_role, label: I18n.t('user.role'), collection: User::ROLES, include_blank: false
        - else
          = f.input :user_role, label: I18n.t('user.role'), collection: User::ROLES, include_blank: false, disabled: true
          = f.hidden_field :user_role

    .col-md-12
      .form-actions
        - if @user.new_record?
          = f.button :submit, I18n.t('user.create_user'), class: "btn btn-primary"
          = link_to I18n.t('cancel'), users_path, class: "btn btn-default"
        - else
          = f.button :submit, I18n.t('user.update_user'), class: "btn btn-primary"
          = link_to I18n.t('cancel'), user_path(@user), class: "btn btn-default"

And here is my feature test (spec/features/users/create_user.rb):

require 'rails_helper'

RSpec.feature "Users", :type =>
:feature do
    before do
        begin
         
         login_as(FactoryBot.create(:user, :admin)) # assumes you have a factory named `admin` that will create a user with the permissions to create other users
        
        rescue StandardError => e
            puts "#{e.message}"
        end
    end
   
    it "Creates a new User" do
        begin
           visit "users/"
            
                new_user = FactoryBot.build(:user) 

                fill_in "first_name", with: I18n.t('new_user.first_name') #new_user.first_name
                fill_in "middle_name", with: I18n.t('new_user.middle_name')#new_user.middle_name
                fill_in "last_name", with: I18n.t('new_user.last_name')#new_user.last_name
                fill_in "username", with: I18n.t('new_user.username')#new_user.username
                click_button "New User"
                expect(page).to have_text "User was successfully created."
        rescue StandardError => e
           puts "#{e.message}"
        end
    end
end

And below is my factoryBot

#spec/factories/users.rb

FactoryBot.define do
	  factory :user do
	    first_name { 'System' }
	  	last_name  { 'Administrator' }
	    username {'admin'}
		password {'password'}	
		system_generated_password {'password'}
		
		trait :admin do 
		   	user_role {"Administrator"} 
		end
	  end	
end


Solution

  • The fact that you're getting validation messages "Users Validation failed: First name can't be blank..." before Capybara has even started the server tells me that your factory isn't producing a valid user record. You should use the FactoryBot linting feature - https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#linting-factories - to make sure all your factories produce valid records before the tests are run. You will also want to look into using sequences - https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#sequences - for the parts of your factories that have uniqueness requirements (username for instance) so that it can be used to create two or more records simultaneously.

    Note: There is no need for the begin ... rescue .. end blocks you have wrapping everything - any raised errors will get printed anyway and by rescuing them you are actually preventing valid test failures.