HTML 5 form validation testing using simple_form Rails

I use devose and simple_form for my list of todo applications. Now I have the following code for my users /edit.html.erb

<%= content_for :title,'Edit profile' %> <h2>Edit profile</h2> <%= simple_form_for current_user, class: 'form-horizontal' do |f| %> <%= f.input :nick_name %> <%= f.submit 'Update profile' %> <% end %> 

My user.rb looks like this:

 class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email,:nick_name, :password, :password_confirmation, :remember_me validates :nick_name, presence: true # the other fields already validated by devise has_many :lists , dependent: :destroy end 

Now, when I click the submit button with an empty nick_name field, I get a popup reminder. This is not like a normal browser alert, I think it is an HTML5 feature. I get this message Please fill out this field as a pop-up window under an empty field. I disabled javascript, but it still shows the same message. This is my nick_name input field:

 <input class="string required" id="user_nick_name" name="user[nick_name]" required="required" size="50" type="text"> 

Now, when I remove presence validation for nick_name in my model, this popup does not appear. When the check line is commented out,

 <input class="string optional" id="user_nick_name" name="user[nick_name]" size="50" type="text"> 

Simple_formation does some backstage magic?

Since this popup does not show any trace of html code, how to check this check in capybara / rspec?

+6
source share
1 answer

Actually, you can check it by finding the html attribute required="required" with the following code example:

 expect(page).to have_xpath("//input[@required='required']") 
+3
source

Source: https://habr.com/ru/post/948396/


All Articles