capybara-playwright-driver 0.4.0 → 0.5.0

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: ff1545e5e7830f5c9c3a3880894c6c4b89cbe3104a18ee388870e16b6d31cd9c
4
- data.tar.gz: 3add9f1a8aa12f459f2efe3b61d8c0200716201b02effacd666bd97f3220d479
3
+ metadata.gz: f12da8f24faaf7dca07c39e57c8ea56d79395a89c2240d99ff48fdc065e10dc1
4
+ data.tar.gz: 07c2560cc17f103aecd41ddd7267e9eb0abd310546f32756a852cc61e3e3f92d
5
5
  SHA512:
6
- metadata.gz: 5a88d7c10d9002bf81c0c5d10760623e69d8b02ee86ef238d3a2138e333d936d3c3fc2c6954b483b93dec856653f99a84bbec59b61106fd5a6df3f374e1616d2
7
- data.tar.gz: aa54328b1a239b1d2800052775a9620180a26cf90ac2cc3fe48334f0b4b4c3d983da29fbc1f3d9aad1b75101319d00e85cd597dcf6ad9d579bda7bee5d236340
6
+ metadata.gz: 0d1de191eff8c0a8a3da8366c38e367eaf215a836c5148cc1ed001c9af28b13f9d9126685a43fb0c871829f9b97e5ec55d0d1cbc77d44da5fb4e42b907941e2f
7
+ data.tar.gz: d7be26ec2e30b783a24324c923ac5ca9164a88077c7010a39e95757b3a0ef68bd156f849354085e8344f76a7f9a38843bc972ad516bd8c1416964aa2a486f538
data/README.md CHANGED
@@ -26,17 +26,18 @@ Capybara.save_path = 'tmp/capybara'
26
26
  # run
27
27
  Capybara.app_host = 'https://github.com'
28
28
  visit '/'
29
- fill_in('q', with: 'Capybara')
29
+ first('div.search-input-container').click
30
+ fill_in('query-builder-test', with: 'Capybara')
30
31
 
31
32
  ## [REMARK] We can use Playwright-native selector and action, instead of Capybara DSL.
32
- # find('a[data-item-type="global_search"]').click
33
+ # first('[aria-label="Capybara, Search all of GitHub"]').click
33
34
  page.driver.with_playwright_page do |page|
34
- page.click('a[data-item-type="global_search"]')
35
+ page.get_by_label('Capybara, Search all of GitHub').click
35
36
  end
36
37
 
37
- all('.repo-list-item').each do |li|
38
+ all('[data-testid="results-list"] h3').each do |li|
38
39
  #puts "#{li.all('a').first.text} by Capybara"
39
- puts "#{li.with_playwright_element_handle { |handle| handle.query_selector('a').text_content }} by Playwright"
40
+ puts "#{li.with_playwright_element_handle { |handle| handle.text_content }} by Playwright"
40
41
  end
