Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.


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"

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")