Vasily
Vasily

Reputation: 461

How to do expectations about field content in Capybara

I am trying to write some tests that verify forms being correctly field.

I saw some snippets with "page.should ..."and "assert page...", but I cannot google definite documentation what can be used and how you are supposed to use it.

Also, I am wondering if it is possible to use regex in field content or just strings.

Upvotes: 1

Views: 1119

Answers (1)

Justin Ko
Justin Ko

Reputation: 46836

There are various rspec matchers that have been developed over time. I believe the most popular right now is the rspec-expectations. You can combine this with the Capybara matchers.

To check that a text field has a certain value, you would do:

expect(page).to have_field('field_id', :with => 'test')

have_field is like the other Capybara finder methods. 'field_id' can be the field's id, name or label. 'test' is the value the text field should have.

The above could also be done with the following, but is not as nice to read.

expect(find_field('field_id').value).to eq('test')

For regex matches, I do not believe you can use the first example. You will have to use the latter appoach with the match method:

expect(find_field('field_id').value).to match(/te/)

Upvotes: 3

Related Questions