...
Playwright Offers API to write custom test reports. A custom reporter is a class that implements specific methods to handle events during the test run. Playwright Reporter API can be implemented and the native reporter methods of API can overridden based on needs. Below is an example:
PlaywrightCustomReporter.js
Code Block | ||
---|---|---|
| ||
class MyCustomReporter {
onBegin(config, suite) {
console.log(`Starting the test run with ${suite.allTests().length} tests`);
}
onSuiteBegin(suite) {
console.log(`Starting suite: ${suite.title}`);
}
onSuiteEnd(suite) {
console.log(`Finished suite: ${suite.title}`);
}
onTestBegin(test) {
console.log(`Starting test: ${test.title}`);
}
onTestEnd(test, result) {
console.log(`Finished test: ${test.title} with status ${result.status}`);
if (result.error) {
console.error(`Error: ${result.error.message}`);
}
}
onTestFailure(test, result) {
console.error(`Test failed: ${test.title}`);
console.error(`Error: ${result.error.message}`);
}
onTestSuccess(test, result) {
console.log(`Test passed: ${test.title}`);
}
onTestTimeout(test) {
console.warn(`Test timed out: ${test.title}`);
}
// Called when a test's retry count is exhausted
onTestRetry(test) {
console.warn(`Test retried: ${test.title}`);
}
onEnd(result) {
console.log(`Test run finished with status ${result.status}`);
console.log(`Total tests: ${result.totalTests}`);
console.log(`Passed: ${result.passedTests}`);
console.log(`Failed: ${result.failedTests}`);
}
onStdOut(data) {
process.stdout.write(data);
}
onStdErr(data) {
process.stderr.write(data);
}
onStepBegin(test, step) {
console.log('Step Begin:');
console.log('Test:', test);
console.log('Step:', step);
}
onStepEnd(test, step, result) {
console.log('Step End:');
console.log('Test:', test);
console.log('Step:', step);
console.log('Result:', result);
}
}
export default MyCustomReporter;
|
This code defines a custom reporter class MyCustomReporter
that implements various methods to track and log the progress of a test run. It's designed to be used with a testing framework, likely Playwright, where it provides detailed logs at different stages of the test execution process.
Here’s a brief explanation of each method:
onBegin(config, suite)
: Logs the start of the test run, including the number of tests in the suite.onSuiteBegin(suite)
: Logs the start of a test suite.onSuiteEnd(suite)
: Logs the end of a test suite.onTestBegin(test)
: Logs the start of an individual test.onTestEnd(test, result)
: Logs the end of an individual test, including its status and any errors if they occurred.onTestFailure(test, result)
: Logs detailed information about a failed test.onTestSuccess(test, result)
: Logs a message when a test passes.onTestTimeout(test)
: Logs when a test times out.onTestRetry(test)
: Logs when a test is retried.onEnd(result)
: Logs the summary of the test run, including the status and counts of total, passed, and failed tests.onStdOut(data)
: Writes stdout output to the console.onStdErr(data)
: Writes stderr output to the console.onStepBegin(test, step)
: Logs the start of a test step, along with the test and step details.onStepEnd(test, step, result)
: Logs the end of a test step, including the result of the step.
There are primarily two ways to use the Custom Reporter API.
Define in playwright.config.js/ts
Code Block | ||
---|---|---|
| ||
import { defineConfig } from '@playwright/test';
export default defineConfig({
globalTeardown: './global-teardown',
}); |
Passing the parameter while executing the test command
Code Block | ||
---|---|---|
| ||
import {exec} from "child_process";
exec("npx playwright test --reporter="./myreporter/PlaywrightCustomReporter.js"") |