Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to follow a redirect after click_link/button with cucumber and capybara in rails?

I'm currently trying to set up integration/acceptance testing for a new rails 3 application with cucumber and capybara. (I initially planed to use webrat, but it seems that it does not support rails 3, so I ended up with capybara)

I'm trying to get a basic login test working:

Feature: Login user
  In order to access the non-public parts of the site,
  as a user,
  I want to login to the site

Scenario: login with valid credentials
  Given I am on the login page
  When I fill in "Email" with "[email protected]"
  And I fill in "Password" with "pass"
  And I press "Login"
  Then I should be on the users home page
  And I should see "Login successful"

The problem now is, that the login form sends me to /user_session which then redirects me to the users home /home. Cucumber does not follow the redirect which causes the Then I should be on the users home page line to fail.

How can I tell cucumber/capybara to follow the redirect so that I am on the right page after I hit a button of follow a link?

There seems to be a follow_redirect! method in the rack_test driver which I am using, but it is private and I have no clue as how to call that functionality.

thanks in advance,
Simon

like image 930
Simon Wallner Avatar asked Jul 23 '10 16:07

Simon Wallner


3 Answers

Capybara automatically follows redirects. Something else is broken.

like image 184
triskweline Avatar answered Nov 14 '22 21:11

triskweline


Add this into your steps:

When /^I dump.* the response$/ do
  puts body
end

Then, when your tests are misbehaving, you can add "And I dump the response" to your steps and see where it's going with the redirects or whatever.

like image 45
Ghoti Avatar answered Nov 14 '22 21:11

Ghoti


Something that might work is switching the order of the statements:

Then I should see "Login successful"
And I should be on the users home page

Then the check for the current page will happen after the check for the page text. Cucumber tests do require a lot of debugging, good luck!

like image 30
Karl Avatar answered Nov 14 '22 20:11

Karl