Skip to content

Commit

Permalink
Add helper methods for starting/stopping trace
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki24 committed Nov 10, 2023
1 parent c6287e6 commit 8819e01
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/capybara/playwright/driver_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,33 @@ def with_playwright_page(&block)

@browser&.with_playwright_page(&block)
end

# Start Playwright tracing (doc: https://playwright.dev/docs/api/class-tracing#tracing-start)
def start_tracing(name: nil, screenshots: false, snapshots: false, sources: false, title: nil)
# Ensure playwright page is initialized.
browser

with_playwright_page do |playwright_page|
playwright_page.context.tracing.start(name: name, screenshots: screenshots, snapshots: snapshots, sources: sources, title: title)
end
end

# Stop Playwright tracing (doc: https://playwright.dev/docs/api/class-tracing#tracing-stop)
def stop_tracing(path: nil)
with_playwright_page do |playwright_page|
playwright_page.context.tracing.stop(path: path)
end
end

# Trace execution of the given block. The tracing is automatically stopped when the block is finished.
def trace(name: nil, screenshots: false, snapshots: false, sources: false, title: nil, path: nil, &block)
raise ArgumentError.new('block must be given') unless block

start_tracing(name: name, screenshots: screenshots, snapshots: snapshots, sources: sources, title: title)
block.call
ensure
stop_tracing(path: path)
end
end
end
end
47 changes: 47 additions & 0 deletions spec/feature/tracing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'spec_helper'

RSpec.describe 'tracing', sinatra: true do
TRACES_DIR = 'tmp/capybara/playwright'.freeze

before do
FileUtils.rm_rf(TRACES_DIR)

sinatra.get '/' do
<<~HTML
<!DOCTYPE html>
<button>Go</button>
HTML
end
end

it 'can start and stop tracing' do
page.driver.start_tracing(name: "test_trace", screenshots: true, snapshots: true, sources: true, title: "test_trace")

visit '/'
click_on 'Go'
expect(page).to have_content('Go')

page.driver.stop_tracing(path: "#{TRACES_DIR}/test_trace.zip")

expect(File).to exist("#{TRACES_DIR}/test_trace.zip")
end

it 'can enable tracing only in the block' do
page.driver.trace name: "test_trace_with_block", screenshots: true, snapshots: true, sources: true, title: "title", path: "#{TRACES_DIR}/test_trace_with_block.zip" do
visit '/'
click_on 'Go'
expect(page).to have_content('Go')
end

expect(File).to exist("#{TRACES_DIR}/test_trace_with_block.zip")
end

it 'does not start tracing when no block is given' do
expect { page.driver.trace }.to raise_error(ArgumentError)

expect {
page.driver.start_tracing
page.driver.stop_tracing
}.not_to raise_error(Playwright::Error, /Tracing has been already started/)
end
end

0 comments on commit 8819e01

Please sign in to comment.