How do I check that a form is pre-populated with values using Cucumber and Webrat?
bdd, cucumber, regex, ruby-on-rails, webrat
Solution
Have a look at features/step_definitions/webrat_steps.rb, the following step definition looks like what you are looking for:
Then /^the "([^\"]*)" field should contain "([^\"]*)"$/ do |field, value|
field_labeled(field).value.should =~ /#{value}/
end
Problem
I am learning Cucumber and Webrat with Rails and would like some advice on the best way to test an "edit" form. When I browse to a user's profile I am presented with an edit form with the user's information pre-populated in the form fields. I would like to be able to test that the fields do in fact contain the information I expect. Here's my scenario: ``` Scenario: View My Profile Given I am logged in as "Mike" with password "secret" When I go to my profile page Then I should see "Mike" in the "Login" field And I should see "mike@email.com" in the "Email" field And I should see a blank "Password" field And I should see a blank "Password confirmation" field ``` Cucumber tells me, correctly, that I need to define the following custom steps: ``` Then /^I should see "([^\"]*)" in the "([^\"]*)" field$/ do |arg1, arg2| pending end Then /^I should see a blank "([^\"]*)" field$/ do |arg1| pending end ``` I'm sure I can figure out some nasty regex to implement evaluating these steps, but I feel there must be something already existing or more elegant that I can do. How do you evaluate forms with data pre-populated in the form fields?