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

11,393

Solution 1

Capybara automatically follows redirects. Something else is broken.

Solution 2

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.

Solution 3

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!

Share:
11,393
Simon Wallner
Author by

Simon Wallner

Updated on April 28, 2022

Comments

  • Simon Wallner
    Simon Wallner about 6 hours

    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

  • Simon Wallner
    Simon Wallner almost 12 years
    Thats the answer that I feared most, but I also heard that from another source. Need to investigate that...
  • Simon Wallner
    Simon Wallner almost 12 years
    d'oh! Something was wrong with my test data. No wonder it didn't work if the user didn't exist in the db. Thanks for the help!
  • Timo
    Timo almost 11 years
    It is Capybara that follows redirects. Rack::Test only follows redirect if you call follow_redirect!: rdoc.info/github/brynary/rack-test/master/Rack/Test/Session
  • ian
    ian over 9 years
    I downvoted this because Rack Test does not automatically follow redirects, and here is a comment saying that and yet the answer has not been updated.