I just spent the better part of 8 hours trying to figure out how to do this. The Selenium WebDriver API docs are pretty bewildering especially when it comes to trying to interact with the HTTP response or request itself because those constructs are abstracted quite a bit.
Then /^I should be on the (.*) page$/ do |page_name| object = instance_variable_get("@#{page_name}") page.current_path.should == send("#{page_name.downcase.gsub(' ','_')}_path", object) page.status_code.should == 200 end Then /^I should be redirected to the (.*) page$/ do |page_name| page.driver.request.env['HTTP_REFERER'].should_not be_nil page.driver.request.env['HTTP_REFERER'].should_not == page.current_url step %Q(I should be on the #{page_name} page) end
Which we can use in a Cucumber feature like so:
Given I am on the homepage When I click on the link to add a widget Then I should be able to complete the widget form And I should be redirected to the widget page And I should see a confirmation message And I should see the widget listed
Nice, but you shouldn't be testing that you're redirected — that's too much testing of implementation. Instead, you should simply use "then I should be on the widget page".