ruby-on-rails-3rspecintegration-testingwebrat

Rails 3 integration testing - Using webrat fill_in not finding fields


I'm learning testing right now, but am having some issues with Webrat not finding form fields using fill_in even though I've verified it is on the correct page. Does Webrat work off of field names or ID's? I've tried using Ruby symbols and form names to access the field, but neither works in my case. Do you see anything incorrect with my implementation?

The error message:

5) Forwarding should forward the user to the requested page after signin
   Failure/Error: integration_sign_in(user)
   Could not find field: :email

The test code:

it "should forward the user to the requested page after signin" do
  user = Factory(:user)
  visit edit_user_path(user)

  # The test automatically follows the redirect to the signin page
  puts current_url
  integration_sign_in(user)

  # The test follows the redirect again, this time to users/edit
  response.should render_template('users/edit')
end

where integration_sign_in is in the spec_helper.rb

def integration_sign_in(user) 
  fill_in :email,   :with => user.email 
  fill_in :password, :with => user.password 
  click_button
end

The form field HTML:

<form action='/sessions' class='mtm' id='sign-in-form' method='post'> 
    <input name='authenticity_token' type='hidden' value='iIIqT6bUwiJkpqpgxm5sjAj3egrNcEgeXPsYmbKQ02U='> 
        <div class='landingSignInForm'> 
          <label class='mas signin-label' for='email'>E-mail:</label> 
          <input class="mls ras" id="email" name="email" placeholder="e-mail address" type="text" /> 
          <label class='mas signin-label' for='password'>Password:</label> 
          <input class="mls ras" id="password" name="password" placeholder="password" type="password" /> 
          <input checked='checked' class='mls mtm' name='remember' type='checkbox' value='permanent'> 
          <span class='remember-me-label'>Keep me signed in</span> 
          <input class='mls mvm ram medium silver button' name='submit' type='submit' value='Sign in'> 
          <a class='forgot-password' href='#'>Forget your password?</a> 
        </div> 
</form> 

Solution

  • Have you tried using the css selector for ID instead of just passing a symbol that matches? I tried reading through the webrat source briefly to determine if it treats symbols as strings internally, wondering if that's your issue. I can't find any examples of webrat syntax using fill_in :symbol only fill_in 'string'

    Try this:

    def integration_sign_in(user) 
      fill_in 'email',   :with => user.email 
      fill_in 'password', :with => user.password 
      click_button
    end
    

    or the css selector route for your ids:

    def integration_sign_in(user) 
      fill_in '#email',   :with => user.email 
      fill_in '#password', :with => user.password 
      click_button
    end