Reputation: 3552
I'm currently writing a Cucumber feature for a messaging system in a Rails app. 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 assertion passes, but the second fails. When I inspect the markup created by 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 is:
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'm tearing my hair out here to understand why Capybara can find this element that is obviously present using have_selector, but not with have_field?
Upvotes: 6
Views: 10217
Reputation: 46836
The problem is that the textarea has the disabled="disabled"
attribute, meaning it is a disabled field. By default, Capybara ignores disabled fields. New in Capybara 2.1 is the option to look for disabled fields.
Adding the disabled: true
option will solve your problem:
expect(page).to have_field(id, type: 'textarea', disabled: true)
Note:
disabled: true
, the field must be disabled. The default is disabled: false
, which only matches fields that are not disabled.type: 'textarea')
. Using a symbol, type: :textarea
will not work.Upvotes: 15
Reputation:
Your test is looking for an input field of type textarea. Textareas are not input fields, they are textareas. Try dropping the type: :textarea
.
See here: https://github.com/jnicklas/capybara/issues/978
Upvotes: 0
Reputation: 3737
It is possible, that you have some other element with either id, name or label that matches 'message_to_13'?
Because that's what the error message indicates - that it found something with 'message_to_13' but it wasn't a textarea. You could also try passing :textarea as string not symbol.
Upvotes: 0