If this is your first time setting up Playwright monitoring, we recommend starting with our Playwright Testing Essentials.
Explore documentation
Playwright monitor
Set up fast and reliable end-to-end transaction monitoring for your web apps with Playwright-powered browser checks.
Getting started
Create your first Playwright scenario:
- Navigate to Monitors → Create monitor.
- Select Alert us when: Playwright scenario fails.
- Use the following code:
With this simple test, you can make sure that your customers can sign up from your landing page on mobile. Creating Playwright scenarios for critical user flows is a great way to protect yourself from regressions and unexpected issues.
Start monitoring your landing page today
Change the URL and the sign up button text in the example test above.
How to create Playwright scenarios?
We recommend using the Playwright Codegen or ChatGPT:
Playwright Codegen: Record end-to-end tests using VSCode or Playwright Inspector. Read more in the official Playwright guide.
ChatGPT: Generate your tests with the help of AI. Go to ChatGPT and start with this query:
Using environment variables
You can use passwords, secrets, and tokens securely in your scenarios by defining them as environment variables:
- Navigate to Monitors → Select a monitor → Configure → Advanced settings → Environment variables.
- Add variable names and values as needed.
To access defined variables in your scenario, use expressions like process.env.PASSWORD:
Adding instant retries of failing test
You can avoid flaky tests using retries in Playwright:
Using Playwright to monitor APIs
You can call your API directly using fetch and use custom Javascript to verify the results:
Using console.log can provide additional info in case an incident is created.
Exporting metadata from your scenario
Your scenario can label the incident it opens by writing a JSON object to metadata.json in its working directory. The incident takes its metadata from the failing run that opens it, so later runs do not relabel it.
Naming each step before it runs labels a DNS failure, timeout or unexpected body with the step it happened in, and a passing run writes nothing. The incident then carries REASON: details_get_fail, which you can read there and filter incident reports by, like any other incident metadata.
Where and when to write the file
Write it in test.afterEach: a failing expect() throws immediately and a timed-out test is abandoned, so the end of your test and any finally block never run. afterEach still runs in both cases, though not when test.beforeAll fails.
Use the plain relative path metadata.json and write nothing beside it, which the sandbox refuses. The file survives the whole run, so with retries a later attempt that fails without setting reason ships the earlier label. A metadata.json written to the test output directory instead, through testInfo.outputPath() for example, is not read as metadata and is published as a publicly linkable artifact.
What Better Stack stores
Values are stored as text, nested objects and arrays as their JSON. A value that is empty, nested deeper than 32 levels or holding a number too large for a double is left out.
One run can export 30 keys, with keys up to 100 and values up to 1000 characters, and 4000 characters in total. Longer values are cut, later keys are dropped, and a file over 1 MB, or one that is not a JSON object, is ignored entirely.
Names are matched loosely, so Order ID, order_id and orderId count as one name. Your scenario cannot use a name your monitor already defines under Metadata, or one Better Stack sets itself: Monitor, Monitor pronounceable name, Monitor type, Monitor URL, Monitor required keyword, Origin, Escalation policy and Group. A name a Catalog relation adds is taken as well, matched exactly rather than loosely.
Metadata is never redacted, and a value can appear in the incident, its notifications and your reports, so keep secrets and personal data out of it. A few names Better Stack uses for alert details, such as Alert type, Operator and Query name, stay on the incident but never reach notifications.
Storing and querying data in Playwright scripts
Playwright monitors are stateless. For persistent storage or to react to previous states, use an HTTP Telemetry source and our SQL API.
Ingest data: Send your Playwright script's output, console logs, or custom event data as JSON events to an HTTP Telemetry source. This allows you to retain any custom data you extract, detailed records of each test run, or performance metrics.
SQL API: Leverage our SQL API to read any data you stored. You can use fetch() with FORMAT JSON to read the data directly in your Playwright monitor, enabling it to react to the previous state.
Alerts: Use the data to create custom alerting based on thresholds or detected anomalies in your Playwright script's performance or output.
Need help?
Please let us know at hello@betterstack.com.
We're happy to help! 🙏