Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper methods for starting/stopping trace #66

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Owner

@YusukeIwaki YusukeIwaki Nov 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuki24 I prefer the method name with_tracing for this. Do you have any ideas about this?

Copy link
Contributor Author

@yuki24 yuki24 Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the name with implies that the word tracing is a thing, rather than being an action. For example, all the methods prefixed with with_ in Rails follow the form of with_thing, rather than with_doing:

スクリーンショット 2023-11-13 10 31 33

Whereas other methods, such as #draw, #transaction, and #freeze_time, all trigger an explicit action under the hood, and the execution in the block is in effect of the action you would like to take. I believe this case is closer to the latter.

So based on this assessment, I would think that the name #trace is more intuitive than #with_tracing.

With that all said, this is ultimately your call. Let me know what you think.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your kind advices.
I didn't know well about the Rails existing method names and its rules, and your advices really helped me.

Let's keep the method name 'with_trace' here.

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
Loading