[rspec-users] cucumber, finding a row in a table

Zach Dennis zach.dennis at gmail.com
Mon Jan 5 18:07:13 EST 2009

On Mon, Jan 5, 2009 at 4:18 PM, James Byrne <lists at ruby-forum.com> wrote:
> 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?

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

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

More information about the rspec-users mailing list