capybara-box 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +3 -0
  3. data/LICENSE +21 -0
  4. data/README.md +126 -0
  5. data/lib/capybara-box.rb +6 -0
  6. data/lib/capybara-box/base.rb +226 -0
  7. data/lib/capybara-box/version.rb +5 -0
  8. data/spec/lib/capybara_box/base/add_argument_spec.rb +35 -0
  9. data/spec/lib/capybara_box/base/add_preference_spec.rb +35 -0
  10. data/spec/lib/capybara_box/base/apply_arguments_spec.rb +33 -0
  11. data/spec/lib/capybara_box/base/apply_bin_path_spec.rb +34 -0
  12. data/spec/lib/capybara_box/base/apply_preferences_spec.rb +43 -0
  13. data/spec/lib/capybara_box/base/apply_version_spec.rb +35 -0
  14. data/spec/lib/capybara_box/base/arguments_spec.rb +72 -0
  15. data/spec/lib/capybara_box/base/browser_spec.rb +27 -0
  16. data/spec/lib/capybara_box/base/chrome_family_spec.rb +23 -0
  17. data/spec/lib/capybara_box/base/chrome_headless_spec.rb +17 -0
  18. data/spec/lib/capybara_box/base/chrome_spec.rb +17 -0
  19. data/spec/lib/capybara_box/base/configure_screenshot_spec.rb +83 -0
  20. data/spec/lib/capybara_box/base/configure_spec.rb +33 -0
  21. data/spec/lib/capybara_box/base/create_spec.rb +77 -0
  22. data/spec/lib/capybara_box/base/driver_options_spec.rb +23 -0
  23. data/spec/lib/capybara_box/base/driver_spec.rb +87 -0
  24. data/spec/lib/capybara_box/base/firefox_spec.rb +17 -0
  25. data/spec/lib/capybara_box/base/http_client_options_spec.rb +24 -0
  26. data/spec/lib/capybara_box/base/http_client_spec.rb +11 -0
  27. data/spec/lib/capybara_box/base/options_spec.rb +43 -0
  28. data/spec/lib/capybara_box/base/preferences_spec.rb +63 -0
  29. data/spec/lib/capybara_box/base/register_spec.rb +17 -0
  30. data/spec/lib/capybara_box/base/self_configure_spec.rb +22 -0
  31. data/spec/lib/capybara_box/base/version_spec.rb +33 -0
  32. data/spec/rails_helper.rb +9 -0
  33. data/spec/support/common.rb +11 -0
  34. metadata +227 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a3c837ec541010303e4e84c3e919583b4a4f72c5
