capybara-headless_chrome 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a764592d374ab41b59fd54042520ab3e7437eb4
4
- data.tar.gz: a39469d6e392e66383c8f7c61fd39f9d24945b47
3
+ metadata.gz: 072a6982915af40cdbd5b6c8141dd6cbb78e81c2
4
+ data.tar.gz: c9f75a5a769c27861f5f93775c6a11f3be0d1894
5
5
  SHA512:
6
- metadata.gz: 43b71aa6fd7a8caa1aaad2ae0048c959191477d3252912a0eef7c3172bf7b6d740940b42ffb52bcae19c1aaf33d054ffdf7d1d795fc55fc0dcf565d2bf8555cc
7
- data.tar.gz: a957183b4d92c42a9f649a6df5e77ebef6dc9ecceea454cbac2b0a37d5d54ce64ce55125f289d6ae972804aec66c02c2f55d40b6af84035beba040d92e8b40ba
6
+ metadata.gz: b3e174527bdf32088f5980f02aa8354dc3890b310575385ceea692bae5db45ab663605c719a5fa8469846d4cbb6ed0b5e6e58a0143ad22c882cfe7fa4f7b8521
7
+ data.tar.gz: e6b9b81bbdadec609b7b4bdb79ffd9c1b98b1538794c4d11b11d4d22b57995b1781a89f8f02bb350f0457ad2f1917a01e3424c48d0c3dbe5b0a5f1c22f4b8666
data/.gitignore CHANGED
@@ -9,3 +9,6 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ .ruby-version
13
+ .ruby-gemset
14
+ Gemfile.lock
data/README.md CHANGED
@@ -1,15 +1,37 @@
1
1
  # Capybara::HeadlessChrome
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/capybara/headless_chrome`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ A nice and tidy Capybara driver for headless Chrome. Even supports file downloads!
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ ## Usage
6
+
7
+ ### Capybara Setup
8
+
9
+ Just `require "capybara/headless_chrome"` somewhere in your test setup. This will register the `:chrome` driver, and make it Capybara's default.
10
+
11
+ ### Working with Downloaded Files
12
+
13
+ The Capybara session is extended with a single `#downloads` method that provides access to files downloaded during the session.
14
+
15
+ ```ruby
16
+ page.click_link "Download Report"
17
+ page.downloads.filenames # => ["report.csv"]
18
+ page.downloads["report.csv"] # => #<File:report.csv>
19
+ ```
20
+
21
+ Note that the `#[]` method is wrapped with Capybara's synchronize, so it will keep trying to find the file for up to `Capybara.default_max_wait_time` seconds.
22
+
23
+ Be sure to run `page.downloads.reset` at the beginning of every test run to empty the downloaded files list.
24
+
25
+ If you're using Cucumber, you can `require "capybara/headless_chrome/cucumber"` somewhere in your cucumber configuration to set this up for you.
6
26
 
7
27
  ## Installation
8
28
 
9
- Add this line to your application's Gemfile:
29
+ Add this to your application's Gemfile:
10
30
 
11
31
  ```ruby
12
- gem 'capybara-headless_chrome'
32
+ group :test do
33
+ gem 'capybara-headless_chrome'
34
+ end
13
35
  ```
14
36
 
15
37
  And then execute:
@@ -20,10 +42,6 @@ Or install it yourself as:
20
42
 
21
43
  $ gem install capybara-headless_chrome
22
44
 
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
45
  ## Development
28
46
 
29
47
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Micah Geisel"]
10
10
  spec.email = ["micah@botandrose.com"]
11
11
 
12
- spec.summary = "Wiring between Capybara and Headless Chrome"
13
- spec.description = "Wiring between Capybara and Headless Chrome"
12
+ spec.summary = "A nice and tidy Capybara driver for headless Chrome"
13
+ spec.description = "A nice and tidy Capybara driver for headless Chrome. Even supports file downloads!"
14
14
  spec.homepage = "https://github.com/botandrose/capybara-headless_chrome"
15
15
  spec.license = "MIT"
16
16
 
