[rspec-users] webrat cannot find label?

Mike Sassak msassak at gmail.com
Thu Nov 13 15:53:34 EST 2008

That's odd. I use the same step in my own scenarios and it works fine.
One problem I did run into is that the label alone isn't always enough
for webrat to find the input field. The id on the label must match the
id on the labeled field for selection by label to work. Sometimes they
differ, for example, if you use form_for() but don't call the label
method on the yielded form object, and instead use the label_tag
helper method.

I ran into this just yesterday with one of the views
restful_authentication generates.

It had generated something like this (for a Reviewer model):

<%= label_tag 'password_confirmation', 'Confirm Password' %>
<%= f.text_field :password_confirmation %>

Which produced HTML like this:

<label for="password_confirmation">Confirm Password</label>
<input id="reviewer_password_confirmation"
name="reviewer[password_confirmation]" size="30" type="password" />

I changed it to this:

<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.text_field :password_confirmation %>

Which produces this:

<label for="reviewer_password_confirmation">Confirm Password</label><br/>
<input id="reviewer_password_confirmation"
name="reviewer[password_confirmation]" size="30" type="password" />

And it worked fine.


On Nov 13, 2008 2:42 PM, "James B. Byrne" <byrnejb at harte-lyne.ca> wrote:

On Thu, November 13, 2008 14:02, James B. Byrne wrote: > > What is the
correct syntax to test sele...

> present in an input box?  ...

The webrat API specifies this:

 selects(option_text, options = {})

Verifies that a an option element exists on the current page with the
specified text. You can optionally restrict the search to a specific
select list by assigning options[:from] the value of the select list's
name or a label. Stores the option's value to be sent when the form is


 selects "January"
 selects "February", :from => "event_month"
 selects "February", :from => "Event Month"

But when I try to use something like:

   And I select "CORP" from "Legal Form"
       # features/step_definitions/webrat_steps.rb:16

This is what happens:

     You have a nil object when you didn't expect it!
     The error occurred while evaluating nil.choose (NoMethodError)

So, checking out webrat.steps I see this:

When /^I select "(.*)" from "(.*)"$/ do |value, field|
 selects(value, :from => field)

So, as far as I can make out, given the webrat API specifies that a label
is an acceptable target, this should work.  But it evidently does not.  Is
this a webrat problem then?

-- *** E-Mail is NOT a SECURE channel *** James B. Byrne mailto:...

More information about the rspec-users mailing list