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:
Access to session and request is not possible from the test ...
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
$ gem install rack_session_access
See README for usage examples.
Rails + Rspec + Capybara example
Add to spec/spec_helper.rb:
And use set_rack_session helper in acceptance test:
feature "My feature" do
@user = Factory(:user)
scenario "logged in user goes to profile page" do
# read your authorization engine manual how it store user into session
page.set_rack_session(:user_id => @user.id)
Enjoy faster testing!
Most recent posts
- Responsive layout tests with Capybara and RSpec
- SQL Joins Visualizer – build SQL JOIN between two tables by using Venn diagrams
- Flexbox as a solution for browser reflow optimization
- Make AngularJS 1.0.7 work with Jasmine 2.0
- Git housekeeping tutorial: clean-up outdated branches in local and remote repositories
Most popular posts
- API with Ruby on Rails: useful tricks 47,783 views
- Creation of pure Swift module 20,544 views
- PostgreSQL most useful extensions 19,489 views
- Advanced server definitions in Capistrano 18,364 views
- Git housekeeping tutorial: clean-up outdated branches in local and remote repositories 16,061 views