@@ -1,4 +1,4 @@
1
1
  Before do
2
- Capybara::HeadlessChrome::Downloads.setup
2
+ page.downloads.reset
3
3
  end
4
4
 
@@ -0,0 +1,44 @@
1
+ require "fileutils"
2
+ require "capybara"
3
+
4
+ module Capybara
5
+ module HeadlessChrome
6
+ class Downloads
7
+ class NotFound < Capybara::ExpectationNotMet; end
8
+
9
+ def dir
10
+ pathname.to_s
11
+ end
12
+
13
+ def reset
14
+ FileUtils.rm_rf(dir)
15
+ FileUtils.mkdir_p(dir)
16
+ end
17
+
18
+ def filenames
19
+ pathname.entries.reject(&:directory?).map(&:to_s)
20
+ end
21
+
22
+ def [] filename
23
+ Capybara.current_session.document.synchronize do
24
+ begin
25
+ File.open(pathname.join(filename))
26
+ rescue Errno::ENOENT
27
+ raise NotFound.new("Couldn't find #{filename} in #{filenames}")
28
+ end
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def pathname
35
+ @pathname ||= Capybara.save_path.join(unique_id, "downloads")
36
+ end
37
+
38
+ def unique_id
39
+ Time.now.strftime('%s%L')
40
+ end
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,66 @@
1
+ require "selenium/webdriver"
2
+ require "chromedriver/helper"
3
+ require "capybara"
4
+ require "capybara/headless_chrome/downloads"
5
+
6
+ module Capybara
7
+ module HeadlessChrome
8
+ class Driver < Capybara::Selenium::Driver
9
+ def initialize app
10
+ super(app, browser: :chrome, desired_capabilities: chrome_capabilities)
11
+ configure_downloads
12
+ end
13
+
14
+ def downloads
15
+ @downloads ||= Downloads.new
16
+ end
17
+
18
+ private
19
+
20
+ def chrome_capabilities
21
+ ::Selenium::WebDriver::Remote::Capabilities.chrome(
22
+ chromeOptions: {
23
+ args: chrome_arguments,
24
+ prefs: chrome_preferences
25
+ }
26
+ )
27
+ end
28
+
29
+ def chrome_arguments
30
+ [
31
+ "no-sandbox",
32
+ "headless",
33
+ "disable-infobars",
34
+ "disable-popup-blocking",
35
+ "disable-gpu",
36
+ "window-size=1280,1024",
37
+ ]
38
+ end
39
+
40
+ def chrome_preferences
41
+ {
42
+ "download.default_directory" => downloads.dir,
43
+ "download.directory_upgrade": "true",
44
+ "download.prompt_for_download": "false",
45
+ "browser.set_download_behavior": "{ behavior: 'allow' }",
46
+ }
47
+ end
48
+
49
+ def configure_downloads
50
+ bridge = browser.send(:bridge)
51
+ path = '/session/:session_id/chromium/send_command'
52
+ path[':session_id'] = bridge.session_id
53
+
54
+ bridge.http.call(:post,
55
+ path,
56
+ cmd: 'Page.setDownloadBehavior',
57
+ params: {
58
+ behavior: 'allow',
59
+ downloadPath: downloads.dir,
60
+ }
61
+ )
62
+ end
63
+ end
64
+ end
65
+ end
66
+
@@ -0,0 +1,10 @@
1
+ module Capybara
2
+ module HeadlessChrome
3
+ module SessionDSL
4
+ def downloads
5
+ driver.downloads
6
+ end
7
+ end
8
+ end
9
+ end
10
+
@@ -1,5 +1,5 @@
1
1
  module Capybara
2
2
  module HeadlessChrome
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -1,94 +1,12 @@
1
1
  require "capybara/headless_chrome/version"
