blah blah woof woof

Cucumber steps for testing page URLs and redirects

Tim Riley 2009.03.04

With the new project at work, we’re making sure we follow the best practices we know. This means building the app in weekly iterations according to user stories that we review and schedule with the business owners. It also means being strict about writing tests first, and so far we’re doing pretty well.

Pepsi Ice Cucumber

We’re using the shiny new Cucumber as much as possible for our high-level integration testing. Along with Cucumber’s webrat steps, it is easy to write features that, among other things, can request pages, fill in forms, and check for text on the returned pages. When we add in Ian White’s pickle, we also have integration with Rails’ named routes to keep your features readable and steps DRY.

We often use this one webrat step for visiting a page:

When /^I go to (.+)$/ do |page_name|
  visit path_to(page_name)
end

This is the only step that webrat provides relating to pages and paths. We found that we needed a couple more:

Then /^I should be on the (.+?) page$/ do |page_name|
  request.request_uri.should == send("#{page_name.downcase.gsub(' ','_')}_path")
  response.should be_success
end

Then /^I should be redirected to the (.+?) page$/ do |page_name|
  request.headers['HTTP_REFERER'].should_not be_nil
  request.headers['HTTP_REFERER'].should_not == request.request_uri

  Then "I should be on the #{page_name} page"
end

With these steps, we can properly check the URL of the page we’ve been returned, and whether or not we have been redirected during that request cycle. This was important for us to have the fullest coverage possible for our authorization features:

Feature: Users cannot access to the system without logging in
  In order to protect the system from unauthorized access
  An anonymous user
  Should not have access to the system

  Scenario: Visiting the login page
    Given an anonymous user
    When I go to the new login page
    Then I should be on the new login page

  Scenario: Redirecting to login page
    Given an anonymous user
    When I go to the home page
    Then I should be redirected to the new login page

The first scenario ensures that we don’t end up in a redirect loop for an anonymous user directly visiting the login page, and the second scenario ensures that an anonymous user accessing any page in the system is appropriately blocked from access and redirected to the login page.

_Image courtesy of Bruce the Moose

Want More?

Previous Article

  1. 2009.02.25 Handy Shell Alias for Restarting your Rack Apps

Next Article

  1. 2009.03.06 Adaptive script/console Shell Alias for both Rails and Sinatra

Archived Comments

drnic 2009-03-04 12:26

Some nice generated steps for these would be lovely too; or webrat or rspec-rails helpers

timriley 2009-03-04 23:55

Thanks for the feedback, Dr Nic. I will investigate getting these merged upstream somewhere! Do you have any suggestions for possible improvements in the meantime?

mislav 2009-03-09 14:32

We use http://gist.github.com/76330

Is testing redirects really necessary? Seems to me like something that belongs to functional tests, not acceptance. You should be able to change a redirect without any impact to the user.

schlick 2009-03-11 05:30

Totally agree. It was what we found that worked at the time. Since then, we've been able to leverage pickle to remove the redirection and just verify the page we're at:
http://gist.github.com/77330