visit our site

Accessing application session in capybara

In Random

by Andriy Yanko by January 7, 2012


There are some cases when you may need to access application session in your tests:

  • User signup or registration flow is too long.
  • Application use another backend and store result into session.

In each story you must repeat the same steps. You can actually use some shared code but it does not speed-up tests anyway.

Nowadays if you want to access session in cucumber/capybara acceptance test you are in trouble.

Capybara’ README says:

And it’s true :)


So is it actually possible to access session? Actually yes :)

Instead of trying to hack capybara you may just extend your application in test environment!
Just add some code that modify session according to given request parameters.

If you use rack based application then your are lucky –
try rack_session_access gem.

If it’s another application you need implement concept yourself :)

This gem should work with any rack application. We covered it with acceptance testing against:

  • rack builder application
  • sinatra application
  • rails3 application


See README for usage examples.

Rails + Rspec + Capybara example

Add to spec/spec_helper.rb:

And use set_rack_session helper in acceptance test:


Enjoy faster testing!


* Railsware is a premium software development consulting company, focused on delivering great web and mobile applications. Learn more about us.
  • Peter

    Thank you! This was very helpful. 

  • Qwe

    Awesome — thanks.  Not being able to access the session hash was preventing me from attaining 100% line coverage (yeah, it’s probably a waste of time to strive for 100%, but whatever.  It makes me feel better.)

  • Saint

    Very useful. Thanks!

  • Why did they do away with access? Does this mean ‘we’re doing it wrong?’

    • Ben Sheldon

      Yes. The rationale was that these are integration tests, which means they should be written and driven from the perspective of the user. I’m sympathetic to the issue of speed, but I think it comes at the risk of not having true integration tests. I tend to have controller specs that test this kind of behavior (from the back-end perspective), and then a few really long integration tests that walk through the entire experience of a logged-in user’s workflow.

  • Pingback: Faking authentication for capybara tests within a Rails engine - SkyOffice Consulting | SkyOffice Consulting()

  • Pedro

    On rails 4 you need to put config.middleware.use RackSessionAccess::Middleware on test.rb in case someone needs it.

    • fancy

      Thank U very much!!!!!

    • Dmitry

      I registered just for this message. Thank you!

  • Blakeley

    Unfortunately this has the pretty awful side effect of visiting a new page every single time you set or get the page!

  • solostyle

    This was very helpful for me! Thank you!

  • In tests that use the RackTest driver, you can get to the session data directly via page.driver.request.session. Session options, such as the session ID, can be accessed via page.driver.request.session_options. No need for an extra gem.

    • ayanko

      It is possible only for RackTest driver. This gems provides interface for all drivers.

Signup for our weekly newsletter

Want to get more of Railsware blog?

RSS Feed

We're always ready to help!

Contact us