capybara-sessionkeeper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 479123ea32d6a302bf33982ee38f4d092fd60b9c
4
+ data.tar.gz: 8b40efcf2f17754d5958860d9f645949fe67da5d
5
+ SHA512:
6
+ metadata.gz: e780cecdc7817e5422aad08024224fa0660a1c112be88424ec8311bd11c8c7c573d3fdd6059033fc0f3c4a13cfda489152eb0734c7eb69ad0278d49e34f1b3e0
7
+ data.tar.gz: 8942a49e8e78597447031c5fdf53cbdba57ccd5ae9dec92809de23e8dbda4e1e3edd8f5fcf89fd17b1cd1ebd7817470b8bb4dfb51f5b06b654a038214d2d13ae
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ .byebug_history
15
+ /spec/tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,14 @@
1
+ dist: trusty
2
+ sudo: false
3
+ language: ruby
4
+ rvm:
5
+ - 2.4.1
6
+ before_install: gem install bundler -v 1.15.1
7
+ before_script:
8
+ - "export DISPLAY=:99.0"
9
+ - "sh -e /etc/init.d/xvfb start"
10
+ - CHROMEDRIVER_VERSION=$(curl -s http://chromedriver.storage.googleapis.com/LATEST_RELEASE)
11
+ - echo $CHROMEDRIVER_VERSION $PWD $PATH
12
+ - curl -L -O "http://chromedriver.storage.googleapis.com/${CHROMEDRIVER_VERSION}/chromedriver_linux64.zip"
13
+ - unzip chromedriver_linux64.zip -d chromedriver
14
+ - export PATH=$PATH:$PWD/chromedriver
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in capybara-sessionkeeper.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Kazuho Yamaguchi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,132 @@
1
+ # Capybara::Sessionkeeper
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/capybara-sessionkeeper.svg)](https://badge.fury.io/rb/capybara-sessionkeeper)
4
+ [![Build Status](https://travis-ci.org/kyamaguchi/capybara-sessionkeeper.svg?branch=master)](https://travis-ci.org/kyamaguchi/capybara-sessionkeeper)
5
+
6
+ Save and restore cookies of capybara session
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'capybara-sessionkeeper'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install capybara-sessionkeeper
23
+
24
+ ## Usage
25
+
26
+ Require with capybara
27
+
28
+ ```
29
+ require "capybara"
30
+ require "capybara/sessionkeeper"
31
+ ```
32
+
33
+ ### Supported drivers
34
+
35
+ [Recommended] Chrome driver is supported.
36
+
37
+ ```
38
+ Capybara.register_driver :chrome do |app|
39
+ Capybara::Selenium::Driver.new(app, browser: :chrome)
40
+ end
41
+ ```
42
+
43
+ Firefox(:selenium option) also works.
44
+
45
+ ```
46
+ session = Capybara::Session.new(:selenium)
47
+ ```
48
+
49
+ ### Location of cookie file
50
+
51
+ It follows `Capybara.save_path`.
52
+
53
+ ### Save cookies
54
+
55
+ ```
56
+ session = Capybara::Session.new(:chrome)
57
+ session.visit 'https://github.com/'
58
+ path = session.save_cookies
59
+ ```
60
+
61
+ Save cookie file with specified file name.
62
+
63
+ ```
64
+ session.save_cookies('user1.cookies.txt')
65
+ ```
66
+
67
+ ### Restore cookies
68
+
69
+ You have to visit the site which you are trying to restore cookie beforehand.
70
+ Otherwise, you will get error.
71
+
72
+ ```
73
+ session = Capybara::Session.new(:chrome)
74
+ session.visit 'https://github.com/'
75
+ cookies = session.restore_cookies
76
+ ```
77
+
78
+ Restore cookie file with specified file name.
79
+
80
+ ```
81
+ session.restore_cookies('user1.cookies.txt')
82
+ ```
83
+
84
+ ### Read cookies of current session
85
+
86
+ ```
87
+ session.driver.browser.manage.all_cookies
88
+ ```
89
+
90
+ ### Notice of cookie restoration
91
+
92
+ On restoring cookies, this gem ignores `Selenium::WebDriver::Error::InvalidCookieDomainError`.
93
+ If you repeat `save_cookies` and `restore_cookies` in a single file, you could lose some cookies of domains you haven't visited.
94
+
95
+ This behavior can be changed in the future.
96
+
97
+ Some use cases are,
98
+ Save/Restore cookies by users/use cases/sites.
99
+ You can switch signed-in users easily.
100
+ You don't need to sign in every time.
101
+ You just need to sign in once(or occasionally) and save/restore cookies.
102
+
103
+ ## Development
104
+
105
+ ### Testing spec which requires signin using envchain
106
+
107
+ There are some spec requiring GitHub signin.
108
+
109
+ You can store environment variables in macOS Keychain.
110
+ Check out [envchain](https://github.com/sorah/envchain)
111
+
112
+ ```
113
+ brew install envchain
114
+ envchain --set github GITHUB_USERNAME GITHUB_PASSWORD
115
+
116
+ envchain github rspec
117
+ ```
118
+
119
+ ### Testing with :selenium(Firefox) driver
120
+
121
+ ```
122
+ SELENIUM=true rspec
123
+ SELENIUM=true envchain github rspec
124
+ ```
125
+
126
+ ## Contributing
127
+
128
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kyamaguchi/capybara-sessionkeeper.
129
+
130
+ ## License
131
+
132
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "capybara/sessionkeeper"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "capybara/sessionkeeper/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "capybara-sessionkeeper"
8
+ spec.version = Capybara::Sessionkeeper::VERSION
9
+ spec.authors = ["Kazuho Yamaguchi"]
10
+ spec.email = ["kzh.yap@gmail.com"]
11
+
12
+ spec.summary = %q{Save and restore cookies of capybara session.}
13
+ spec.description = %q{Save and restore cookies of capybara session.}
14
+ spec.homepage = "https://github.com/kyamaguchi/capybara-sessionkeeper"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "capybara"
25
+ spec.add_dependency "selenium-webdriver"
26
+ spec.add_development_dependency "bundler", "~> 1.15"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "byebug"
30
+ end
@@ -0,0 +1,55 @@
1
+ require 'capybara'
2
+ require "capybara/sessionkeeper/version"
3
+
4
+ module Capybara
5
+ module Sessionkeeper
6
+ class CookieError < StandardError; end
7
+
8
+ def save_cookies(path = nil)
9
+ path = prepare_path(path, cookie_file_extension)
10
+ data = Marshal.dump driver.browser.manage.all_cookies
11
+ File.write(path, data)
12
+ path
13
+ end
14
+
15
+ def restore_cookies(path = nil)
16
+ raise CookieError, "visit must be performed to restore cookies" if driver.browser.manage.all_cookies.empty?
17
+ path ||= find_latest_cookie_file
18
+ return nil if path.nil?
19
+ data = File.read(path)
20
+ Marshal.load(data).each do |d|
21
+ begin
22
+ driver.browser.manage.add_cookie d
23
+ rescue => e
24
+ skip_invalid_cookie_domain_error(e)
25
+ end
26
+ end
27
+ driver.browser.manage.all_cookies
28
+ end
29
+
30
+ def cookie_file_extension
31
+ 'cookies.txt'
32
+ end
33
+
34
+ private
35
+
36
+ def find_latest_cookie_file
37
+ Dir.glob(File.join(Capybara.save_path, "*.#{cookie_file_extension}")).max_by{|f| File.mtime(f) }
38
+ end
39
+
40
+ def skip_invalid_cookie_domain_error(e)
41
+ if e.message =~ /InvalidCookieDomainError/
42
+ # Case of :selenium driver(Firefox). e.message -> "ReferenceError: InvalidCookieDomainError is not defined"
43
+ # Selenium::WebDriver::Error::UnknownError: ReferenceError: InvalidCookieDomainError is not defined
44
+ elsif e.message =~ /invalid cookie domain/
45
+ # Case of :chrome driver. e.message -> 'invalid cookie domain: invalid domain:".github.com"'
46
+ # Selenium::WebDriver::Error::InvalidCookieDomainError
47
+ # puts "Skipped invalid cookie domain: #{d[:domain]} - #{d.inspect}"
48
+ else
49
+ raise(e)
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ Capybara::Session.send(:include, Capybara::Sessionkeeper)
@@ -0,0 +1,5 @@
1
+ module Capybara
2
+ module Sessionkeeper
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capybara-sessionkeeper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kazuho Yamaguchi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capybara
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: selenium-webdriver
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.15'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Save and restore cookies of capybara session.
98
+ email:
99
+ - kzh.yap@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - capybara-sessionkeeper.gemspec
114
+ - lib/capybara/sessionkeeper.rb
115
+ - lib/capybara/sessionkeeper/version.rb
116
+ homepage: https://github.com/kyamaguchi/capybara-sessionkeeper
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.4.8
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Save and restore cookies of capybara session.
140
+ test_files: []