How do I check that a form is pre-populated with values using Cucumber and Webrat?
Asked Answered
T

1

10

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 "[email protected]" 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?

Thomey answered 23/9, 2009 at 5:27 Comment(0)
M
15

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
Manager answered 23/9, 2009 at 9:46 Comment(1)
Dang. I'm not sure how I missed that! Based on this I created a version that works for empty fields. As in /^the "([^\"]*)" field should be blank$/" being implemented as "field_labeled(field).value.should == nil". Thanks! end`Thomey

© 2022 - 2024 — McMap. All rights reserved.