capybara-playwright-driver 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9e6ff7fd5aaa4b41cc19395605ea19820f140703b1e52d7e2e07aaaa73f0428a
4
- data.tar.gz: d77b98b91bad49e4a115b2cc7c8238611f02389e882a444d065418aeea3c2ae2
3
+ metadata.gz: '08985d0c37caddddfb7059ad5a4447df6264d00ee692b04b123eb1a52452a479'
4
+ data.tar.gz: 2570f39ea3cb9571910d8d70d1b2bbd2f4a011f974e2a0e0c984c36319fa0753
5
5
  SHA512:
6
- metadata.gz: e3eafd55be3b31d780fe7f4f1c7f5c3afa35ead692a5ce8ea22e755c4cceab37d390bbcf875fad6ae355e5475e047dc4719bbf6ebfd7abaf04732d52e9ed7c60
7
- data.tar.gz: 7cf9a6db8a89ba6437e6790812924682c08bbf55bc182737159aea1883190313a86aeddf86a85219fba048ed884b93a0cfd868e97f74b25e171974f0c56ebabc
6
+ metadata.gz: 8a8a2b2897c5a5b4baca85fb223b128ff620db07327c80e82da543a77a89d74726f682b0c0f952bb797b13c2fa209421cef527c5edab7790d0d2c66efb702554
7
+ data.tar.gz: d7b1db32061db7491d5d0d6595e35d90a72731b6d5a011f5cf56ce5985b8d94c58d4fc46dd4b18e95ddfab519cf1df57cf74bfc25de1aa787367f69dea290734
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.required_ruby_version = '>= 2.4'
28
28
  spec.add_dependency 'capybara'
29
29
  spec.add_dependency 'playwright-ruby-client', '>= 0.5.9'
30
+ spec.add_development_dependency 'allure-rspec'
30
31
  spec.add_development_dependency 'bundler', '~> 2.2.3'
31
32
  spec.add_development_dependency 'launchy', '>= 2.0.4'
32
33
  spec.add_development_dependency 'pry-byebug'
@@ -1,16 +1,26 @@
1
+ require_relative './tmpdir_owner'
2
+
1
3
  module Capybara
2
4
  module Playwright
5
+ # Responsibility of this class is:
6
+ # - Handling Capybara::Driver commands.
7
+ # - Managing Playwright browser contexts and pages.
8
+ #
9
+ # Note that this class doesn't manage Playwright::Browser.
10
+ # We should not use Playwright::Browser#close in this class.
3
11
  class Browser
12
+ include TmpdirOwner
4
13
  extend Forwardable
5
14
 
6
15
  class NoSuchWindowError < StandardError ; end
7
16
 
8
- def initialize(playwright:, driver:, browser_type:, browser_options:, page_options:)
17
+ def initialize(driver:, playwright_browser:, page_options:, record_video: false)
9
18
  @driver = driver
10
-
11
- browser_type = playwright.send(browser_type)
12
- @playwright_browser = browser_type.launch(**browser_options)
19
+ @playwright_browser = playwright_browser
13
20
  @page_options = page_options
21
+ if record_video
22
+ @page_options[:record_video_dir] ||= tmpdir
23
+ end
14
24
  @playwright_page = create_page(create_browser_context)
15
25
  end
16
26
 
@@ -34,8 +44,8 @@ module Capybara
34
44
  end
35
45
  end
36
46
 
37
- def quit
38
- @playwright_browser.close
47
+ def clear_browser_contexts
48
+ @playwright_browser.contexts.each(&:close)
39
49
  end
40
50
 
41
51
  def current_url
@@ -155,6 +165,22 @@ module Capybara
155
165
  wrap_node(result)
156
166
  end
157
167
 
168
+ # Not used by Capybara::Session.
169
+ # Intended to be directly called by user.
170
+ def video_path
171
+ return nil if !@playwright_page || @playwright_page.closed?
172
+
173
+ @playwright_page.video&.path
174
+ end
175
+
176
+ # Not used by Capybara::Session.
177
+ # Intended to be directly called by user.
178
+ def raw_screenshot(**options)
179
+ return nil if !@playwright_page || @playwright_page.closed?
180
+
181
+ @playwright_page.screenshot(**options)
182
+ end
183
+
158
184
  def save_screenshot(path, **options)