41
42
  ```
42
43
 
@@ -14,25 +14,31 @@ module Capybara
14
14
 
15
15
  class NoSuchWindowError < StandardError ; end
16
16
 
17
- def initialize(driver:, playwright_browser:, page_options:, record_video: false, default_navigation_timeout: nil)
17
+ def initialize(driver:, playwright_browser:, page_options:, record_video: false, callback_on_save_trace: nil, default_timeout: nil, default_navigation_timeout: nil)
18
18
  @driver = driver
19
19
  @playwright_browser = playwright_browser
20
20
  @page_options = page_options
21
21
  if record_video
22
22
  @page_options[:record_video_dir] ||= tmpdir
23
23
  end
24
+ @callback_on_save_trace = callback_on_save_trace
25
+ @default_timeout = default_timeout
24
26
  @default_navigation_timeout = default_navigation_timeout
25
27
  @playwright_page = create_page(create_browser_context)
26
28
  end
27
29
 
28
30
  private def create_browser_context
29
31
  @playwright_browser.new_context(**@page_options).tap do |browser_context|
32
+ browser_context.default_timeout = @default_timeout if @default_timeout
30
33
  browser_context.default_navigation_timeout = @default_navigation_timeout if @default_navigation_timeout
31
34
  browser_context.on('page', ->(page) {
32
35
  unless @playwright_page
33
36
  @playwright_page = page
34
37
  end
35
38
  })
39
+ if @callback_on_save_trace
40
+ browser_context.tracing.start(screenshots: true, snapshots: true)
41
+ end
36
42
  end
37
43
  end
38
44
 
@@ -47,6 +53,14 @@ module Capybara
47
53
  end
48
54
 
49
55
  def clear_browser_contexts
56
+ if @callback_on_save_trace
57
+ @playwright_browser.contexts.each do |browser_context|
58
+ filename = SecureRandom.hex(8)
59
+ zip_path = File.join(tmpdir, "#{filename}.zip")
60
+ browser_context.tracing.stop(path: zip_path)
61
+ @callback_on_save_trace.call(zip_path)
62
+ end
63
+ end
50
64
  @playwright_browser.contexts.each(&:close)
51
65
  end
52
66
 
@@ -9,9 +9,15 @@ module Capybara
9
9
  def initialize(app, **options)
10
10
  @browser_runner = BrowserRunner.new(options)
11
11
  @page_options = PageOptions.new(options)
12
- if options[:timeout].is_a?(Numeric)
12
+ if options[:timeout].is_a?(Numeric) # just for compatibility with capybara-selenium-driver
13
13
  @default_navigation_timeout = options[:timeout] * 1000
14
14
  end
15
+ if options[:default_timeout].is_a?(Numeric)
16
+ @default_timeout = options[:default_timeout] * 1000
17
+ end
18
+ if options[:default_navigation_timeout].is_a?(Numeric)
19
+ @default_navigation_timeout = options[:default_navigation_timeout] * 1000
20
+ end
15
21
  end
16
22
 
17
23
  def wait?; true; end
@@ -23,6 +29,8 @@ module Capybara
23
29
  playwright_browser: playwright_browser,
24
30
  page_options: @page_options.value,
25
31
  record_video: callback_on_save_screenrecord?,
32
+ callback_on_save_trace: @callback_on_save_trace,
33
+ default_timeout: @default_timeout,
26
34
  default_navigation_timeout: @default_navigation_timeout,
27
35
  )
28
36
  end
@@ -35,11 +35,46 @@ module Capybara
35
35
  @callback_on_save_screenrecord&.call(video_path)
36
36
  end
37
37
 
38
+ # Register trace save process.
39
+ # The callback is called just after trace is saved.
40
+ #
41
+ # The trace.zip path (String) is called back into the given block
42
+ def on_save_trace(&block)
43
+ @callback_on_save_trace = block
44
+ end
45
+
38
46
  def with_playwright_page(&block)
39
47
  raise ArgumentError.new('block must be given') unless block
40
48
 
41
49
  @browser&.with_playwright_page(&block)
42
50
  end
51
+
52
+ # Start Playwright tracing (doc: https://playwright.dev/docs/api/class-tracing#tracing-start)
53
+ def start_tracing(name: nil, screenshots: false, snapshots: false, sources: false, title: nil)
54
+ # Ensure playwright page is initialized.
55
+ browser
56
+
57
+ with_playwright_page do |playwright_page|
58
+ playwright_page.context.tracing.start(name: name, screenshots: screenshots, snapshots: snapshots, sources: sources, title: title)
59
+ end
60
+ end
61
+
62
+ # Stop Playwright tracing (doc: https://playwright.dev/docs/api/class-tracing#tracing-stop)
63
+ def stop_tracing(path: nil)
64
+ with_playwright_page do |playwright_page|
65
+ playwright_page.context.tracing.stop(path: path)
66
+ end
67
+ end
68
+
69
+ # Trace execution of the given block. The tracing is automatically stopped when the block is finished.
70
+ def trace(name: nil, screenshots: false, snapshots: false, sources: false, title: nil, path: nil, &block)
71
+ raise ArgumentError.new('block must be given') unless block
72
+
73
+ start_tracing(name: name, screenshots: screenshots, snapshots: snapshots, sources: sources, title: title)
74
+ block.call
75
+ ensure
76
+ stop_tracing(path: path)
77
+ end
43
78
  end
44
79
  end
45
80
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Capybara
4
4
  module Playwright
5
- VERSION = '0.4.0'
5
+ VERSION = '0.5.0'
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.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - YusukeIwaki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-10 00:00:00.000000000 Z
11
+ date: 2023-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capybara