# Webrat cannot find label?

**URL:** <https://www.ruby-forum.com/t/webrat-cannot-find-label/153239>\
**Category:** RSpec\
**Created:** [November 13, 2008, 8:04pm UTC](https://www.ruby-forum.com/t/webrat-cannot-find-label/153239 "2008-11-13T20:04:50Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 13, 2008, 8:04pm UTC](https://www.ruby-forum.com/t/webrat-cannot-find-label/153239/1 "2008-11-13T20:04:50Z")

</div>

Cucumber 0.1.9, Rails 2.2.0RC1, Ruby 1.8.6, Webrat… etc.

Here is the scenario:

Scenario: Attempt to ADD a VALID entity  
Given I am on the new entity page  
When I fill in “Common Name” with “My Common Name”  
And I to select “CORP” from the list labelled “Legal  
Form”  
And I fill in “Full Legal Name” with "My Full Legal Name"e  
And I press “Create”  
Then I should see “was successfully created”

What is the correct syntax to test selecting from a range of values  
present in an input box? I have tried several variants of “select x  
from  
y” and I have tried to match the syntax of webrat to cucumber but  
nothing  
I do seems to work as I anticipate. Do I have to write my own step for  
this sort of thing? The “fill in” syntax seems to find a generated  
step,  
because I certainly have not written one for it. I so,. should not all  
of  
the input methods do likewise?

Guidance will be greatly appreciated.

–  
\*\*\* E-Mail is NOT a SECURE channel \*\*\*  
James B. Byrne [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Harte & Lyne Limited [http://www.harte-lyne.ca](http://www.harte-lyne.ca)  
9 Brockley Drive vox: +1 905 561 1241  
Hamilton, Ontario fax: +1 905 561 0757  
Canada L8E 3C3

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 13, 2008, 8:42pm UTC](https://www.ruby-forum.com/t/webrat-cannot-find-label/153239/2 "2008-11-13T20:42:58Z")

</div>

On Thu, November 13, 2008 14:02, James B. Byrne wrote:

> What is the correct syntax to test selecting from a range of values  
> 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  
submitted.

Examples:

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)
  /usr/lib/ruby/gems/1.8/gems/webrat-0.3.2/lib/webrat/core/scope.rb:91:in

```

`selects' /usr/lib/ruby/gems/1.8/gems/webrat-0.3.2/lib/webrat/rails.rb:88:in `send’

So, checking out webrat.steps I see this:

When /^I select “(._)" from "(._)”$/ do |value, field|  
selects(value, :from =\> field)  
end

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:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Harte & Lyne Limited [http://www.harte-lyne.ca](http://www.harte-lyne.ca)  
9 Brockley Drive vox: +1 905 561 1241  
Hamilton, Ontario fax: +1 905 561 0757  
Canada L8E 3C3

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 13, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/webrat-cannot-find-label/153239/3 "2008-11-13T20:47:06Z")

</div>

On Thu, Nov 13, 2008 at 1:41 PM, James B. Byrne [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> selects “February”, :from =\> “Event Month”  
> You have a nil object when you didn’t expect it!  
> selects(value, :from =\> field)  
> end
> 
> 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?

It seems like it. Webrat has its own tracker at  
[http://webrat.lighthouseapp.com](http://webrat.lighthouseapp.com) - can you submit a ticket there  
please?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 13, 2008, 10:00pm UTC](https://www.ruby-forum.com/t/webrat-cannot-find-label/153239/4 "2008-11-13T22:00:28Z")

</div>

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:

Confirm Password

I changed it to this:

\<%= f.label :password\_confirmation, “Confirm Password” %\>  
\<%= f.text\_field :password\_confirmation %\>

Which produces this:

Confirm  
Password

And it worked fine.

Mike

On Nov 13, 2008 2:42 PM, “James B. Byrne” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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  
submitted.

Examples:

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)
 /usr/lib/ruby/gems/1.8/gems/webrat-0.3.2/lib/webrat/core/scope.rb:91:in

```

`selects' /usr/lib/ruby/gems/1.8/gems/webrat-0.3.2/lib/webrat/rails.rb:88:in `send’

So, checking out webrat.steps I see this:

When /^I select “(._)" from "(._)”$/ do |value, field|  
selects(value, :from =\> field)  
end

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:…

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 13, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/webrat-cannot-find-label/153239/5 "2008-11-13T21:01:47Z")

</div>

On Thu, November 13, 2008 14:41, James B. Byrne wrote:

> 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?

Does anyone know why webrat does this for select:

```
def selects(option_text, options = {})
  find_select_option(option_text, options[:from]).choose
end

```

But uses field\_locator for everything else except click:

def fill\_in(field\_locator, options = {})  
def uncheck(field\_locator)  
def choose(field\_locator)  
def attach\_file(field\_locator, path, content\_type = nil

–  
\*\*\* E-Mail is NOT a SECURE channel \*\*\*  
James B. Byrne [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Harte & Lyne Limited [http://www.harte-lyne.ca](http://www.harte-lyne.ca)  
9 Brockley Drive vox: +1 905 561 1241  
Hamilton, Ontario fax: +1 905 561 0757  
Canada L8E 3C3

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 14, 2008, 6:46am UTC](https://www.ruby-forum.com/t/webrat-cannot-find-label/153239/6 "2008-11-14T06:46:38Z")

</div>

Hi James,

If you’re using the generated common\_webrat steps that cucumber  
generates for Rails projects, then “I select X from Y” should work  
just fine. The step itself calls Webrat’s select method like so:  
selects(value, :from =\> field).

You can see an example of sorts in the first comments here:  
[http://www.benmabey.com/2008/02/04/rspec-plain-text-stories-webrat-chunky-bacon/](http://www.benmabey.com/2008/02/04/rspec-plain-text-stories-webrat-chunky-bacon/)

HTH,  
Mike

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 14, 2008, 1:53pm UTC](https://www.ruby-forum.com/t/webrat-cannot-find-label/153239/7 "2008-11-14T13:53:34Z")

</div>

On 13-nov-2008, at 20:41, James B. Byrne wrote:

> selects “February”, :from =\> “Event Month”  
> You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.choose (NoMethodError)  
> /usr/lib/ruby/gems/1.8/gems/webrat-0.3.2/lib/webrat/core/  
> scope.rb:91:in  
> `selects' /usr/lib/ruby/gems/1.8/gems/webrat-0.3.2/lib/webrat/rails.rb: 88:in `send’

Are you 100% sure the thing you are selecting exists in the select  
box? That has bitten me in the butt a few times..

cheers,  
bartz

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 13, 2008, 10:56pm UTC](https://www.ruby-forum.com/t/webrat-cannot-find-label/153239/8 "2008-11-13T22:56:49Z")

</div>

On Thu, November 13, 2008 14:59, James B. Byrne wrote:

> 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?

It seems as if I have run into two separate webrat problems. One is  
reported as ticket 28 and is fixed by the patch submitted by ticket 29.  
This involves not having the input field tags nested within the  
associated  
label tags. However, even with this cured the locator still returns a  
nil  
object for the select, whether searched for by label or by name, causing  
a  
failure so there is still a problem with webrat. Does anyone have any  
workarounds that they can suggest?

–  
\*\*\* E-Mail is NOT a SECURE channel \*\*\*  
James B. Byrne [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Harte & Lyne Limited [http://www.harte-lyne.ca](http://www.harte-lyne.ca)  
9 Brockley Drive vox: +1 905 561 1241  
Hamilton, Ontario fax: +1 905 561 0757  
Canada L8E 3C3

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 14, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/webrat-cannot-find-label/153239/9 "2008-11-14T18:32:44Z")

</div>

Bart Z. wrote:

> On 13-nov-2008, at 20:41, James B. Byrne wrote:

> Are you 100% sure the thing you are selecting exists in the select  
> box? That has bitten me in the butt a few times..
> 
> cheers,  
> bartz

This is what Rails generates from the view:

**Legal Form:**   
 Select the type of entity CORP - Corporation PART - Partnership PERS - Natural Person

So, it appears to me that the label and target ids match,  
“entity\_entity\_legal\_form”; and the value that I am testing, “CORP” is  
available in the list of values.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 14, 2008, 7:29pm UTC](https://www.ruby-forum.com/t/webrat-cannot-find-label/153239/10 "2008-11-14T19:29:16Z")

</div>

What happens if you remove the b and br tags from within the label and  
change the text to “Legal Form” only, without the colon?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 14, 2008, 7:45pm UTC](https://www.ruby-forum.com/t/webrat-cannot-find-label/153239/11 "2008-11-14T19:45:19Z")

</div>

Mike S. wrote:

> What happens if you remove the b and br tags from within the label and  
> change the text to “Legal Form” only, without the colon?

I get exactly the same behaviour:

entities/new  
…

**Legal Form**

```
  <select id="entity_entity_legal_form" 

```

name=“entity[entity\_legal\_form]”\>Select the type of  
entity

CORP - Corporation PART - Partnership PERS - Natural Person 

...
# rake features

…  
And I select “CORP” from “Legal Form” #  
features/step\_definitions/webrat\_steps.rb:16  
You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.choose (NoMethodError)  
/usr/lib/ruby/gems/1.8/gems/webrat-0.3.2/lib/webrat/core/scope.rb:91:in  
`selects’

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 14, 2008, 9:38pm UTC](https://www.ruby-forum.com/t/webrat-cannot-find-label/153239/12 "2008-11-14T21:38:59Z")

</div>

Barry Mitchelson wrote:

> On Thu, Nov 13, 2008 at 7:41 PM, James B. Byrne  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> 
> > On Thu, November 13, 2008 14:02, James B. Byrne wrote:
> > 
> > > What is the correct syntax to test selecting from a range of values  
> > > 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  
> > submitted.
> 
> The API says to use the option text, not the value.

…

> CORP is the value of the option, the text is “CORP - Corporation”
> 
> Does this work?
> 
> Barry

I’ll be damned… It works.

Thank you so very much. I would never have connected “option\_text” to  
the description portion of the list.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 14, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/webrat-cannot-find-label/153239/13 "2008-11-14T21:45:42Z")

</div>

James B. wrote:

> I’ll be damned… It works.
> 
> Thank you so very much. I would never have connected “option\_text” to  
> the description portion of the list.

This is what I ended up with:

features/entity.features  
…  
Scenario: Attempt to ADD a VALID entity  
Given I am on the new entity page  
When I add a new entity correctly  
And I press “Create”  
Then I should see a success confirmation  
…

steps/entity.steps  
…  
Given /I am on the new entity page/ do  
visits “/entities/new”  
end

When /I add a new entity correctly/ do  
fills\_in(“Common Name”, :with =\> “My Common Name”)  
fills\_in(“Full Legal Name”, :with =\> “My Legal Name”)  
selects(“CORP - Corporation”, :from =\> “Legal Form”)  
end

Then /I should see a success confirmation/ do  
response.body.should =~ /successfully created/m  
end

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 14, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/webrat-cannot-find-label/153239/14 "2008-11-14T19:55:44Z")

</div>

On Thu, Nov 13, 2008 at 7:41 PM, James B. Byrne  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> On Thu, November 13, 2008 14:02, James B. Byrne wrote:
> 
> > What is the correct syntax to test selecting from a range of values  
> > 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  
> submitted.

The API says to use the option text, not the value.

> But when I try to use something like:
> 
> And I select “CORP” from “Legal Form”  
> # features/step\_definitions/webrat\_steps.rb:16

CORP is the value of the option, the text is “CORP - Corporation”

Does this work?

Barry
