{"id":1200,"date":"2012-01-07T14:00:57","date_gmt":"2012-01-07T12:00:57","guid":{"rendered":"http:\/\/blog.railsware.com\/?p=1200"},"modified":"2021-08-12T12:56:27","modified_gmt":"2021-08-12T09:56:27","slug":"accessing-application-session-in-capybara","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/accessing-application-session-in-capybara\/","title":{"rendered":"Accessing application session in Capybara"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Trouble<\/h2>\n\n\n\n<p>There are some cases when you may need to access application session in your tests:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>User signup or registration flow is too long.<\/li><li>Application use another backend and store result into session.<\/li><\/ul>\n\n\n\n<p>In each story you must repeat the same steps. You can actually use some shared code but it does not speed-up tests anyway.<\/p>\n\n\n\n<p>Nowadays if you want to access session in cucumber\/capybara acceptance test you are in trouble.<\/p>\n\n\n\n<p>Capybara&#8217; <a href=\"https:\/\/github.com\/jnicklas\/capybara\/blob\/master\/README.md\">README<\/a> says:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  Access to session and request is not possible from the test ...\n<\/pre>\n\n\n\n<p>And it&#8217;s true :)<br><\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Rescue<\/h2>\n\n\n\n<p>So is it actually possible to access session? Actually yes :)<\/p>\n\n\n\n<p>Instead of trying to hack capybara you may just extend your application in test environment!<br>Just add some code that modify session according to given request parameters.<\/p>\n\n\n\n<p>If you use rack based application then your are lucky &#8211;<br>try <a href=\"https:\/\/github.com\/railsware\/rack_session_access\">rack_session_access<\/a> gem.<\/p>\n\n\n\n<p>If it&#8217;s another application you need implement concept yourself :)<\/p>\n\n\n\n<p>This gem should work with any rack application. We covered it with acceptance testing against:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>rack builder application<\/li><li>sinatra application<\/li><li>rails3 application<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Usage<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">  $ gem install rack_session_access\n<\/pre>\n\n\n\n<p>See <a href=\"https:\/\/github.com\/railsware\/rack_session_access\/blob\/master\/README.md\">README<\/a> for usage examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Rails + Rspec + Capybara example<\/h2>\n\n\n\n<p>Add to <em>spec\/spec_helper.rb<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">require 'capybara\/rspec'\nrequire 'rack_session_access\/capybara'\n\nRails.application.config do\n  config.middleware.use RackSessionAccess::Middleware\nend\n<\/pre>\n\n\n\n<p>And use <strong>set_rack_session<\/strong> helper in acceptance test:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">require 'spec_helper'\n\nfeature \"My feature\" do\n  background do\n    @user = Factory(:user)\n  end\n  scenario \"logged in user goes to profile page\" do\n    # read your authorization engine manual how it store user into session\n    page.set_rack_session(:user_id =&gt; @user.id)\n    page.visit '\/profile'\n    ...\n  end\nend\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Enjoy faster testing!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/github.com\/jnicklas\/capybara\">capybara<\/a><\/li><li><a href=\"https:\/\/github.com\/railsware\/rack_session_access\">rack_session_access<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Trouble 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&#8230;.<\/p>\n","protected":false},"author":19,"featured_media":9443,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["Andriy Yanko"],"class_list":["post-1200","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"https:\/\/railsware.com\/blog\/wp-content\/themes\/railsware\/vendors\/images\/article-thumbnail-default.jpg","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/1200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=1200"}],"version-history":[{"count":16,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/1200\/revisions"}],"predecessor-version":[{"id":13954,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/1200\/revisions\/13954"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/9443"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=1200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=1200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=1200"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=1200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}