4
+ data.tar.gz: dffeb421a4aaeaa05878c54074dcc2c362044c8c
5
+ SHA512:
6
+ metadata.gz: c436a08f788a378584be91ecd5e7103ed978983ab1a284ccf9c583124258509d309f85f943a68849121e37bf07054d0159766c49b42f07bd3d75d5fdec3b77a7
7
+ data.tar.gz: 804393024ebd9c80e67e54574b049f6dcca5a5368c8688dc1085102200d5869d937565ec13b297fddc0859866d5561989cc8b81b661d373c8cbaa80bd84ced29
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.1.0
2
+
3
+ First release.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Washington Botelho
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.
data/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # Capybara Box
2
+
3
+ [![Build Status](https://travis-ci.org/wbotelhos/capybara-box.svg)](https://travis-ci.org/wbotelhos/capybara-box)
4
+ [![Gem Version](https://badge.fury.io/rb/capybara-box.svg)](https://badge.fury.io/rb/capybara-box)
5
+
6
+ Configure Capybara with **Chrome**, **Headless Chrome** or **Firefox** with *Screenshot* feature and *Session* without lost your mind with just one line.
7
+
8
+ ## install
9
+
10
+ Add the following code on your Gemfile and run bundle install:
11
+
12
+ ```
13
+ group :test do
14
+ gem 'capybara-box', require: false
15
+ end
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ Just require the lib **after** Capybara require and you done:
21
+
22
+ ```
23
+ require 'capybara/rails'
24
+ require 'capybara-box'
25
+
26
+ CapybaraBox::Base.configure
27
+ ```
28
+
29
+ By default, `chrome` is de driver, but you can use `chrome`, `chrome_headless` or `firefox`.
30
+
31
+ ## Screenshot
32
+
33
+ You can enable screenshot on failure and send it to S3.
34
+
35
+ ```
36
+ CapybaraBox.configure(
37
+ screenshot: {
38
+ s3: {
39
+ access_key_id: 'KEY',
40
+ bucket: 'bucket',
41
+ secret_access_key: 'SECRET'
42
+ }
43
+ }
44
+ )
45
+ ```
46
+
47
+ If you want enable it only on CI, use the `enabled` option:
48
+
49
+ ```
50
+ CapybaraBox.configure(
51
+ enabled: ENV['CI'],
52
+
53
+ screenshot: { // ... }
54
+ )
55
+ ```
56
+
57
+ ## Session
58
+
59
+ By default, Rack Session manipulation comes as battery, just use it.
60
+
61
+ ```
62
+ page.set_rack_session key: 'value'
63
+ ```
64
+
65
+ ```
66
+ page.get_rack_session :key
67
+ # 'value'
68
+ ```
69
+
70
+ You can disable this feature using the `session` option:
71
+
72
+ ```
73
+ CapybaraBox.configure browser: :chrome, session: false
74
+ ```
75
+
76
+ ## Add Argument
77
+
78
+ By default some Switches are enabled for a better performance, you can add yours too:
79
+
80
+ ```
81
+ capybara_box = CapybaraBox.configure
82
+ capybara_box.add_argument '--incognito'
83
+ ```
84
+
85
+ ## Arguments
86
+
87
+ If you prefere, is possible override all of them:
88
+
89
+ ```
90
+ CapybaraBox.configure arguments: ['--incognito']
91
+ ```
92
+
93
+ Click [here](https://peter.sh/experiments/chromium-command-line-switches) to see the avaiables.
94
+
95
+ ## Add Preference
96
+
97
+ By default some Preferences are enabled for a better performance, you can add yours too:
98
+
99
+ ```
100
+ capybara_box = CapybaraBox.configure
101
+ capybara_box.add_preference :credentials_enable_service, false
102
+ ```
103
+
104
+ ## Preferences
105
+
106
+ If you prefere, is possible override all of them:
107
+
108
+ ```
109
+ CapybaraBox.configure preferences: { credentials_enable_service: false }
110
+ ```
111
+
112
+ You can check [Chrome](https://sites.google.com/a/chromium.org/chromedriver/home) and [Firefox](http://preferential.mozdev.org/preferences.html).
113
+
114
+ ## HTTP Client Options
115
+
116
+ By default some timeout configs are enabled only on CI env for a better performance.
117
+ It has this restrition because with timeout enabled, debugger cannot evaluate the variables values.
118
+ You can override all of them too:
119
+
120
+ ```
121
+ CapybaraBox.configure http_client_options: { read_timeout: 60 }
122
+ ```
123
+
124
+ ## Love it!
125
+
126
+ Via [PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=X8HEP2878NDEG&item_name=capybara-box) or [Gratipay](https://gratipay.com/~wbotelhos). Thanks! (:
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CapybaraBox
4
+ end
5
+
6
+ require 'capybara-box/base'
@@ -0,0 +1,226 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CapybaraBox
4
+ require 'selenium/webdriver'
5
+
6
+ class Base
7
+ def initialize(parameters = {})
8
+ @browser = parameters[:browser] || :chrome
9
+ @max_wait_time = parameters[:max_wait_time]
10
+ @parameters = parameters
11
+
12
+ requires
13
+ end
14
+
15
+ def add_argument(value)
16
+ options.add_argument value unless options.nil?
17
+ end
18
+
19
+ def add_preference(key, value)
20
+ options.add_preference key, value unless options.nil?
21
+ end
22
+
23
+ def apply_arguments
24
+ arguments.each { |argument| add_argument argument }
25
+
26
+ add_argument '--headless' if chrome_headless?
27
+ end
28
+
29
+ def apply_preferences
30
+ preferences.each { |key, value| add_preference key, value }
31
+ end
32
+
33
+ def apply_version(version)
34
+ if chrome_family?
35
+ require 'chromedriver/helper'
36
+
37
+ ::Chromedriver.set_version version
38
+ end
39
+ end
40
+
41
+ def arguments
42
+ return @parameters[:arguments] if @parameters[:arguments]
43
+ return [] unless chrome_family?
44
+
45
+ %w[
46
+ --disable-default-apps
47
+ --disable-extensions
48
+ --disable-infobars
49
+ --disable-notifications
50
+ --disable-password-generation
51
+ --disable-password-manager-reauthentication
52
+ --disable-password-separated-signin-flow
53
+ --disable-popup-blocking
54
+ --disable-save-password-bubble
55
+ --disable-translate
56
+ --incognito
57
+ --mute-audio
58
+ --no-default-browser-check
59
+ --start-fullscreen
60
+ ]
61
+ end
62
+
63
+ def browser
64
+ @browser ||= chrome_family? ? :chrome : @browser
65
+ end
66
+
67
+ def chrome?
68
+ @browser == :chrome
69
+ end
70
+
71
+ def chrome_family?
72
+ chrome? || chrome_headless?
73
+ end
74
+
75
+ def chrome_headless?
76
+ @browser == :chrome_headless
77
+ end
78
+
79
+ def configure
80
+ Capybara.javascript_driver = browser
81
+ Capybara.default_max_wait_time = @max_wait_time if @max_wait_time
82
+ end
83
+
84
+ def apply_bin_path(path)
85
+ if firefox?
86
+ Selenium::WebDriver::Firefox::Binary.path = path
87
+
88
+ Selenium::WebDriver::Firefox::Binary.path
89
+ end
90
+ end
91
+
92
+ def configure_screenshot
93
+ return false if @parameters[:screenshot].key?(:enabled) && !true?(@parameters[:screenshot][:enabled])
94
+
95
+ require 'capybara-screenshot/rspec'
96
+
97
+ Capybara::Screenshot.append_timestamp = false
98
+ Capybara::Screenshot.prune_strategy = :keep_last_run
99
+ Capybara::Screenshot::RSpec.add_link_to_screenshot_for_failed_examples = false
100
+
101
+ Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example|
102
+ example.full_description.downcase.gsub('#', '--').tr ' ', '-'
103
+ end
104
+
105
+ if @parameters[:screenshot][:s3]
106
+ Capybara::Screenshot.s3_configuration = screenshot_s3_config
107
+ end
108
+
109
+ Capybara::Screenshot.register_driver(browser) do |driver, path|
110
+ driver.browser.save_screenshot path
111
+ end
112
+ end
113
+
114
+ def create
115
+ apply_arguments
116
+ apply_preferences
117
+
118
+ apply_bin_path @parameters[:bin_path] if @parameters[:bin_path]
119
+ apply_version @parameters[:version] if version?
120
+ configure_screenshot if @parameters[:screenshot]
121
+
122
+ register browser
123
+
124
+ configure
125
+ end
126
+
127
+ def driver(app)
128
+ opts = {}
129
+ opts[:options] = options if chrome_family?
130
+ opts[:http_client] = http_client if true?(ENV['CI'])
131
+
132
+ Capybara::Selenium::Driver.new app, opts.merge(driver_options)
133
+ end
134
+
135
+ def driver_options
136
+ return @parameters[:driver_options] if @parameters[:driver_options]
137
+
138
+ {
139
+ browser: browser,
140
+ clear_local_storage: true,
141
+ clear_session_storage: true
142
+ }
143
+ end
144
+
145
+ def firefox?
146
+ @browser == :firefox
147
+ end
148
+
149
+ def http_client_options
150
+ return @parameters[:http_client_options] if @parameters[:http_client_options]
151
+
152
+ {
153
+ open_timeout: nil,
154
+ read_timeout: 120
155
+ }
156
+ end
157
+
158
+ def http_client
159
+ @http_client ||= Selenium::WebDriver::Remote::Http::Default.new(http_client_options)
160
+ end
161
+
162
+ def options
163
+ @options ||= Selenium::WebDriver::Chrome::Options.new if chrome_family?
164
+ end
165
+
166
+ def preferences
167
+ return @parameters[:preferences] if @parameters[:preferences]
168
+ return {} unless chrome_family?
169
+
170
+ {
171
+ credentials_enable_service: false,
172
+
173
+ profile: {
174
+ password_manager_enabled: false
175
+ }
176
+ }
177
+ end
178
+
179
+ def register(name)
180
+ Capybara.register_driver name do |app|
181
+ driver app
182
+ end
183
+ end
184
+
185
+ def version
186
+ if chrome_family?
187
+ require 'chromedriver/helper'
188
+
189
+ ::Chromedriver::Helper.new.current_version
190
+ end
191
+ end
192
+
193
+ def self.configure(parameters)
194
+ box = new(parameters)
195
+
196
+ box.create
197
+
198
+ box
199
+ end
200
+
201
+ private
202
+
203
+ def true?(value)
204
+ ['true', true].include? value
205
+ end
206
+
207
+ def requires
208
+ require 'rack_session_access/capybara' unless @parameters[:session] == false
209
+ end
210
+
211
+ def screenshot_s3_config
212
+ {
213
+ s3_client_credentials: {
214
+ access_key_id: @parameters[:screenshot][:s3][:access_key_id],
215
+ secret_access_key: @parameters[:screenshot][:s3][:secret_access_key]
216
+ },
217
+
218
+ bucket_name: @parameters[:screenshot][:s3][:bucket]
219
+ }
220
+ end
221
+
222
+ def version?
223
+ !!@parameters[:version]
224
+ end
225
+ end
226
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CapybaraBox
4
+ VERSION = '0.1.0'.freeze
5
+ end
@@ -0,0 +1,35 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe CapybaraBox::Base, '.add_argument' do
4
+ subject { described_class.new parameters }
5
+
6
+ context 'when is chrome' do
7
+ let!(:parameters) { { browser: :chrome } }
8
+
9
+ it 'adds the argument' do
10
+ subject.add_argument '--argument'
11
+
12
+ expect(subject.options.args).to eq ['--argument']
13
+ end
14
+ end
15
+
16
+ context 'when is chrome headless' do
17
+ let!(:parameters) { { browser: :chrome_headless } }
18
+
19
+ it 'adds the argument' do
20
+ subject.add_argument '--argument'
21
+
22
+ expect(subject.options.args).to eq ['--argument']
23
+ end
24
+ end
25
+
26
+ context 'when is firefox' do
27
+ let!(:parameters) { { browser: :firefox } }
28
+
29
+ it 'does not applies options' do
30
+ subject.add_argument '--argument'
31
+
32
+ expect(subject.options).to be_nil
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe CapybaraBox::Base, '.add_preference' do
4
+ subject { described_class.new parameters }
5
+
6
+ context 'when is chrome' do
7
+ let!(:parameters) { { browser: :chrome } }
8
+
9
+ it 'adds the argument' do
10
+ subject.add_preference :key, :value
11
+
12
+ expect(subject.options.prefs).to eq(key: :value)
13
+ end
14
+ end
15
+
16
+ context 'when is chrome headless' do
17
+ let!(:parameters) { { browser: :chrome_headless } }
18
+
19
+ it 'adds the argument' do
20
+ subject.add_preference :key, :value
21
+
22
+ expect(subject.options.prefs).to eq(key: :value)
23
+ end
24
+ end
25
+
26
+ context 'when is firefox' do
27
+ let!(:parameters) { { browser: :firefox } }
28
+
29
+ it 'does not applies options' do
30
+ subject.add_preference :key, :value
31
+
32
+ expect(subject.options).to be_nil
33
+ end
34
+ end
35
+ end