159
185
  assert_page_alive
160
186
 
@@ -310,7 +336,6 @@ module Capybara
310
336
 
311
337
  def with_playwright_page(&block)
312
338
  assert_page_alive
313
- raise ArgumentError.new('block must be given') unless block
314
339
 
315
340
  block.call(@playwright_page)
316
341
  end
@@ -1,7 +1,10 @@
1
+ require_relative './driver_extension'
2
+
1
3
  module Capybara
2
4
  module Playwright
3
5
  class Driver < ::Capybara::Driver::Base
4
6
  extend Forwardable
7
+ include DriverExtension
5
8
 
6
9
  def initialize(app, **options)
7
10
  @playwright_cli_executable_path = options[:playwright_cli_executable_path] || 'npx playwright'
@@ -18,10 +21,20 @@ module Capybara
18
21
  def needs_server?; true; end
19
22
 
20
23
  def browser
21
- @browser ||= create_browser
24
+ @browser ||= ::Capybara::Playwright::Browser.new(
25
+ driver: self,
26
+ playwright_browser: playwright_browser,
27
+ page_options: @page_options.value,
28
+ record_video: callback_on_save_screenrecord?,
29
+ )
22
30
  end
23
31
 
24
- private def create_browser
32
+ private def playwright_browser
33
+ @playwright_browser ||= create_playwright_browser
34
+ end
35
+
36
+ private def create_playwright_browser
37
+ # clean up @playwright_browser and @playwright_execution on exit.
25
38
  main = Process.pid
26
39
  at_exit do
27
40
  # Store the exit status of the test run since it goes away after calling the at_exit proc...
@@ -30,27 +43,45 @@ module Capybara
30
43
  exit @exit_status if @exit_status # Force exit with stored status
31
44
  end
32
45
 
33
- @execution = execute_playwright
34
- ::Capybara::Playwright::Browser.new(
35
- playwright: @execution.playwright,
36
- driver: self,
37
- browser_type: @browser_type,
38
- browser_options: @browser_options.value,
39
- page_options: @page_options.value,
40
- )
46
+ browser_type = playwright_execution.playwright.send(@browser_type)
47
+ browser_options = @browser_options.value
48
+ browser_type.launch(**browser_options)
41
49
  end
42
50
 
43
- private def execute_playwright
44
- ::Playwright.create(playwright_cli_executable_path: @playwright_cli_executable_path)
51
+ private def playwright_execution
52
+ @playwright_execution ||= ::Playwright.create(
53
+ playwright_cli_executable_path: @playwright_cli_executable_path,
54
+ )
45
55
  end
46
56
 
47
57
  private def quit
48
- @browser&.quit
49
- @execution&.stop
58
+ @playwright_browser&.close
59
+ @playwright_browser = nil
60
+ @playwright_execution&.stop
61
+ @playwright_execution = nil
50
62
  end
51
63
 
52
64
  def reset!
53
- quit
65
+ # screenshot is available only before closing page.
66
+ if callback_on_save_screenshot?
67
+ raw_screenshot = @browser&.raw_screenshot
68
+ if raw_screenshot
69
+ callback_on_save_screenshot(raw_screenshot)
70
+ end
71
+ end
72
+
73
+ # video path can be aquired only before closing context.
74
+ # video is completedly saved only after closing context.
75
+ video_path = @browser&.video_path
76
+
77
+ # [NOTE] @playwright_browser should keep alive for better performance.
78
+ # Only `Browser` is disposed.
79
+ @browser&.clear_browser_contexts
80
+
81
+ if video_path
82
+ callback_on_save_screenrecord(video_path)
83
+ end
84
+
54
85
  @browser = nil
55
86
  end
56
87
 
