diff --git a/lib/capybara/playwright/browser.rb b/lib/capybara/playwright/browser.rb index dda4827..c072e0d 100644 --- a/lib/capybara/playwright/browser.rb +++ b/lib/capybara/playwright/browser.rb @@ -14,13 +14,14 @@ class Browser class NoSuchWindowError < StandardError ; end - def initialize(driver:, playwright_browser:, page_options:, record_video: false, default_timeout: nil, default_navigation_timeout: nil) + def initialize(driver:, playwright_browser:, page_options:, record_video: false, callback_on_save_trace: nil, default_timeout: nil, default_navigation_timeout: nil) @driver = driver @playwright_browser = playwright_browser @page_options = page_options if record_video @page_options[:record_video_dir] ||= tmpdir end + @callback_on_save_trace = callback_on_save_trace @default_timeout = default_timeout @default_navigation_timeout = default_navigation_timeout @playwright_page = create_page(create_browser_context) @@ -35,6 +36,9 @@ def initialize(driver:, playwright_browser:, page_options:, record_video: false, @playwright_page = page end }) + if @callback_on_save_trace + browser_context.tracing.start(screenshots: true, snapshots: true) + end end end @@ -49,6 +53,14 @@ def initialize(driver:, playwright_browser:, page_options:, record_video: false, end def clear_browser_contexts + if @callback_on_save_trace + @playwright_browser.contexts.each do |browser_context| + filename = SecureRandom.hex(8) + zip_path = File.join(tmpdir, "#{filename}.zip") + browser_context.tracing.stop(path: zip_path) + @callback_on_save_trace.call(zip_path) + end + end @playwright_browser.contexts.each(&:close) end diff --git a/lib/capybara/playwright/driver.rb b/lib/capybara/playwright/driver.rb index fb5e558..cf75c82 100644 --- a/lib/capybara/playwright/driver.rb +++ b/lib/capybara/playwright/driver.rb @@ -29,6 +29,7 @@ def needs_server?; true; end playwright_browser: playwright_browser, page_options: @page_options.value, record_video: callback_on_save_screenrecord?, + callback_on_save_trace: @callback_on_save_trace, default_timeout: @default_timeout, default_navigation_timeout: @default_navigation_timeout, ) diff --git a/lib/capybara/playwright/driver_extension.rb b/lib/capybara/playwright/driver_extension.rb index 5367df4..e2fdad9 100644 --- a/lib/capybara/playwright/driver_extension.rb +++ b/lib/capybara/playwright/driver_extension.rb @@ -35,6 +35,14 @@ def on_save_screenrecord(&block) @callback_on_save_screenrecord&.call(video_path) end + # Register trace save process. + # The callback is called just after trace is saved. + # + # The trace.zip path (String) is called back into the given block + def on_save_trace(&block) + @callback_on_save_trace = block + end + def with_playwright_page(&block) raise ArgumentError.new('block must be given') unless block diff --git a/spec/feature/example_spec.rb b/spec/feature/example_spec.rb index 6891f9f..21c1557 100644 --- a/spec/feature/example_spec.rb +++ b/spec/feature/example_spec.rb @@ -21,6 +21,17 @@ test_case: true, ) end + + Capybara.current_session.driver.on_save_trace do |trace_path| + next unless defined?(Allure) + + Allure.add_attachment( + name: "trace - #{example.description}", + source: File.read(trace_path), + type: 'application/zip', + test_case: true, + ) + end end end