Tracing
API for collecting and saving Playwright traces. Playwright traces can be opened in Trace Viewer after Playwright script runs.
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.49Use 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
-
Group name shown in the trace viewer.
-
optionsTracing.GroupOptions(optional)-
setLocationLocation (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.49Closes the last group created by Tracing.group().
Usage
Tracing.groupEnd();
Returns
start
Added in: v1.12Start tracing. Disposing the returned [Disposable] stops tracing without saving the trace, similarly to calling Tracing.stop() without a path.
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