2
- require "capybara"
3
- require "chromedriver/helper"
4
- require "selenium/webdriver"
5
-
6
- module Capybara
7
- module HeadlessChrome
8
- class DownloadNotFound < Capybara::ExpectationNotMet; end
9
-
10
- class Downloads
11
- class << self
12
- def setup
13
- FileUtils.rm_rf dir
14
- FileUtils.mkdir_p dir
15
- end
16
-
17
- def configure_driver driver
18
- bridge = driver.browser.send(:bridge)
19
- path = '/session/:session_id/chromium/send_command'
20
- path[':session_id'] = bridge.session_id
21
-
22
- bridge.http.call(:post,
23
- path,
24
- cmd: 'Page.setDownloadBehavior',
25
- params: {
26
- behavior: 'allow',
27
- downloadPath: dir,
28
- }
29
- )
30
- end
31
-
32
- def chrome_prefs
33
- {
34
- "download.default_directory" => dir,
35
- "download.directory_upgrade": "true",
36
- "download.prompt_for_download": "false",
37
- "browser.set_download_behavior": "{ behavior: 'allow' }",
38
- }
39
- end
40
-
41
- def dir
42
- pathname.to_s
43
- end
44
-
45
- def pathname
46
- Capybara.save_path.join("downloads")
47
- end
48
- end
49
-
50
- def filenames
51
- self.class.pathname.entries.reject(&:directory?).map(&:to_s)
52
- end
53
-
54
- def [] filename
55
- path = self.class.pathname.join(filename)
56
- Capybara.current_session.document.synchronize do
57
- begin
58
- File.open(path)
59
- rescue Errno::ENOENT
60
- raise DownloadNotFound.new("Couldn't find #{filename} in #{filenames}")
61
- end
62
- end
63
- end
64
- end
65
- end
66
- end
2
+ require "capybara/headless_chrome/driver"
3
+ require "capybara/headless_chrome/session_dsl"
67
4
 
68
5
  Capybara.register_driver :chrome do |app|
69
- capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
70
- chromeOptions: {
71
- args: [
72
- "no-sandbox",
73
- "headless",
74
- "disable-infobars",
75
- "disable-popup-blocking",
76
- "disable-gpu",
77
- "window-size=1280,1024",
78
- ],
79
- prefs: {}.merge(Capybara::HeadlessChrome::Downloads.chrome_prefs),
80
- }
81
- )
82
- Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: capabilities).tap do |driver|
83
- Capybara::HeadlessChrome::Downloads.configure_driver driver
84
- end
6
+ Capybara::HeadlessChrome::Driver.new(app)
85
7
  end
86
8
 
87
- Capybara.javascript_driver = :chrome
88
- Capybara.default_driver = :chrome
9
+ Capybara.default_driver = Capybara.javascript_driver = :chrome
10
+
11
+ Capybara::Session.include Capybara::HeadlessChrome::SessionDSL
89
12
 
90
- Capybara::Session.class_eval do
91
- def downloads
92
- Capybara::HeadlessChrome::Downloads.new
93
- end
94
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-headless_chrome
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
@@ -94,7 +94,8 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '3.0'
97
- description: Wiring between Capybara and Headless Chrome
97
+ description: A nice and tidy Capybara driver for headless Chrome. Even supports file
98
+ downloads!
98
99
  email:
99
100
  - micah@botandrose.com
100
101
  executables: []
@@ -113,6 +114,9 @@ files:
113
114
  - capybara-headless_chrome.gemspec
114
115
  - lib/capybara/headless_chrome.rb
115
116
  - lib/capybara/headless_chrome/cucumber.rb
117
+ - lib/capybara/headless_chrome/downloads.rb
118
+ - lib/capybara/headless_chrome/driver.rb
119
+ - lib/capybara/headless_chrome/session_dsl.rb
116
120
  - lib/capybara/headless_chrome/version.rb
117
121
  homepage: https://github.com/botandrose/capybara-headless_chrome
118
122
  licenses:
@@ -137,5 +141,5 @@ rubyforge_project:
137
141
  rubygems_version: 2.6.14
138
142
  signing_key:
139
143
  specification_version: 4
140
- summary: Wiring between Capybara and Headless Chrome
144
+ summary: A nice and tidy Capybara driver for headless Chrome
141
145
  test_files: []