2021-07-23 04:56:36 +02:00
# class: Reporter
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
* langs: js
Test runner notifies the reporter about various events during test execution. All methods of the reporter are optional.
2021-08-06 23:02:41 +02:00
You can create a custom reporter by implementing a class with some of the reporter methods. Make sure to export this class as default.
2021-07-23 04:56:36 +02:00
2023-05-10 23:30:51 +02:00
```js tab=js-js title="my-awesome-reporter.js"
2021-07-23 04:56:36 +02:00
// @ts -check
/** @implements {import('@playwright/test/reporter').Reporter} */
class MyReporter {
2023-02-24 12:29:08 +01:00
constructor(options) {
console.log(`my-awesome-reporter setup with customOption set to ${options.customOption}`);
}
2021-07-23 04:56:36 +02:00
onBegin(config, suite) {
console.log(`Starting the run with ${suite.allTests().length} tests`);
}
onTestBegin(test) {
console.log(`Starting test ${test.title}`);
}
onTestEnd(test, result) {
console.log(`Finished test ${test.title}: ${result.status}`);
}
onEnd(result) {
console.log(`Finished the run: ${result.status}`);
}
}
module.exports = MyReporter;
```
2023-05-10 23:30:51 +02:00
```js tab=js-ts title="my-awesome-reporter.ts"
2023-08-02 11:23:47 +02:00
import type {
Reporter, FullConfig, Suite, TestCase, TestResult, FullResult
} from '@playwright/test/reporter';
2021-07-23 04:56:36 +02:00
class MyReporter implements Reporter {
2023-03-16 19:01:15 +01:00
constructor(options: { customOption?: string } = {}) {
2023-02-24 12:29:08 +01:00
console.log(`my-awesome-reporter setup with customOption set to ${options.customOption}`);
}
2023-01-28 18:38:42 +01:00
onBegin(config: FullConfig, suite: Suite) {
2021-07-23 04:56:36 +02:00
console.log(`Starting the run with ${suite.allTests().length} tests`);
}
2023-01-28 18:38:42 +01:00
onTestBegin(test: TestCase) {
2021-07-23 04:56:36 +02:00
console.log(`Starting test ${test.title}`);
}
2023-01-28 18:38:42 +01:00
onTestEnd(test: TestCase, result: TestResult) {
2021-07-23 04:56:36 +02:00
console.log(`Finished test ${test.title}: ${result.status}`);
}
2023-01-28 18:38:42 +01:00
onEnd(result: FullResult) {
2021-07-23 04:56:36 +02:00
console.log(`Finished the run: ${result.status}`);
}
}
export default MyReporter;
```
2022-03-25 19:30:45 +01:00
Now use this reporter with [`property: TestConfig.reporter`]. Learn more about [using reporters ](../test-reporters.md ).
2021-07-23 04:56:36 +02:00
2023-05-10 18:38:12 +02:00
```js title="playwright.config.ts"
2023-01-12 22:12:02 +01:00
import { defineConfig } from '@playwright/test';
2021-07-23 04:56:36 +02:00
2023-01-12 22:12:02 +01:00
export default defineConfig({
2023-02-24 12:29:08 +01:00
reporter: ['./my-awesome-reporter.ts', { customOption: 'some value' }],
2023-01-12 22:12:02 +01:00
});
2021-07-23 04:56:36 +02:00
```
2021-09-16 00:29:06 +02:00
Here is a typical order of reporter calls:
* [`method: Reporter.onBegin`] is called once with a root suite that contains all other suites and tests. Learn more about [suites hierarchy][Suite].
* [`method: Reporter.onTestBegin`] is called for each test run. It is given a [TestCase] that is executed, and a [TestResult] that is almost empty. Test result will be populated while the test runs (for example, with steps and stdio) and will get final `status` once the test finishes.
* [`method: Reporter.onStepBegin`] and [`method: Reporter.onStepEnd`] are called for each executed step inside the test. When steps are executed, test run has not finished yet.
* [`method: Reporter.onTestEnd`] is called when test run has finished. By this time, [TestResult] is complete and you can use [`property: TestResult.status`], [`property: TestResult.error`] and more.
* [`method: Reporter.onEnd`] is called once after all tests that should run had finished.
2023-04-04 19:50:40 +02:00
* [`method: Reporter.onExit`] is called immediately before the test runner exits.
2021-09-16 00:29:06 +02:00
Additionally, [`method: Reporter.onStdOut`] and [`method: Reporter.onStdErr`] are called when standard output is produced in the worker process, possibly during a test execution,
and [`method: Reporter.onError`] is called when something went wrong outside of the test execution.
2021-07-23 04:56:36 +02:00
2022-11-28 19:32:01 +01:00
If your custom reporter does not print anything to the terminal, implement [`method: Reporter.printsToStdio`] and return `false` . This way, Playwright will use one of the standard terminal reporters in addition to your custom reporter to enhance user experience.
2022-04-08 03:51:05 +02:00
## optional method: Reporter.onBegin
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
Called once before running tests. All tests have been already discovered and put into a hierarchy of [Suite]s.
### param: Reporter.onBegin.config
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
- `config` < [TestConfig]>
Resolved configuration.
### param: Reporter.onBegin.suite
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
- `suite` < [Suite]>
The root suite that contains all projects, files and test cases.
2022-04-08 03:51:05 +02:00
## optional async method: Reporter.onEnd
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
2023-08-17 00:40:14 +02:00
Called after all tests have been run, or testing has been interrupted. Note that this method may return a [Promise] and Playwright Test will await it.
2021-07-23 04:56:36 +02:00
### param: Reporter.onEnd.result
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
- `result` < [Object]>
2023-09-12 03:17:49 +02:00
- `status` < [FullStatus]< "passed"|"failed"|"timedout"|"interrupted">> Test run status.
- `startTime` < [Date]> Test run start wall time.
- `duration` < [int]> Test run duration in milliseconds.
2021-07-23 04:56:36 +02:00
2023-09-12 03:17:49 +02:00
Result of the full test run, `status` can be one of:
2021-07-23 04:56:36 +02:00
* `'passed'` - Everything went as expected.
* `'failed'` - Any test has failed.
* `'timedout'` - The [`property: TestConfig.globalTimeout`] has been reached.
* `'interrupted'` - Interrupted by the user.
2022-04-08 03:51:05 +02:00
## optional method: Reporter.onError
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
Called on some global error, for example unhandled exception in the worker process.
### param: Reporter.onError.error
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
- `error` < [TestError]>
The error.
2023-04-04 19:50:40 +02:00
## optional async method: Reporter.onExit
* since: v1.33
Called immediately before test runner exists. At this point all the reporters
2023-06-04 18:04:42 +02:00
have received the [`method: Reporter.onEnd`] signal, so all the reports should
2023-04-04 19:50:40 +02:00
be build. You can run the code that uploads the reports in this hook.
2021-07-23 04:56:36 +02:00
2022-04-08 03:51:05 +02:00
## optional method: Reporter.onStdErr
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
Called when something has been written to the standard error in the worker process.
### param: Reporter.onStdErr.chunk
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
- `chunk` < [string]|[Buffer]>
Output chunk.
### param: Reporter.onStdErr.test
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
- `test` < [void]|[TestCase]>
2021-12-15 19:39:49 +01:00
Test that was running. Note that output may happen when no test is running, in which case this will be [void].
2021-07-23 04:56:36 +02:00
2021-08-03 02:17:20 +02:00
### param: Reporter.onStdErr.result
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-08-03 02:17:20 +02:00
- `result` < [void]|[TestResult]>
Result of the test run, this object gets populated while the test runs.
2021-07-23 04:56:36 +02:00
2022-04-08 03:51:05 +02:00
## optional method: Reporter.onStdOut
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
Called when something has been written to the standard output in the worker process.
### param: Reporter.onStdOut.chunk
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
- `chunk` < [string]|[Buffer]>
Output chunk.
### param: Reporter.onStdOut.test
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
- `test` < [void]|[TestCase]>
2021-12-15 19:39:49 +01:00
Test that was running. Note that output may happen when no test is running, in which case this will be [void].
2021-07-23 04:56:36 +02:00
2021-08-03 02:17:20 +02:00
### param: Reporter.onStdOut.result
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-08-03 02:17:20 +02:00
- `result` < [void]|[TestResult]>
Result of the test run, this object gets populated while the test runs.
2022-04-08 03:51:05 +02:00
## optional method: Reporter.onStepBegin
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-08-03 02:17:20 +02:00
Called when a test step started in the worker process.
### param: Reporter.onStepBegin.test
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-08-03 02:17:20 +02:00
- `test` < [TestCase]>
2021-12-15 19:39:49 +01:00
Test that the step belongs to.
2021-08-03 02:17:20 +02:00
### param: Reporter.onStepBegin.result
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-08-03 02:17:20 +02:00
- `result` < [TestResult]>
Result of the test run, this object gets populated while the test runs.
### param: Reporter.onStepBegin.step
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-08-25 04:48:28 +02:00
- `step` < [TestStep]>
2021-08-03 02:17:20 +02:00
2021-12-15 19:39:49 +01:00
Test step instance that has started.
2021-07-23 04:56:36 +02:00
2022-04-08 03:51:05 +02:00
## optional method: Reporter.onStepEnd
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-08-03 02:17:20 +02:00
Called when a test step finished in the worker process.
### param: Reporter.onStepEnd.test
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-08-03 02:17:20 +02:00
- `test` < [TestCase]>
2021-12-15 19:39:49 +01:00
Test that the step belongs to.
2021-08-03 02:17:20 +02:00
### param: Reporter.onStepEnd.result
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-08-03 02:17:20 +02:00
- `result` < [TestResult]>
Result of the test run.
### param: Reporter.onStepEnd.step
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-08-25 04:48:28 +02:00
- `step` < [TestStep]>
2021-08-03 02:17:20 +02:00
2021-12-15 19:39:49 +01:00
Test step instance that has finished.
2021-07-23 04:56:36 +02:00
2022-04-08 03:51:05 +02:00
## optional method: Reporter.onTestBegin
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
Called after a test has been started in the worker process.
### param: Reporter.onTestBegin.test
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
- `test` < [TestCase]>
Test that has been started.
2021-08-03 02:17:20 +02:00
### param: Reporter.onTestBegin.result
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-08-03 02:17:20 +02:00
- `result` < [TestResult]>
Result of the test run, this object gets populated while the test runs.
2021-07-23 04:56:36 +02:00
2022-04-08 03:51:05 +02:00
## optional method: Reporter.onTestEnd
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
Called after a test has been finished in the worker process.
### param: Reporter.onTestEnd.test
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
- `test` < [TestCase]>
Test that has been finished.
### param: Reporter.onTestEnd.result
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-07-23 04:56:36 +02:00
- `result` < [TestResult]>
Result of the test run.
2021-11-03 16:25:16 +01:00
2022-04-08 03:51:05 +02:00
## optional method: Reporter.printsToStdio
2022-07-06 02:24:50 +02:00
* since: v1.10
2021-11-03 16:25:16 +01:00
- returns: < [boolean]>
2022-11-28 19:32:01 +01:00
Whether this reporter uses stdio for reporting. When it does not, Playwright Test could add some output to enhance user experience. If your reporter does not print to the terminal, it is strongly recommended to return `false` .