Examples
Copy-pasteable examples for common testrelic-playwright scenarios. All of them rely on the auto-registered plugin — there is no conftest.py wiring in any example.
Browser E2E test
A standard Playwright test using the page fixture. The plugin records the navigation timeline, console logs, action steps, assertions, screenshots, and video automatically.
from playwright.sync_api import Page, expect
def test_homepage_loads(page: Page) -> None:
page.goto("https://example.com")
expect(page.locator("h1")).to_be_visible()
API test
Tests that drive APIRequestContext are tracked as API calls — method, URL, status, headers, bodies, and timing are captured (subject to your redaction and URL-filter settings).
from playwright.sync_api import APIRequestContext
def test_fetch_posts(playwright) -> None:
request = playwright.request.new_context()
response = request.get("https://api.example.com/posts")
assert response.status == 200
Unified browser + API test
A single test can drive the browser and make API calls — both surface in the same timeline, with API calls linked to the assertions that depend on them.
from playwright.sync_api import Page, expect
def test_create_and_view_post(page: Page, playwright) -> None:
request = playwright.request.new_context()
created = request.post(
"https://api.example.com/posts",
data={"title": "Hello"},
)
assert created.status == 201
page.goto("https://app.example.com/posts")
expect(page.get_by_text("Hello")).to_be_visible()
Large suite with streaming
For suites of hundreds of tests, switch to streaming so the report is written to disk instead of held in memory. With reportMode = auto this happens automatically once the test count reaches streamingThreshold (default 500); set it explicitly to control the behavior.
[tool.pytest.ini_options]
testrelic_options = [
"reportMode = streaming",
"streamingThreshold = 500",
]
pytest
testrelic-playwright serve ./test-results/.testrelic-report --port 9323 --open
The serve command starts a local server for the streamed .testrelic-report directory. See Reports for what the served report contains.
Sharded runs merged into one report
When you split a suite across shards, each shard writes its own timeline JSON. Merge them into a single report with the CLI:
# shard 1
pytest --testrelic-output shard-1.json
# shard 2
pytest --testrelic-output shard-2.json
# combine
testrelic-playwright merge shard-1.json shard-2.json -o merged.json
This mirrors the JS reporter's shard workflow — see Merging shards for the cross-SDK background.
Manual navigation hook
Some navigation can't be observed automatically — custom client-side routing, iframes, or other indirect transitions. Record those explicitly so they appear in the timeline:
from playwright.sync_api import Page
def test_custom_routing(page: Page) -> None:
page.goto("https://app.example.com")
page.get_by_role("button", name="Go to settings").click()
record_navigation("https://app.example.com/settings")