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

FormsRspecCapybara

Forms Problem Overview


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?

Forms Solutions


Solution 1 - Forms

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.

Solution 2 - Forms

You can use an xpath query to check if there's an input element with a particular value (e.g. 'John'):

expect(page).to have_xpath("//input[@value='John']")

See http://www.w3schools.com/xpath/xpath_syntax.asp for more info.

For perhaps a prettier way:

expect(find_field('Your name').value).to eq 'John'

EDIT: Nowadays I'd probably use have_selector

expect(page).to have_selector("input[value='John']")

If you are using the page object pattern(you should be!)

class MyPage < SitePrism::Page
  element :my_field, "input#my_id"
  
  def has_secret_value?(value)
    my_field.value == value
  end
end

my_page = MyPage.new

expect(my_page).to have_secret_value "foo"

Solution 3 - Forms

If you specifically want to test for a placeholder, use:

page.should have_field("some_field_name", placeholder: "Some Placeholder")

or:

expect(page).to have_field("some_field_name", placeholder: "Some Placeholder")

If you want to test the user-entered value:

page.should have_field("some_field_name", with: "Some Entered Value")

Solution 4 - Forms

I was wondering how to do something slightly different: I wanted to test whether the field had some value (while making use of Capybara's ability to re-test the matcher until it matches). It turns out that it's possible to use a "filter block" to do this:

expect(page).to have_field("field_name") { |field|
  field.value.present?
}

Solution 5 - Forms

If the field is a hidden one with an id of 'some_field', then you can use

expect(find("input#somefield", :visible => false).value).to eq 'John'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarc-Andr&#233; LafortuneView Question on Stackoverflow
Solution 1 - FormsfqxpView Answer on Stackoverflow
Solution 2 - FormsDVGView Answer on Stackoverflow
Solution 3 - FormsJim StewartView Answer on Stackoverflow
Solution 4 - FormsjonleightonView Answer on Stackoverflow
Solution 5 - FormsObromiosView Answer on Stackoverflow