Code Monkey home page Code Monkey logo

capybara-sessionkeeper's Introduction

Capybara::Sessionkeeper

Gem Version Build Status

Save and restore cookies of capybara session

Installation

Add this line to your application's Gemfile:

gem 'capybara-sessionkeeper'

And then execute:

$ bundle

Or install it yourself as:

$ gem install capybara-sessionkeeper

Usage

Require with capybara

require "capybara"
require "capybara/sessionkeeper"

Supported drivers

[Recommended] Chrome driver is supported.
Install chromedriver

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(app, browser: :chrome)
end

Firefox(:selenium option) may not work.
Install geckodriver

session = Capybara::Session.new(:selenium)

session will be equivalent to page in the system test.

Location of cookie file

It follows Capybara.save_path.

Save cookies

session = Capybara::Session.new(:chrome)
session.visit 'https://github.com/'
path = session.save_cookies

Save cookie file with specified file name.

session.save_cookies('user1.cookies.txt')

Restore cookies

You have to visit the site which you are trying to restore cookie beforehand.
Otherwise, you will get error.

session = Capybara::Session.new(:chrome)
session.visit 'https://github.com/'
cookies = session.restore_cookies

Restore cookie file with specified file name.

session.restore_cookies(File.join(Capybara.save_path, 'user1.cookies.txt'))

Save and restore cookies from YAML

Save cookie to yaml string (serialization to yaml).

yaml_str = session.cookies_to_yaml

Restore cookie from yaml string (deserialization from yaml).

session.restore_cookies_from_data(yaml_str, format: 'yaml')

Read cookies of current session

session.driver.browser.manage.all_cookies

Notice of cookie restoration

On restoring cookies, this gem ignores Selenium::WebDriver::Error::InvalidCookieDomainError.
If you repeat save_cookies and restore_cookies in a single file, you could lose some cookies of domains you haven't visited.

This behavior can be changed in the future.

Some use cases are,
Save/Restore cookies by users/use cases/sites.
You can switch signed-in users easily.
You don't need to sign in every time.
You just need to sign in once(or occasionally) and save/restore cookies.

Development

Testing with :selenium(Firefox) driver

SELENIUM=true rspec

Test app

Some spec uses test app https://github.com/kyamaguchi/testapp-capybara-sessionkeeper

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/kyamaguchi/capybara-sessionkeeper.

License

The gem is available as open source under the terms of the MIT License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.