How can I check that a form field is prefilled correctly using capybara?

Marc-André Lafortune picture Marc-André Lafortune · May 8, 2012 · Viewed 74.6k times · Source

I have a field with a proper label that I can fill in with capybara without a problem:

fill_in 'Your name', with: 'John'

I'd like to check the value it has before filling it in and can't figure it out.

If I add after the fill_in the following line:

find_field('Your name').should have_content('John')

That test fails, although the filling just before worked as I've verified by saving the page.

What am I missing?

Answer

fqxp picture fqxp · May 16, 2013

Another pretty solution would be:

page.should have_field('Your name', with: 'John')

or

expect(page).to have_field('Your name', with: 'John')

respectively.

Also see the reference.

Note: for disabled inputs, you'll need to add the option disabled: true.