Last active
July 6, 2016 19:42
-
-
Save andreypronin/5058959 to your computer and use it in GitHub Desktop.
A little spec library I use to test session/persistent cookies behavior in Rails apps with Capybara. Plus, example on how I use it. Implemented for Rack::Test and Poltergeist (PhantomJS) drivers only, as these are my default test preferences.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
feature "Login" do | |
scenario "remembered user" do | |
password = "12345678" | |
user = create(:user, password: password) | |
access_protected_page_should_ask_to_login | |
current_path.should eq login_path | |
login_should_succeed user.email, password, true | |
imitate_new_browser_session # Note the use of function from my cookie-testing mini-library | |
access_protected_page_should_not_ask_to_login | |
current_path.should eq protected_page_path | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In your spec files you can call: | |
* copy_cookies [:to => session2,] [:from => session1,] [:only => cookies_type] # copy cookies of type :persistent|:session|:all from session1 to session2 | |
* clear_cookies [:in => session,] [:only => cookies_type] # clear all cookies of a given type in a given session | |
* cookie_value "name", [:in => session] # get cookie value | |
* clear_cookie "name", [:in => session] # clear specific cookie | |
* all_cookies [:in => session] # get all cookies | |
* imitate_new_browser_session [:only => cookies_type] # clear cookies of specified type in this session, by default only session cookies are cleared, and persistent are preserved, as in real life | |
* start_new_session [:name=>"session_name",] [:from=>session] [ { yield } ] # create new Capybara session (with a given name), copy persistent cookies there from the specified session (current by default), and execute a block in it if block given | |
In all cases if session is omitted, the current session is taken, if cookies_type is omitted, :all is assumed (unless stated otherwise). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment