Class: Playwright::Tracing

Inherits:
PlaywrightApi show all
Defined in:
lib/playwright_api/tracing.rb,
sig/playwright.rbs

Overview

API for collecting and saving Playwright traces. Playwright traces can be opened in Trace Viewer after Playwright script runs.

NOTE: You probably want to enable tracing in your config file instead of using context.tracing.

The context.tracing API captures browser operations and network activity, but it doesn't record test assertions (like expect calls). We recommend enabling tracing through Playwright Test configuration, which includes those assertions and provides a more complete trace for debugging test failures.

Start recording a trace before performing actions. At the end, stop tracing and save it to a file.

browser = chromium.launch()
context = browser.new_context()
context.tracing.start(screenshots=True, snapshots=True)
page = context.new_page()
page.goto("https://playwright.dev")
context.tracing.stop(path = "trace.zip")

Instance Method Summary collapse

Methods inherited from PlaywrightApi

#initialize, unwrap, wrap

Constructor Details

This class inherits a constructor from Playwright::PlaywrightApi

Instance Method Details

#group(name, location: nil) ⇒ Object

NOTE: Use test.step instead when available.

Creates a new group within the trace, assigning any subsequent API calls to this group, until [method: Tracing.groupEnd] is called. Groups can be nested and will be visible in the trace viewer.

Usage

# All actions between group and group_end
# will be shown in the trace viewer as a group.
page.context.tracing.group("Open Playwright.dev > API")
page.goto("https://playwright.dev/")
page.get_by_role("link", name="API").click()
page.context.tracing.group_end()

Parameters:

  • name (String)
  • location: (Hash[untyped, untyped]) (defaults to: nil)

Returns:

  • (Object)


104
105
106
# File 'lib/playwright_api/tracing.rb', line 104

def group(name, location: nil)
  wrap_impl(@impl.group(unwrap_impl(name), location: unwrap_impl(location)))
end

#group_endvoid

This method returns an undefined value.

Closes the last group created by [method: Tracing.group].



110
111
112
# File 'lib/playwright_api/tracing.rb', line 110

def group_end
  wrap_impl(@impl.group_end)
end

#off(event, callback) ⇒ Object

-- inherited from EventEmitter --



134
135
136
# File 'lib/playwright_api/tracing.rb', line 134

def off(event, callback)
  event_emitter_proxy.off(event, callback)
end

#on(event, callback) ⇒ Object

-- inherited from EventEmitter --



146
147
148
# File 'lib/playwright_api/tracing.rb', line 146

def on(event, callback)
  event_emitter_proxy.on(event, callback)
end

#once(event, callback) ⇒ Object

-- inherited from EventEmitter --



140
141
142
# File 'lib/playwright_api/tracing.rb', line 140

def once(event, callback)
  event_emitter_proxy.once(event, callback)
end

#start(ariaSnapshots: nil, live: nil, name: nil, screenshots: nil, screenSnapshots: nil, snapshots: nil, sources: nil, title: nil) ⇒ void

This method returns an undefined value.

Start tracing.

NOTE: You probably want to enable tracing in your config file instead of using Tracing.start.

The context.tracing API captures browser operations and network activity, but it doesn't record test assertions (like expect calls). We recommend enabling tracing through Playwright Test configuration, which includes those assertions and provides a more complete trace for debugging test failures.

Usage

context.tracing.start(screenshots=True, snapshots=True)
page = context.new_page()
page.goto("https://playwright.dev")
context.tracing.stop(path = "trace.zip")

Parameters:

  • ariaSnapshots: (Boolean) (defaults to: nil)
  • live: (Boolean) (defaults to: nil)
  • name: (String) (defaults to: nil)
  • screenshots: (Boolean) (defaults to: nil)
  • screenSnapshots: (Boolean) (defaults to: nil)
  • snapshots: (Boolean) (defaults to: nil)
  • sources: (Boolean) (defaults to: nil)
  • title: (String) (defaults to: nil)


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/playwright_api/tracing.rb', line 36

def start(
      ariaSnapshots: nil,
      live: nil,
      name: nil,
      screenshots: nil,
      screenSnapshots: nil,
      snapshots: nil,
      sources: nil,
      title: nil)
  wrap_impl(@impl.start(ariaSnapshots: unwrap_impl(ariaSnapshots), live: unwrap_impl(live), name: unwrap_impl(name), screenshots: unwrap_impl(screenshots), screenSnapshots: unwrap_impl(screenSnapshots), snapshots: unwrap_impl(snapshots), sources: unwrap_impl(sources), title: unwrap_impl(title)))
end

#start_chunk(name: nil, title: nil) ⇒ void

This method returns an undefined value.

Start a new trace chunk. If you'd like to record multiple traces on the same BrowserContext, use [method: Tracing.start] once, and then create multiple trace chunks with [method: Tracing.startChunk] and [method: Tracing.stopChunk].

Usage

context.tracing.start(screenshots=True, snapshots=True)
page = context.new_page()
page.goto("https://playwright.dev")

context.tracing.start_chunk()
page.get_by_text("Get Started").click()
# Everything between start_chunk and stop_chunk will be recorded in the trace.
context.tracing.stop_chunk(path = "trace1.zip")

context.tracing.start_chunk()
page.goto("http://example.com")
# Save a second trace file with different actions.
context.tracing.stop_chunk(path = "trace2.zip")

Parameters:

  • name: (String) (defaults to: nil)
  • title: (String) (defaults to: nil)


68
69
70
# File 'lib/playwright_api/tracing.rb', line 68

def start_chunk(name: nil, title: nil)
  wrap_impl(@impl.start_chunk(name: unwrap_impl(name), title: unwrap_impl(title)))
end

#start_har(path, content: nil, mode: nil, urlFilter: nil) ⇒ Object

Start recording a HAR (HTTP Archive) of network activity in this context. The HAR file is written to disk when [method: Tracing.stopHar] is called, or when the returned Disposable is disposed.

Only one HAR recording can be active at a time per Tracing instance.

Usage

context.tracing.start_har("trace.har")
page = context.new_page()
page.goto("https://playwright.dev")
context.tracing.stop_har()

Parameters:

  • path (String, File)
  • content: ("omit", "embed", "attach") (defaults to: nil)
  • mode: ("full", "minimal") (defaults to: nil)
  • urlFilter: (String, Regexp) (defaults to: nil)

Returns:

  • (Object)


85
86
87
# File 'lib/playwright_api/tracing.rb', line 85

def start_har(path, content: nil, mode: nil, urlFilter: nil)
  wrap_impl(@impl.start_har(unwrap_impl(path), content: unwrap_impl(content), mode: unwrap_impl(mode), urlFilter: unwrap_impl(urlFilter)))
end

#stop(path: nil) ⇒ void

This method returns an undefined value.

Stop tracing.

Parameters:

  • path: (String, File) (defaults to: nil)


116
117
118
# File 'lib/playwright_api/tracing.rb', line 116

def stop(path: nil)
  wrap_impl(@impl.stop(path: unwrap_impl(path)))
end

#stop_chunk(path: nil) ⇒ void

This method returns an undefined value.

Stop the trace chunk. See [method: Tracing.startChunk] for more details about multiple trace chunks.

Parameters:

  • path: (String, File) (defaults to: nil)


122
123
124
# File 'lib/playwright_api/tracing.rb', line 122

def stop_chunk(path: nil)
  wrap_impl(@impl.stop_chunk(path: unwrap_impl(path)))
end

#stop_harvoid

This method returns an undefined value.

Stop HAR recording and save the HAR file to the path given to [method: Tracing.startHar].



128
129
130
# File 'lib/playwright_api/tracing.rb', line 128

def stop_har
  wrap_impl(@impl.stop_har)
end