Skip to content

Commit

Permalink
Merge pull request #67 from YusukeIwaki/tracing_config
Browse files Browse the repository at this point in the history
Provide a simple configuration for tracing
  • Loading branch information
YusukeIwaki authored Nov 13, 2023
2 parents d3f27e0 + 1e6d19b commit 80787f3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/capybara/playwright/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/capybara/playwright/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
8 changes: 8 additions & 0 deletions lib/capybara/playwright/driver_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions spec/feature/example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 80787f3

Please sign in to comment.