Posted by Lena Herrmann Sun, 25 Oct 2009 21:53:00 GMT

In every project I use cucumber (that means, always) there are a few things that I keep reinventing, or I spend a lot of time searching for them in other projects. Here are some of them. They aren't all written by me, but maybe my coworkers like to use this as a reference, too?

When you upgrade to the current cucumber (0.4.2) from an older version some steps will fail, because cucumber doesn't examine the whole html anymore. You can still check the raw html with these steps:

Then /^I should see "([^\"]*)" in the source$/ do |text|
  response.body.should match(/#{text}/)
end

Then /^I should not see "([^\"]*)" in the source$/ do |text|
  response.body.should_not match(/#{text}/)
end

Want to test sorting in one step?

Then /^I should see "([^\"]*)" before "([^\"]*)"$/ do |first, second|
  response.body.should match(/#{first}.*#{second}/im)
end

Sometimes you want to click a link in a specific div:

When /^I buy "([^\"]*)"$/ do |title|
  product = Product.find_by_title(title)
  field_by_xpath("//div[@id='buy_product_dialog_product_#{product.id}']//input").click
end

Or you want to test what happens when you delete a specific object:

When /^I press "Delete" for comment "([^\"]*)"$/ do |comment|
  tag = Comment.find_by_title(comment)
  field_by_xpath("//form[@id='delete_comment_#{comment.id}']//input[@type='submit']").click
end

With this you can check for a string that's within a tag with a specific id:

Then /^I should see "([^\"]+)" in "([^\"]+)"$/ do |content, css_id|
  response.body.should have_tag("##{css_id}", content)
end

Use this regex to check for something within a given tag:

Then /^I should see "([^\"]*)" within a (\w+) tag$/ do |text, tag|
  response.body.should match(/<#{tag}[^>]*>[^<]*#{text}[^<]*<\/#{tag}>/)
end

Cucumber 0.4.2 auto-generates webrat_steps.rb including a step to save the current page and open it in the browser. Just in case not everyone knows this step by now:

Then /^show me the page$/ do
  save_and_open_page
end

no comments |

Trackbacks

Use the following link to trackback from your own site:
http://lenaherrmann.net/trackbacks?article_id=3

Leave a comment

Leave a comment