Skip to main content

Tracing

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 browser = chromium.launch();
BrowserContext context = browser.newContext();
context.tracing().start(new Tracing.StartOptions()
.setScreenshots(true)
.setSnapshots(true));
Page page = context.newPage();
page.navigate("https://playwright.dev");
context.tracing().stop(new Tracing.StopOptions()
.setPath(Paths.get("trace.zip")));

Methods​

group​

Added in: v1.49 tracing.group
caution

Use test.step instead when available.

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

Usage

// All actions between group and groupEnd
// will be shown in the trace viewer as a group.
page.context().tracing().group("Open Playwright.dev > API");
page.navigate("https://playwright.dev/");
page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("API")).click();
page.context().tracing().groupEnd();

Arguments

  • name String#

    Group name shown in the trace viewer.

  • options Tracing.GroupOptions (optional)

    • setLocation Location (optional)#

      • setFile String

      • setLine int (optional)

      • setColumn int (optional)

      Specifies a custom location for the group to be shown in the trace viewer. Defaults to the location of the Tracing.group() call.

Returns

  • [Disposable]#

groupEnd​

Added in: v1.49 tracing.groupEnd

Closes the last group created by Tracing.group().

Usage

Tracing.groupEnd();

Returns


start​

Added in: v1.12 tracing.start

Start tracing. Disposing the returned [Disposable] stops tracing without saving the trace, similarly to calling Tracing.stop() without a path.

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