Forum: RSpec cucumber, finding a row in a table

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 2009-01-05 22:18
I generate the following html on the page in question

<tr id="entity_id_1">
    <td>Just An Entity</td>
    <td></td>
    <td>CORP</td>
    <td>000001</td>
    <td><a href="/entities/1">Show Entity</a></td>
    <td><a href="/entities/1/edit">Edit Entity</a></td>
    <td><a href="/entities/1" onclick="if (confirm('Are you sure?')) \
        { var f = document.createElement('form'); f.style.display =
'none';
          this.parentNode.appendChild(f); f.method = 'POST';
          f.action = this.href;var m = document.createElement('input');
          m.setAttribute('type', 'hidden'); m.setAttribute('name',
'_method');
          m.setAttribute('value', 'delete');
f.appendChild(m);f.submit();
        };return false;">Destroy Entity</a>
    </td>
  </tr>

I have this step definition:

When /I delete the "(.*)" entity/ do |row|
  visits entities_url
  my_entity = Entity.find_by_entity_name(
              "my entity number #{row.hll_words_to_i}")
  within("table > tr#entity_id_" + my_entity.id.to_s) do
    puts "table > tr#entity_id_" + my_entity.id.to_s
    click_link "Destroy Entity"
  end
end

The puts statement displays this:

table > tr#entity_id_1

after wich I see this:

    When I delete the "first" entity    # features/app/models/entities
                                        #/step_definitions/entity_steps.rb:128
      You have a nil object when you didn't expect it!
      The error occurred while evaluating nil.to_html (NoMethodError)
      /usr/lib64/ruby/gems/1.8/gems/webrat-0.3.4/lib/webrat/core/scope.rb:176:in
`scoped_dom'
...

The table entries exist. The find_by_name returns a valid instance.  I
do not know what the nil object is.  Can someone point out to me what I
am missing?
F86901feca747abbb5c6c020362ef2e7?d=identicon&s=25 Zach Dennis (zdennis)
on 2009-01-06 00:57
(Received via mailing list)
On Mon, Jan 5, 2009 at 4:18 PM, James Byrne <lists@ruby-forum.com>
wrote:
>        { var f = document.createElement('form'); f.style.display =
>
> end
>      The error occurred while evaluating nil.to_html (NoMethodError)
>      /usr/lib64/ruby/gems/1.8/gems/webrat-0.3.4/lib/webrat/core/scope.rb:176:in
> `scoped_dom'
> ...
>
> The table entries exist. The find_by_name returns a valid instance.  I
> do not know what the nil object is.  Can someone point out to me what I
> am missing?

Does your outputted HTML properly include a tbody in the table? If so
the selector you are using won't work. You would need to make match
any descendant rather than any direct child, ie:  "table
tr#entity_id_1"

On a related note (although not specific your problem at hand) to
generate an id on an ActiveRecord model you can use the dom_id helper
method provided by Rails. ie: "dom_id(entity)". You can use this both
in your template and your steps/specs. And rather than hardcoding
"table tr" CSS selectors why not just give your table an id like
"entities" and then use dom_id to give your rows ids? Should you
change the display to a ul or ol you wouldn't have to go back and
change your CSS selectors,

--
Zach Dennis
http://www.continuousthinking.com
http://www.mutuallyhuman.com
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-01-06 22:08
Zach Dennis wrote:
>
> Does your outputted HTML properly include a tbody in the table? If so
> the selector you are using won't work. You would need to make match
> any descendant rather than any direct child, ie:  "table
> tr#entity_id_1"
>

I cannot find one if there is.

> On a related note (although not specific your problem at hand) to
> generate an id on an ActiveRecord model you can use the dom_id helper
> method provided by Rails. ie: "dom_id(entity)". You can use this both
> in your template and your steps/specs. And rather than hardcoding
> "table tr" CSS selectors why not just give your table an id like
> "entities" and then use dom_id to give your rows ids? Should you
> change the display to a ul or ol you wouldn't have to go back and
> change your CSS selectors,

Could you provide a reference to an example of how this is done?  I am
afraid that the API was insufficient to clarify this for me.

Thank you for your help.
F86901feca747abbb5c6c020362ef2e7?d=identicon&s=25 Zach Dennis (zdennis)
on 2009-01-07 01:56
(Received via mailing list)
On Tue, Jan 6, 2009 at 4:08 PM, James Byrne <lists@ruby-forum.com>
wrote:
>> On a related note (although not specific your problem at hand) to
>
When constructing HTML ids you can avoid concatenating strings, ie:
   "entity_id_" + my_entity.to_s

