capybara-puppeteer-driver 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0b6ce352841199c3218ea9b9ea07c24e54e8c25b72ef01c0f08614a49e7761e3
4
+ data.tar.gz: 5a81ff21fc5fbc5fc937076591ec622cb7d2c78b7ea2a636b40f84c41ef514af
5
+ SHA512:
6
+ metadata.gz: cec655cd4c177f542aa34121fd8bc3ec756c7c75752fdda1ae7c1068ae4bafa0ef8c38fcb623e476db66eff9d6ae2aac644d048f1d1e5a6d2929ca5f491deeeb
7
+ data.tar.gz: 75b0259ac206c9dd9a0f407344f15850319bd476d96faca3322c7523c6fc5d65fec33fc004d431dfa16a9adf27d4c18890483aea9fc005c0c5e02d580b7259e7
@@ -0,0 +1,31 @@
1
+ name: Deploy
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - '[0-9]+.[0-9]+.[0-9]+'
7
+
8
+ jobs:
9
+ push_to_rubygems:
10
+ name: Push to RubyGems
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - name: Set RELEASE_TAG
14
+ run: echo "RELEASE_TAG=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
15
+ - uses: actions/checkout@v2
16
+ - uses: ruby/setup-ruby@v1
17
+ with:
18
+ ruby-version: 2.7.3
19
+ bundler-cache: true
20
+ - name: Check Capybara::Puppeteer::VERSION
21
+ run: bundle exec ruby -e 'raise "invalid Capybara::Puppeteer::VERSION" unless Capybara::Puppeteer::VERSION == ENV["RELEASE_TAG"]'
22
+ - run: rake build
23
+ - name: setup API key
24
+ run: |
25
+ mkdir -p ~/.gem/
26
+ echo "---" > ~/.gem/credentials
27
+ echo ":rubygems_api_key: $RUBYGEMS_API_KEY" >> ~/.gem/credentials
28
+ chmod 600 ~/.gem/credentials
29
+ env:
30
+ RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}}
31
+ - run: gem push pkg/capybara-puppeteer-driver-$RELEASE_TAG.gem
@@ -0,0 +1,18 @@
1
+ name: Check
2
+
3
+ on: [pull_request]
4
+
5
+ jobs:
6
+ check:
7
+ name: RSpec
8
+ runs-on: ubuntu-latest
9
+ container: circleci/ruby:2.7.3-buster-node-browsers
10
+ steps:
11
+ # @see https://docs.github.com/actions/reference/virtual-environments-for-github-hosted-runners#docker-container-filesystem
12
+ - name: Setup file system permissions
13
+ run: sudo chmod -R 777 $GITHUB_WORKSPACE /github /__w/_temp
14
+ - uses: actions/checkout@v2
15
+ - name: Install dependencies
16
+ run: gem install bundler:2.2.3 && bundle install --path vendor/bundle
17
+ - run: bundle exec rspec --format documentation
18
+ timeout-minutes: 45
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ /.ruby-version
11
+ /.vscode
12
+ /Gemfile.lock
13
+ /vendor/bundle
14
+
15
+ # rspec failure tracking
16
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 1.17.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at iwaki@i3-systems.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in capybara-puppeteer-driver.gemspec
8
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 YusukeIwaki
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,43 @@
1
+ # capybara-puppeteer-driver
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/puppeteer`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'capybara-puppeteer-driver'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install capybara-puppeteer-driver
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/capybara-puppeteer-driver. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Capybara::Puppeteer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/capybara-puppeteer-driver/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
data/bin/console ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'capybara/puppeteer'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ require 'pry'
12
+ Pry.start
data/bin/setup ADDED
@@ -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,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'capybara/puppeteer/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'capybara-puppeteer-driver'
9
+ spec.version = Capybara::Puppeteer::VERSION
10
+ spec.authors = ['YusukeIwaki']
11
+ spec.email = ['q7w8e9w8q7w8e9@yahoo.co.jp']
12
+
13
+ spec.summary = 'Headless Chrome driver for Capybara using puppeteer-ruby'
14
+ spec.homepage = 'https://github.com/YusukeIwaki/capybara-puppeteer-driver'
15
+ spec.license = 'MIT'
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_dependency 'capybara'
27
+ spec.add_dependency 'puppeteer-ruby', '>= 0.34.3'
28
+ spec.add_development_dependency 'bundler', '~> 2.2.3'
29
+ spec.add_development_dependency 'launchy', '>= 2.0.4'
30
+ spec.add_development_dependency 'pry-byebug'
31
+ spec.add_development_dependency 'rake', '~> 13.0.3'
32
+ spec.add_development_dependency 'rspec', '~> 3.10.0'
33
+ spec.add_development_dependency 'rubocop-rspec'
34
+ spec.add_development_dependency 'sinatra', '>= 1.4.0'
35
+ spec.add_development_dependency 'webrick'
36
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'puppeteer'
4
+ require 'capybara'
5
+
6
+ require 'capybara/puppeteer/browser'
7
+ require 'capybara/puppeteer/browser_options'
8
+ require 'capybara/puppeteer/dialog_event_handler'
9
+ require 'capybara/puppeteer/driver'
10
+ require 'capybara/puppeteer/node'
11
+ require 'capybara/puppeteer/page'
@@ -0,0 +1,302 @@
1
+ module Capybara
2
+ module Puppeteer
3
+ class Browser
4
+ class NoSuchWindowError < StandardError ; end
5
+
6
+ def initialize(driver:, puppeteer_browser:)
7
+ @driver = driver
8
+ @puppeteer_browser = puppeteer_browser
9
+ @puppeteer_page = puppeteer_browser.pages.first || puppeteer_browser.new_page
10
+
11
+ @puppeteer_browser.on('targetdestroyed') do |target|
12
+ if target.type == 'page'
13
+ if target.target_id == @puppeteer_page&.capybara_id
14
+ @puppeteer_page = nil
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ def current_url
21
+ assert_page_alive
22
+
23
+ @puppeteer_page.url
24
+ end
25
+
26
+ def visit(path)
27
+ assert_page_alive
28
+
29
+ url =
30
+ if Capybara.app_host
31
+ URI(Capybara.app_host).merge(path)
32
+ elsif Capybara.default_host
33
+ URI(Capybara.default_host).merge(path)
34
+ else
35
+ path
36
+ end
37
+
38
+ @puppeteer_page.capybara_current_frame.goto(url)
39
+ end
40
+
41
+ def refresh
42
+ assert_page_alive
43
+
44
+ @puppeteer_page.capybara_current_frame.evaluate('() => { location.reload(true) }')
45
+ end
46
+
47
+ private def find_with(query_method, query, **options)
48
+ begin
49
+ @puppeteer_page.capybara_current_frame.send(query_method, query).map do |el|
50
+ Node.new(@driver, @puppeteer_page, el)
51
+ end
52
+ rescue => err
53
+ # Navigation occured during finding Node.
54
+ if err.message =~ /Cannot find context with specified id/
55
+ return [] # Rely on Capybara's retry.
56
+ end
57
+
58
+ raise
59
+ end
60
+ end
61
+
62
+ def find_xpath(query, **options)
63
+ assert_page_alive
64
+
65
+ find_with(:Sx, query, **options)
66
+ end
67
+
68
+ def find_css(query, **options)
69
+ assert_page_alive
70
+
71
+ find_with(:query_selector_all, query, **options)
72
+ end
73
+
74
+ def response_headers
75
+ assert_page_alive
76
+
77
+ @puppeteer_page.capybara_response_headers
78
+ end
79
+
80
+ def status_code
81
+ assert_page_alive
82
+
83
+ @puppeteer_page.capybara_status_code
84
+ end
85
+
86
+ def html
87
+ assert_page_alive
88
+
89
+ js = <<~JAVASCRIPT
90
+ () => {
91
+ let html = '';
92
+ if (document.doctype) html += new XMLSerializer().serializeToString(document.doctype);
93
+ if (document.documentElement) html += document.documentElement.outerHTML;
94
+ return html;
95
+ }
96
+ JAVASCRIPT
97
+ @puppeteer_page.capybara_current_frame.evaluate(js)
98
+ end
99
+
100
+ def title
101
+ assert_page_alive
102
+
103
+ @puppeteer_page.title
104
+ end
105
+
106
+ def go_back
107
+ assert_page_alive
108
+
109
+ @puppeteer_page.go_back
110
+ end
111
+
112
+ def go_forward
113
+ assert_page_alive
114
+
115
+ @puppeteer_page.go_forward
116
+ end
117
+
118
+ def execute_script(script, *args)
119
+ assert_page_alive
120
+
121
+ @puppeteer_page.capybara_current_frame.evaluate("function () { #{script} }", *unwrap_node(args))
122
+ nil
123
+ end
124
+
125
+ def evaluate_script(script, *args)
126
+ assert_page_alive
127
+
128
+ result = @puppeteer_page.capybara_current_frame.evaluate_handle("function () { return #{script} }", *unwrap_node(args))
129
+ wrap_node(result)
130
+ end
131
+
132
+ def evaluate_async_script(script, *args)
133
+ assert_page_alive
134
+
135
+ js = <<~JAVASCRIPT
136
+ function(){
137
+ let args = Array.prototype.slice.call(arguments);
138
+ return new Promise((resolve, reject) => {
139
+ args.push(resolve);
140
+ (function(){ #{script} }).apply(this, args);
141
+ });
142
+ }
143
+ JAVASCRIPT
144
+ result = @puppeteer_page.capybara_current_frame.evaluate_handle(js, *unwrap_node(args))
145
+ wrap_node(result)
146
+ end
147
+
148
+ def send_keys(*args)
149
+ assert_page_alive
150
+
151
+ Node::SendKeys.new(@puppeteer_page.keyboard, @puppeteer_page.keyboard, args).execute
152
+ end
153
+
154
+ def save_screenshot(path, **options)
155
+ assert_page_alive
156
+
157
+ @puppeteer_page.screenshot(path: path)
158
+ end
159
+
160
+ def switch_to_frame(frame)
161
+ case frame
162
+ when :top
163
+ @puppeteer_page.capybara_reset_frames
164
+ when :parent
165
+ @puppeteer_page.capybara_pop_frame
166
+ else
167
+ puppeteer_frame = frame.native.content_frame
168
+ raise ArgumentError.new("Not a frame element: #{frame}") unless puppeteer_frame
169
+ @puppeteer_page.capybara_push_frame(puppeteer_frame)
170
+ end
171
+ end
172
+
173
+ private def assert_page_alive
174
+ if !@puppeteer_page || @puppeteer_page.closed?
175
+ raise NoSuchWindowError
176
+ end
177
+ end
178
+
179
+ def window_handles
180
+ @puppeteer_browser.pages.map(&:capybara_id)
181
+ end
182
+
183
+ def current_window_handle
184
+ @puppeteer_page&.capybara_id
185
+ end
186
+
187
+ def open_new_window(kind = :tab)
188
+ @puppeteer_browser.new_page
189
+ end
190
+
191
+ private def on_window(handle, &block)
192
+ page = @puppeteer_browser.pages.find { |page| page.capybara_id == handle }
193
+ if page
194
+ block.call(page)
195
+ else
196
+ raise NoSuchWindowError
197
+ end
198
+ end
199
+
200
+ def switch_to_window(handle)
201
+ return if @puppeteer_page&.capybara_id == handle
202
+
203
+ on_window(handle) do |page|
204
+ @puppeteer_page = page.tap(&:bring_to_front)
205
+ end
206
+ end
207
+
208
+ def close_window(handle)
209
+ on_window(handle) do |page|
210
+ page.close
211
+ end
212
+
213
+ if @puppeteer_page&.capybara_id == handle
214
+ @puppeteer_page = nil
215
+ end
216
+ end
217
+
218
+ def window_size(handle)
219
+ on_window(handle) do |page|
220
+ page.evaluate('() => [window.innerWidth, window.innerHeight]')
221
+ end
222
+ end
223
+
224
+ def resize_window_to(handle, width, height)
225
+ on_window(handle) do |page|
226
+ page.viewport = ::Puppeteer::Viewport.new(width: width, height: height)
227
+ end
228
+ end
229
+
230
+ def maximize_window(handle)
231
+ on_window(handle) do |page|
232
+ screen_size = page.evaluate('() => ({ width: window.screen.width, height: window.screen.height})')
233
+ page.viewport = ::Puppeteer::Viewport.new(width: screen_size['width'], height: screen_size['height'])
234
+ end
235
+ end
236
+
237
+ def fullscreen_window(handle)
238
+ on_window(handle) do |page|
239
+ page.evaluate('() => document.body.requestFullscreen()')
240
+ end
241
+ end
242
+
243
+ def accept_modal(dialog_type, **options, &block)
244
+ assert_page_alive
245
+
246
+ @puppeteer_page.capybara_accept_modal(dialog_type, **options, &block)
247
+ end
248
+
249
+ def dismiss_modal(dialog_type, **options, &block)
250
+ assert_page_alive
251
+
252
+ @puppeteer_page.capybara_dismiss_modal(dialog_type, **options, &block)
253
+ end
254
+
255
+ private def capybara_default_wait_time
256
+ Capybara.default_max_wait_time * 1000
257
+ end
258
+
259
+ private def unwrap_node(args)
260
+ args.map do |arg|
261
+ if arg.is_a?(Node)
262
+ arg.native
263
+ else
264
+ arg
265
+ end
266
+ end
267
+ end
268
+
269
+ private def wrap_node(arg)
270
+ case arg
271
+ when Array
272
+ arg.map do |item|
273
+ wrap_node(item)
274
+ end
275
+ when Hash
276
+ arg.map do |key, value|
277
+ [key, wrap_node(value)]
278
+ end.to_h
279
+ when ::Puppeteer::ElementHandle
280
+ Node.new(@driver, @puppeteer_page, arg)
281
+ when ::Puppeteer::JSHandle
282
+ obj_type, is_array = arg.evaluate('obj => [typeof obj, Array.isArray(obj)]')
283
+ if obj_type == 'object'
284
+ if is_array
285
+ arg.properties.map do |_, value|
286
+ wrap_node(value)
287
+ end
288
+ else
289
+ arg.properties.map do |key, value|
290
+ [key, wrap_node(value)]
291
+ end.to_h
292
+ end
293
+ else
294
+ arg.json_value
295
+ end
296
+ else
297
+ arg
298
+ end
299
+ end
300
+ end
301
+ end
302
+ end