Forum: RSpec webrat cannot find label?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2008-11-13 20:04
(Received via mailing list)
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 <do what> 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:ByrneJB@Harte-Lyne.ca
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
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2008-11-13 20:42
(Received via mailing list)
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:ByrneJB@Harte-Lyne.ca
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
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2008-11-13 20:47
(Received via mailing list)
On Thu, Nov 13, 2008 at 1:41 PM, James B. Byrne <byrnejb@harte-lyne.ca>
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?
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2008-11-13 21:01
(Received via mailing list)
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:ByrneJB@Harte-Lyne.ca
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
B6adc9e099cf9c257e5e29d8be8ebe4c?d=identicon&s=25 Mike Sassak (Guest)
on 2008-11-13 22:00
(Received via mailing list)
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.

Mike

On Nov 13, 2008 2:42 PM, "James B. Byrne" <byrnejb@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
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:...
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2008-11-13 22:56
(Received via mailing list)
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:ByrneJB@Harte-Lyne.ca
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
B6adc9e099cf9c257e5e29d8be8ebe4c?d=identicon&s=25 Mike Sassak (Guest)
on 2008-11-14 06:46
(Received via mailing list)
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-tex...

HTH,
Mike
3880f04333bf8375b44cd6aa135406c7?d=identicon&s=25 Bart Zonneveld (Guest)
on 2008-11-14 13:53
(Received via mailing list)
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
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2008-11-14 18:32
Bart Zonneveld 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:

<p>
<label class="input_box" for="entity_entity_legal_form" title="Legal
form is one of PERS, CORP or PART">
  <b>Legal Form: </b><br />
</label>
<select id="entity_entity_legal_form" name="entity[entity_legal_form]">
  <option value="">Select the type of entity</option>
  <option value="CORP">CORP - Corporation</option>
  <option value="PART">PART - Partnership</option>
  <option value="PERS">PERS - Natural Person</option>
</select>
</p>

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.
B6adc9e099cf9c257e5e29d8be8ebe4c?d=identicon&s=25 Mike Sassak (Guest)
on 2008-11-14 19:29
(Received via mailing list)
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?
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2008-11-14 19:45
Mike Sassak 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
...
    <p>
      <b>
      <label class="input_box" for="entity_entity_legal_form"
title="Legal form is one of PERS, CORP or PART">Legal Form</label>
    </b> <br />


      <select id="entity_entity_legal_form"
name="entity[entity_legal_form]"><option value="">Select the type of
entity</option>
<option value="CORP">CORP - Corporation</option>
<option value="PART">PART - Partnership</option>
<option value="PERS">PERS - Natural Person</option></select>    </p>
...

# 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'
1b51d01b6382cbe108f9bc72ef754964?d=identicon&s=25 Barry Mitchelson (Guest)
on 2008-11-14 19:55
(Received via mailing list)
On Thu, Nov 13, 2008 at 7:41 PM, James B. Byrne
<byrnejb@harte-lyne.ca>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
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2008-11-14 21:38
Barry Mitchelson wrote:
> On Thu, Nov 13, 2008 at 7:41 PM, James B. Byrne
> <byrnejb@harte-lyne.ca>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.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2008-11-14 21:45
James Byrne 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
This topic is locked and can not be replied to.