Instead you can use this:
   dom_id(entity)

You can also pass in additional identifiers:
   dom_id(entity, "show")

If you use this in your steps and in your views themselves it cleans
generating html ids up. In my cucumber env.rb I have added so the dom
helpers are available throughout my steps

  Cucumber::Rails::World.class_eval do
    include ActionView::Helpers::RecordIdentificationHelper
  end

Hope this helps,

--
Zach Dennis
http://www.continuousthinking.com
http://www.mutuallyhuman.com
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-01-08 19:13
I am still not getting this to work.  Here is what I have done:

In index.html.erb

  <table  id="entities" caption="Entity Listing"
          title="All Entites"
          border="1" cellpadding="2"cellspacing="6"
          style="width:80em;"
          summary="This layout lists all of the entities on file and
          provides links to maintain dependent roles and data.">

  <thead>
    <tr>
      <th style="width:20em;">Short Name</th>
...
    </tr>
...
  </thead>

  <tbody>

  <% for entity in @entities %>
  <tr id="<%=dom_id(entity)%>">
    <td><%=h entity.entity_name.titlecase -%></td>
...
    <td><%= "%06d" % entity.id -%></td>
    <td><%= link_to 'Show Entity', entity -%></td>
    <td><%= link_to 'Edit Entity', edit_entity_path(entity) -%></td>
    <td><%= link_to 'Destroy Entity', entity,
                    :confirm => 'Are you sure?',
                    :method => :delete -%></td>
  </tr>
...

In the step definition file:

When /I delete the "(.*)" entity/ do |row|
  visits entities_url
  puts entities_url
  my_entity = Entity.find_by_entity_name(
              "my entity number #{row.hll_words_to_i}")
  puts "selector found" if have_selector(
               "table > tbody > tr#" + dom_id(my_entity) + " > td > a")
  within("table > tbody > tr#" + dom_id(my_entity) + " > td > a") do
  click_link "Destroy Entity"
  end
end


The results:

  Scenario: Delete entity                   #
features/app/models/entities/entity.feature:32
    Given I have "4" valid entities         #
features/app/models/entities/step_definitions/entity_steps.rb:115
http://www.example.com/entities
selector found
    When I delete the "first" entity        #
features/app/models/entities/step_definitions/entity_steps.rb:128
      You have a nil object when you didn't expect it!
      The error occurred while evaluating nil.to_html (NoMethodError)
      /usr/lib64/ruby/gems/1.8/gems/webrat-0.3.4/lib/webrat/core/scope.rb:176:in
`scoped_dom'

The selector is found so what is my error?

I have backed off the selector element by element to "table > tbody >
tr#" + dom_id(myentity) with no change in the result. I always get a nil
object error.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-01-08 20:55
I have tried to simplify this as much as possible.  To the best of my
ability to determine when one selects an html element via id then one
obtains the entire element contents to the termination tag.

So, I did this:

  <tr id="<%=dom_id(entity, :list)%>">
    <td><%=h entity.entity_name.titlecase -%></td>
    <td></td>
    <td><%=h entity.entity_legal_form -%></td>
    <!-- See http://www.ruby-doc.org/core/classes/Kernel.html#M005984
-->
    <!-- and http://www.ruby-doc.org/core/classes/Kernel.html#M005984
-->
    <td><%= "%06d" % entity.id -%></td>
    <td id="<%=dom_id(entity, :show)%>">
       <%= link_to 'Show Entity', entity -%></td>
    <td id="<%=dom_id(entity, :edit)%>">
       <%= link_to 'Edit Entity', edit_entity_path(entity) -%></td>
    <td id="<%=dom_id(entity, :delete)%>">
       <%= link_to 'Destroy Entity', entity,
                    :confirm => 'Are you sure?',
                    :method => :delete -%></td>
  </tr>

Now, when I check for either of the selector ids using webrat's within
method they both work.

  within("tr#"+dom_id(my_entity, :list)) do
    puts "within method executed for tr#dom_id selector"
  end
  within("td#"+dom_id(my_entity, :delete)) do
    puts "within method executed for td#dom_id selector"
  end

produces:

within method executed for tr#dom_id selector
within method executed for td#dom_id selector

But, any call to the click_link("Destroy Entity") method from a
successful within match gives a nil object exception.  So, what is
happening here?
This topic is locked and can not be replied to.