From 8819e015f742d8b247b304a5c8b4c5f15275593a Mon Sep 17 00:00:00 2001 From: Yuki Nishijima Date: Wed, 16 Aug 2023 15:40:52 +0900 Subject: [PATCH] Add helper methods for starting/stopping trace --- lib/capybara/playwright/driver_extension.rb | 27 ++++++++++++ spec/feature/tracing_spec.rb | 47 +++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 spec/feature/tracing_spec.rb diff --git a/lib/capybara/playwright/driver_extension.rb b/lib/capybara/playwright/driver_extension.rb index 25a803e..5367df4 100644 --- a/lib/capybara/playwright/driver_extension.rb +++ b/lib/capybara/playwright/driver_extension.rb @@ -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 diff --git a/spec/feature/tracing_spec.rb b/spec/feature/tracing_spec.rb new file mode 100644 index 0000000..a9d41dc --- /dev/null +++ b/spec/feature/tracing_spec.rb @@ -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 + + + 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