What is the process for implementing custom reporters in Playwright to stream real-time metrics to platform analytics tools?
Asked by The SDET Playbook
Asked Sep 28, 2026Viewed 0 times
What is the process for implementing custom reporters in Playwright to stream real-time metrics to platform analytics tools?
Asked by The SDET Playbook
Sign in to answer and to vote.
Write a class that implements Playwright's Reporter interface, and register it in the config. Playwright calls onTestEnd after each test but doesn't wait for it, while it does wait for an async onEnd, so start the requests as tests finish and make sure they have all settled before the run ends:
import type { FullResult, Reporter, TestCase, TestResult } from '@playwright/test/reporter';
class TelemetryReporter implements Reporter {
private pending: Promise<unknown>[] = [];
onTestEnd(test: TestCase, result: TestResult) {
const payload = {
title: test.titlePath().join(' › '),
status: result.status,
durationMs: result.duration,
retry: result.retry,
error: result.error?.message ?? null,
};
this.pending.push(
fetch(process.env.TELEMETRY_URL!, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(payload),
}).catch((error) => console.error('telemetry failed', error)),
);
}
async onEnd(result: FullResult) {
await Promise.allSettled(this.pending);
}
}
export default TelemetryReporter;
// playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: [['list'], ['./reporters/telemetry-reporter.ts']],
});
Keep a normal reporter next to it, so a telemetry outage never hides the results, and catch network errors so they can't fail the run. For large suites, batch payloads instead of sending one request per test.
Sources: Playwright reporters, Reporter API