capybara-playwright-driver 0.4.0 → 0.5.1

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: 65aa84fdb6c7d3f3fd725b263e265f45d8561ed1d3c76c838fe7aabc9fdd86b2
4
+ data.tar.gz: a3215d7afedcc54c1054726905c37c4406bd5f68b774c43ce0b525056fab26b5
5
5
  SHA512:
6
- metadata.gz: 5a88d7c10d9002bf81c0c5d10760623e69d8b02ee86ef238d3a2138e333d936d3c3fc2c6954b483b93dec856653f99a84bbec59b61106fd5a6df3f374e1616d2
7
- data.tar.gz: aa54328b1a239b1d2800052775a9620180a26cf90ac2cc3fe48334f0b4b4c3d983da29fbc1f3d9aad1b75101319d00e85cd597dcf6ad9d579bda7bee5d236340
6
+ metadata.gz: cc48335bbc9baa44eaede35ba9c7d3bd71cd66f9de1b15e099ece3e8615084db6bad2e15ac699c26a4679eecf573ce063944e135a77a652d566bb0cdbf153692
7
+ data.tar.gz: a9e1365afcda4442f7262023c6983bfb269921cb1b25bac52d42b971797f7913a97ca638c5c1109db53f7bf6eda229b2dbf08b3273de6ad94026b1775b4054a2
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
 
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.require_paths = ['lib']
26
26
 
27
27
  spec.required_ruby_version = '>= 2.4'
28
+ spec.add_dependency 'addressable'
28
29
  spec.add_dependency 'capybara'
29
30
  spec.add_dependency 'playwright-ruby-client', '>= 1.16.0'
30
31
  spec.add_development_dependency 'allure-rspec'
@@ -1,3 +1,4 @@
1
+ require 'addressable/uri'
1
2
  require_relative './tmpdir_owner'
2
3
 
3
4
  module Capybara
@@ -14,25 +15,31 @@ module Capybara
14
15
 
15
16
  class NoSuchWindowError < StandardError ; end
16
17
 
17
- def initialize(driver:, playwright_browser:, page_options:, record_video: false, default_navigation_timeout: nil)
18
+ def initialize(driver:, playwright_browser:, page_options:, record_video: false, callback_on_save_trace: nil, default_timeout: nil, default_navigation_timeout: nil)
18
19
  @driver = driver
19
20
  @playwright_browser = playwright_browser
20
21
  @page_options = page_options
21
22
  if record_video
22
23
  @page_options[:record_video_dir] ||= tmpdir
23
24
  end
25
+ @callback_on_save_trace = callback_on_save_trace
26
+ @default_timeout = default_timeout
24
27
  @default_navigation_timeout = default_navigation_timeout
25
28
  @playwright_page = create_page(create_browser_context)
26
29
  end
27
30
 
28
31
  private def create_browser_context
29
32
  @playwright_browser.new_context(**@page_options).tap do |browser_context|
33
+ browser_context.default_timeout = @default_timeout if @default_timeout
30
34
  browser_context.default_navigation_timeout = @default_navigation_timeout if @default_navigation_timeout
31
35
  browser_context.on('page', ->(page) {
32
36
  unless @playwright_page
33
37
  @playwright_page = page
34
38
  end
35
39
  })
40
+ if @callback_on_save_trace
41
+ browser_context.tracing.start(screenshots: true, snapshots: true)
42
+ end
36
43
  end
37
44
  end
38
45
 
@@ -47,6 +54,14 @@ module Capybara
47
54
  end
48
55
 
49
56
  def clear_browser_contexts
57
+ if @callback_on_save_trace
58
+ @playwright_browser.contexts.each do |browser_context|
59
+ filename = SecureRandom.hex(8)
60
+ zip_path = File.join(tmpdir, "#{filename}.zip")
61
+ browser_context.tracing.stop(path: zip_path)
62
+ @callback_on_save_trace.call(zip_path)
63
+ end
64
+ end
50
65
  @playwright_browser.contexts.each(&:close)
51
66
  end
52
67
 
@@ -60,9 +75,9 @@ module Capybara
60
75
  assert_page_alive {
61
76
  url =
62
77
  if Capybara.app_host
63
- URI(Capybara.app_host).merge(path)
78
+ Addressable::URI.parse(Capybara.app_host) + path
64
79
  elsif Capybara.default_host
65
- URI(Capybara.default_host).merge(path)
80
+ Addressable::URI.parse(Capybara.default_host) + path
66
81
  else
67
82
  path
68
83
  end
@@ -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.1'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,15 +1,29 @@
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.1
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: 2024-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: addressable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: capybara
15
29
  requirement: !ruby/object:Gem::Requirement