https://github.com/sixarm/sixarm_ruby_capybara_session_cookies
SixArm.com » Ruby » Capybara session cookies for Rails testing
https://github.com/sixarm/sixarm_ruby_capybara_session_cookies
capybara cookie ruby session
Last synced: over 1 year ago
JSON representation
SixArm.com » Ruby » Capybara session cookies for Rails testing
- Host: GitHub
- URL: https://github.com/sixarm/sixarm_ruby_capybara_session_cookies
- Owner: SixArm
- License: other
- Created: 2012-01-12T01:11:05.000Z (over 14 years ago)
- Default Branch: main
- Last Pushed: 2023-09-15T19:27:48.000Z (almost 3 years ago)
- Last Synced: 2025-02-06T00:25:43.197Z (over 1 year ago)
- Topics: capybara, cookie, ruby, session
- Language: Ruby
- Homepage: http://sixarm.com
- Size: 332 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# SixArm.com → Ruby →
Capybara session cookies for Rails testing
* Doc:
* Gem:
* Repo:
## Introduction
This gem wraps the code and writeup from Steve Richert at Collective Idea:
http://collectiveidea.com/blog/archives/2012/01/05/capybara-cucumber-and-how-the-cookie-crumbles/
All credit for this goes to Steve.
For docs go to
Want to help? We're happy to get pull requests.
## Install
### Gem
To install this gem in your shell or terminal:
gem install sixarm_ruby_capybara_session_cookies
### Gemfile
To add this gem to your Gemfile:
gem 'sixarm_ruby_capybara_session_cookies'
### Require
To require the gem in your code:
require 'sixarm_ruby_capybara_session_cookies'
## Details
From Steve's writeup...
When I write a new Rails application, it often needs some sort of user authentication.
I like to test authentication in Cucumber.
I want to set the set the signed-in user directly.
Here’s the current_user helper method in my ApplicationController:
def current_user
return @current_user if defined?(@current_user)
@current_user = cookies[:token] && User.find_by_token(cookies[:token])
end
Each Capybara driver handles its cookies differently. The cookies hash we access in our step is specific to Rack::Test and is actually a Rack::Test::CookieJar object.
If you want your application cookies to Just Work™ from anywhere in your Cucumber suite, throw the following into features/support/cookies.rb:
module Capybara
class Session
def cookies
@cookies ||= begin
secret = Rails.application.config.secret_token
cookies = ActionDispatch::Cookies::CookieJar.new(secret)
cookies.stub(:close!)
cookies
end
end
end
end
Before do
request = ActionDispatch::Request.any_instance
request.stub(:cookie_jar).and_return{ page.cookies }
request.stub(:cookies).and_return{ page.cookies }
end
You’ll need a stubbing library. I’m using RSpec.
This allows each of your Capybara sessions to keep its own separate set of cookies. And they’re real cookies, meaning that you can use cookies.permananent and cookies.signed just like you do in your controllers. Then, after each scenario, Capybara will clean its sessions, along with your cookies.
Just use page.cookies and you’re good to go!
When /^I am signed in as "([^"]*)"$/ do |email|
page.cookies[:token] = User.find_by_email!(email).token
end