Cucumber, finding a row in a table


#1

I generate the following html on the page in question

Just An Entity CORP 000001 Show Entity Edit Entity Destroy Entity

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?


#2

On Mon, Jan 5, 2009 at 4:18 PM, James B. removed_email_address@domain.invalid
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 D.
http://www.continuousthinking.com
http://www.mutuallyhuman.com


#3

Zach D. 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.


#4

On Tue, Jan 6, 2009 at 4:08 PM, James B. removed_email_address@domain.invalid
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 D.
http://www.continuousthinking.com
http://www.mutuallyhuman.com


#5

I am still not getting this to work. Here is what I have done:

In index.html.erb

... ...

<% for entity in @entities %>

... ...

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.

Short Name
<%=h entity.entity_name.titlecase -%><%= "%06d" % entity.id -%> <%= link_to 'Show Entity', entity -%> <%= link_to 'Edit Entity', edit_entity_path(entity) -%> <%= link_to 'Destroy Entity', entity, :confirm => 'Are you sure?', :method => :delete -%>

#6

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:

<%=h entity.entity_name.titlecase -%> <%=h entity.entity_legal_form -%> <%= "%06d" % entity.id -%> <%= link_to 'Show Entity', entity -%> <%= link_to 'Edit Entity', edit_entity_path(entity) -%> <%= link_to 'Destroy Entity', entity, :confirm => 'Are you sure?', :method => :delete -%>

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?