DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Cucumber Useful Step Definition (based On WebRat)
In this tutorial (http://www.ultrasaurus.com/sarahblog/2008/12/rails-2-day-4-rcov-and-more-behavior-driven-development/), you can learn how to do an action to a specific element in a table, finding the row where it is.
Well, I improved the technique so you can press a button, follow a link, choose a radio button, or check/uncheck a checkbox within the row describing a particular element, finding it by any of its attributes (id, name, caption, ...).
This code is assuming that the "trs" of your tables are made like this : <tr id="row_product-or-wathever_the-id">
I haven't used the dom_id thing because I can't get it to work, but if it works for you it's no big deal to change my code a bit.
And of course, you can adapt your within css selector to your needs.
And well, ok, this won't work if you can find more than 1 element matching the value for a given attribute
When /^I (press|follow|check|uncheck|choose) "([^\"]*)" for (.*) whose (.*) is "([^\"]*)"$/ do |action, whatyouclick, class_name, var_name, value|
unless var_name == "id" then
id = eval("\"#{class_name}\".classify.constantize.find_by_#{var_name}(\"#{value}\").id.to_s")
else
id = value
end
within("tr[id=row_#{class_name}_#{id}]") do
case action
when "press"
click_button(whatyouclick)
when "follow"
click_link(whatyouclick)
when "check"
check(whatyouclick)
when "uncheck"
uncheck(whatyouclick)
when "choose"
uncheck(whatyouclick)
end
end
end
Use :
When I go to the products page And I press "Delete" for product whose name is "An awesome product" Then I should see "Product An awesome product deleted"





