Webrat cannot find label?


#1

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
Harte & Lyne Limited http://www.harte-lyne.ca
9 Brockley Drive vox: +1 905 561 1241
Hamilton, Ontario fax: +1 905 561 0757
Canada L8E 3C3


#2

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:insend’

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
Harte & Lyne Limited http://www.harte-lyne.ca
9 Brockley Drive vox: +1 905 561 1241
Hamilton, Ontario fax: +1 905 561 0757
Canada L8E 3C3


#3

On Thu, Nov 13, 2008 at 1:41 PM, James B. Byrne 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 - can you submit a ticket there
please?


#4

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 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:insend’

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


#5

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
Harte & Lyne Limited http://www.harte-lyne.ca
9 Brockley Drive vox: +1 905 561 1241
Hamilton, Ontario fax: +1 905 561 0757
Canada L8E 3C3


#6

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/

HTH,
Mike


#7

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:insend’

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


#8

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
Harte & Lyne Limited http://www.harte-lyne.ca
9 Brockley Drive vox: +1 905 561 1241
Hamilton, Ontario fax: +1 905 561 0757
Canada L8E 3C3


#9

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.


#10

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?


#11

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’


#12

Barry Mitchelson wrote:

On Thu, Nov 13, 2008 at 7:41 PM, James B. Byrne
removed_email_address@domain.invalidwrote:

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.


#13

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


#14

On Thu, Nov 13, 2008 at 7:41 PM, James B. Byrne
removed_email_address@domain.invalidwrote:

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