Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(reporter): Add `parallelIndex` field to `TestResult` (#19570)

Vladimir Semenov467d9f37fc3a
diff --git a/docs/src/test-reporter-api/class-testresult.md b/docs/src/test-reporter-api/class-testresult.md
index f540c5e697..7674ac333d 100644
--- a/docs/src/test-reporter-api/class-testresult.md
+++ b/docs/src/test-reporter-api/class-testresult.md
@@ -78,3 +78,9 @@ List of steps inside this test run.
Index of the worker where the test was run. If the test was not run a single time, for example when the user interrupted testing, the only result will have a `workerIndex` equal to `-1`.
Learn more about [parallelism and sharding](../test-parallel.md) with Playwright Test.
+
+## property: TestResult.parallelIndex
+* since: v1.30
+- type: <[int]>
+
+The index of the worker between `0` and `workers - 1`. It is guaranteed that workers running at the same time have a different `parallelIndex`.
diff --git a/packages/playwright-test/src/dispatcher.ts b/packages/playwright-test/src/dispatcher.ts
index cced1404fd..0ff95225db 100644
--- a/packages/playwright-test/src/dispatcher.ts
+++ b/packages/playwright-test/src/dispatcher.ts
@@ -199,6 +199,7 @@ export class Dispatcher {
const data = this._testById.get(params.testId)!;
const result = data.test._appendTestResult();
data.resultByWorkerIndex.set(worker.workerIndex, { result, stepStack: new Set(), steps: new Map() });
+ result.parallelIndex = worker.parallelIndex;
result.workerIndex = worker.workerIndex;
result.startTime = new Date(params.startWallTime);
this._reporter.onTestBegin?.(data.test, result);
@@ -495,7 +496,7 @@ let lastWorkerIndex = 0;
class Worker extends EventEmitter {
private process: child_process.ChildProcess;
private _hash: string;
- private parallelIndex: number;
+ readonly parallelIndex: number;
readonly workerIndex: number;
private _didSendStop = false;
private _didFail = false;
diff --git a/packages/playwright-test/src/test.ts b/packages/playwright-test/src/test.ts
index 594b6df42a..cb490f4edf 100644
--- a/packages/playwright-test/src/test.ts
+++ b/packages/playwright-test/src/test.ts
@@ -212,6 +212,7 @@ export class TestCase extends Base implements reporterTypes.TestCase {
_appendTestResult(): reporterTypes.TestResult {
const result: reporterTypes.TestResult = {
retry: this.results.length,
+ parallelIndex: -1,
workerIndex: -1,
duration: 0,
startTime: new Date(),
diff --git a/packages/playwright-test/types/testReporter.d.ts b/packages/playwright-test/types/testReporter.d.ts
index 76af5e52b1..f29a4e4b21 100644
--- a/packages/playwright-test/types/testReporter.d.ts
+++ b/packages/playwright-test/types/testReporter.d.ts
@@ -282,7 +282,13 @@ export interface TestResult {
*
* Learn more about [parallelism and sharding](https://playwright.dev/docs/test-parallel) with Playwright Test.
*/
- workerIndex: number;}
+ workerIndex: number;
+
+ /**
+ * The index of the worker between `0` and `workers - 1`. It is guaranteed that workers running at the same time have
+ * a different `parallelIndex`.
+ */
+ parallelIndex: number;}
/**
* Result of the full test run.
diff --git a/tests/playwright-test/reporter.spec.ts b/tests/playwright-test/reporter.spec.ts
index b424923368..264265db22 100644
--- a/tests/playwright-test/reporter.spec.ts
+++ b/tests/playwright-test/reporter.spec.ts
@@ -607,6 +607,29 @@ var import_test = __toModule(require("@playwright/test"));
expect(result.output).toContain('a.spec.ts');
});
+test('parallelIndex is presented in onTestEnd', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'reporter.ts': `
+ class Reporter {
+ onTestEnd(test, result) {
+ console.log('parallelIndex: ' + result.parallelIndex)
+ }
+ }
+ module.exports = Reporter;`,
+ 'playwright.config.ts': `
+ module.exports = {
+ reporter: './reporter',
+ };
+ `,
+ 'a.spec.js': `
+ pwt.test('test', () => {});
+ `,
+ }, { 'reporter': '', 'workers': 1 });
+
+ expect(result.output).toContain('parallelIndex: 0');
+});
+
+
function stripEscapedAscii(str: string) {
return str.replace(/\\u00[a-z0-9][a-z0-9]\[[^m]+m/g, '');
}