@@ -94,9 +125,6 @@ module Capybara
94
125
  def_delegator(:browser, :switch_to_window)
95
126
  def_delegator(:browser, :accept_modal)
96
127
  def_delegator(:browser, :dismiss_modal)
97
-
98
- # capybara-playwright-driver specific methods
99
- def_delegator(:browser, :with_playwright_page)
100
128
  end
101
129
  end
102
130
  end
@@ -0,0 +1,45 @@
1
+ module Capybara
2
+ module Playwright
3
+ module DriverExtension
4
+ # Register screenshot save process.
5
+ # The callback is called just before page is closed.
6
+ # (just before #reset_session!)
7
+ #
8
+ # The **binary** (String) of the page screenshot is called back into the given block
9
+ def on_save_raw_screenshot_before_reset(&block)
10
+ @callback_on_save_screenshot = block
11
+ end
12
+
13
+ private def callback_on_save_screenshot?
14
+ !!@callback_on_save_screenshot
15
+ end
16
+
17
+ private def callback_on_save_screenshot(raw_screenshot)
18
+ @callback_on_save_screenshot&.call(raw_screenshot)
19
+ end
20
+
21
+ # Register screenrecord save process.
22
+ # The callback is called just after page is closed.
23
+ # (just after #reset_session!)
24
+ #
25
+ # The video path (String) is called back into the given block
26
+ def on_save_screenrecord(&block)
27
+ @callback_on_save_screenrecord = block
28
+ end
29
+
30
+ private def callback_on_save_screenrecord?
31
+ !!@callback_on_save_screenrecord
32
+ end
33
+
34
+ private def callback_on_save_screenrecord(video_path)
35
+ @callback_on_save_screenrecord&.call(video_path)
36
+ end
37
+
38
+ def with_playwright_page(&block)
39
+ raise ArgumentError.new('block must be given') unless block
40
+
41
+ @browser&.with_playwright_page(&block)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,40 @@
1
+ module Capybara
2
+ module Playwright
3
+ module TmpdirOwner
4
+ require 'tmpdir'
5
+
6
+ def tmpdir
7
+ return @tmpdir if @tmpdir
8
+
9
+ dir = Dir.mktmpdir
10
+ ObjectSpace.define_finalizer(self, TmpdirRemover.new(dir))
11
+ @tmpdir = dir
12
+ end
13
+
14
+ def remove_tmpdir
15
+ if @tmpdir
16
+ FileUtils.remove_entry(@tmpdir, true)
17
+ ObjectSpace.undefine_finalizer(self)
18
+ @tmpdir = nil
19
+ end
20
+ end
21
+
22
+ class TmpdirRemover
23
+ def initialize(tmpdir)
24
+ @pid = Process.pid
25
+ @tmpdir = tmpdir
26
+ end
27
+
28
+ def call(*args)
29
+ return if @pid != Process.pid
30
+
31
+ begin
32
+ FileUtils.remove_entry(@tmpdir, true)
33
+ rescue => err
34
+ $stderr.puts err
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Capybara
4
4
  module Playwright
5
- VERSION = '0.1.2'
5
+ VERSION = '0.1.3'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-playwright-driver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - YusukeIwaki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-02 00:00:00.000000000 Z
11
+ date: 2021-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capybara
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.5.9
41
+ - !ruby/object:Gem::Dependency
42
+ name: allure-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -172,9 +186,11 @@ files:
172
186
  - lib/capybara/playwright/browser_options.rb
173
187
  - lib/capybara/playwright/dialog_event_handler.rb
174
188
  - lib/capybara/playwright/driver.rb
189
+ - lib/capybara/playwright/driver_extension.rb
175
190
  - lib/capybara/playwright/node.rb
176
191
  - lib/capybara/playwright/page.rb
177
192
  - lib/capybara/playwright/page_options.rb
193
+ - lib/capybara/playwright/tmpdir_owner.rb
178
194
  - lib/capybara/playwright/version.rb
179
195
  homepage: https://github.com/YusukeIwaki/capybara-playwright-driver
180
196
  licenses: