Skip to content

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

feat(json report): expose startTime and duration (#27290)

Dmitry Gozman66eb3043f5bb
diff --git a/packages/playwright/src/reporters/json.ts b/packages/playwright/src/reporters/json.ts
index 1a20225812..f0bb1ecbb7 100644
--- a/packages/playwright/src/reporters/json.ts
+++ b/packages/playwright/src/reporters/json.ts
@@ -55,10 +55,10 @@ class JSONReporter extends EmptyReporter {
}
override async onEnd(result: FullResult) {
- await outputReport(this._serializeReport(), this.config, this._outputFile);
+ await outputReport(this._serializeReport(result), this.config, this._outputFile);
}
- private _serializeReport(): JSONReport {
+ private _serializeReport(result: FullResult): JSONReport {
return {
config: {
...removePrivateFields(this.config),
@@ -79,7 +79,11 @@ class JSONReporter extends EmptyReporter {
})
},
suites: this._mergeSuites(this.suite.suites),
- errors: this._errors
+ errors: this._errors,
+ stats: {
+ startTime: result.startTime.toISOString(),
+ duration: result.duration,
+ },
};
}
@@ -194,7 +198,7 @@ class JSONReporter extends EmptyReporter {
stderr: result.stderr.map(s => stdioEntry(s)),
retry: result.retry,
steps: steps.length ? steps.map(s => this._serializeTestStep(s)) : undefined,
- startTime: result.startTime,
+ startTime: result.startTime.toISOString(),
attachments: result.attachments.map(a => ({
name: a.name,
contentType: a.contentType,
diff --git a/packages/playwright/types/testReporter.d.ts b/packages/playwright/types/testReporter.d.ts
index 42211ef9b5..0b3a472e08 100644
--- a/packages/playwright/types/testReporter.d.ts
+++ b/packages/playwright/types/testReporter.d.ts
@@ -504,6 +504,10 @@ export interface JSONReport {
};
suites: JSONReportSuite[];
errors: TestError[];
+ stats: {
+ startTime: string; // Date in ISO 8601 format.
+ duration: number; // In milliseconds;
+ }
}
export interface JSONReportSuite {
@@ -551,7 +555,7 @@ export interface JSONReportTestResult {
stderr: JSONReportSTDIOEntry[];
retry: number;
steps?: JSONReportTestStep[];
- startTime: Date;
+ startTime: string; // Date in ISO 8601 format.
attachments: {
name: string;
path?: string;
diff --git a/tests/playwright-test/reporter-json.spec.ts b/tests/playwright-test/reporter-json.spec.ts
index b8775cf669..2148c2cfe0 100644
--- a/tests/playwright-test/reporter-json.spec.ts
+++ b/tests/playwright-test/reporter-json.spec.ts
@@ -63,7 +63,7 @@ test('should not report skipped due to sharding', async ({ runInlineTest }) => {
expect(result.report.suites[0].specs[1].tests[0].status).toBe('skipped');
});
-test('should report projects', async ({ runInlineTest }, testInfo) => {
+test('should report projects and stats', async ({ runInlineTest }, testInfo) => {
const result = await runInlineTest({
'playwright.config.ts': `
module.exports = {
@@ -107,6 +107,9 @@ test('should report projects', async ({ runInlineTest }, testInfo) => {
expect(result.report.suites[0].specs[0].tests[0].projectName).toBe('p1');
expect(result.report.suites[0].specs[0].tests[1].projectName).toBe('p2');
+
+ expect(new Date(result.report.stats.startTime).valueOf()).not.toBeNaN();
+ expect(result.report.stats.duration).toEqual(expect.any(Number));
});
test('should show steps', async ({ runInlineTest }) => {
diff --git a/utils/generate_types/overrides-testReporter.d.ts b/utils/generate_types/overrides-testReporter.d.ts
index 61a20f42fa..fe371001af 100644
--- a/utils/generate_types/overrides-testReporter.d.ts
+++ b/utils/generate_types/overrides-testReporter.d.ts
@@ -75,6 +75,10 @@ export interface JSONReport {
};
suites: JSONReportSuite[];
errors: TestError[];
+ stats: {
+ startTime: string; // Date in ISO 8601 format.
+ duration: number; // In milliseconds;
+ }
}
export interface JSONReportSuite {
@@ -122,7 +126,7 @@ export interface JSONReportTestResult {
stderr: JSONReportSTDIOEntry[];
retry: number;
steps?: JSONReportTestStep[];
- startTime: Date;
+ startTime: string; // Date in ISO 8601 format.
attachments: {
name: string;
path?: string;