Capybara has_field does not find the field found by has_selector

I am currently writing a Cucumber function for a messaging system in a Rails application. This is one of my steps.

Then(/^they should see the message displayed in their language$/) do id = "message_to_#{@family.id}" expect(page).to have_selector("textarea##{id}") save_and_open_page expect(page).to have_field(id, type: :textarea) end 

The first statement passes, but the second fails. When I check the markup created using save_and_open_page, the following element is present:

 <textarea cols="22" disabled="disabled" id="message_to_13" name="body" placeholder="Hallo, Ich bin sehr interessiert an deinem Profil. WΓΌrdest du gerne mit mir in Kontakt treten?" rows="7"></textarea> 

The error message displayed for the second test:

 expected to find field "message_to_13" but there were no matches. Also found "", which matched the selector but not all filters. (Capybara::ExpectationNotMet) 

I am tearing my hair off here to understand why Capybara can find this element that is explicitly present using has_selector but not with has_field?

+4
source share
3 answers

The problem is that textarea has an attribute disabled="disabled" , that is, this field is disabled. By default, Capybara ignores disabled fields. New in Capybara 2.1 is the option to search for disabled fields .

Adding an option :disabled => true will solve your problem:

 expect(page).to have_field(id, :type => 'textarea', :disabled => true) 

Note:

  • When enabled :disabled => true field must be disabled. The default is :disabled => false , which only matches fields that are not disabled.
  • Type value: must be a string. As can be seen from the above, it is :type => 'textarea') . Using the character :type => :textarea will not work.
+9
source

Is it possible that you have another element with an identifier, name or label that matches "message_to_13"?

Due to the fact that the error message indicates that he found something with the message message_to_13, but this is not a text field. You can also try passing: textarea as a string is not a character.

0
source

Your test looks for an input field of type textarea. Text fields are not input fields, they are text fields. Try resetting type: :textarea .

See here: https://github.com/jnicklas/capybara/issues/978

0
source

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


All Articles