Skip to content
Merged
1 change: 1 addition & 0 deletions docs/src/test-api/class-testinfo.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test('basic test', async ({ page }, testInfo) => {
- type: <[Array]<[Object]>>
- `type` <[string]> Annotation type, for example `'skip'` or `'fail'`.
- `description` ?<[string]> Optional description.
- `location` ?<[Location]> Optional location in the source where the annotation is added.

The list of annotations applicable to the current test. Includes annotations from the test, annotations from all [`method: Test.describe`] groups the test belongs to and file-level annotations for the test file.

Expand Down
1 change: 1 addition & 0 deletions docs/src/test-reporter-api/class-testcase.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- type: <[Array]<[Object]>>
- `type` <[string]> Annotation type, for example `'skip'` or `'fail'`.
- `description` ?<[string]> Optional description.
- `location` ?<[Location]> Optional location in the source where the annotation is added.

The list of annotations applicable to the current test. Includes:
* annotations defined on the test or suite via [`method: Test.(call)`] and [`method: Test.describe`];
Expand Down
1 change: 1 addition & 0 deletions docs/src/test-reporter-api/class-testresult.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The list of files or buffers attached during the test execution through [`proper
- type: <[Array]<[Object]>>
- `type` <[string]> Annotation type, for example `'skip'` or `'fail'`.
- `description` ?<[string]> Optional description.
- `location` ?<[Location]> Optional location in the source where the annotation is added.

The list of annotations appended during test execution. Includes:
* annotations implicitly added by methods [`method: Test.skip`], [`method: Test.fixme`] and [`method: Test.fail`] during test execution;
Expand Down
1 change: 1 addition & 0 deletions docs/src/test-reporter-api/class-teststep.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ List of steps inside this step.
- type: <[Array]<[Object]>>
- `type` <[string]> Annotation type, for example `'skip'`.
- `description` ?<[string]> Optional description.
- `location` ?<[Location]> Optional location in the source where the annotation is added.

The list of annotations applicable to the current test step.

Expand Down
3 changes: 2 additions & 1 deletion packages/html-reporter/src/testCaseView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
limitations under the License.
*/

import type { TestCase, TestAnnotation, TestCaseSummary } from './types';
import type { TestAnnotation } from '@playwright/test';
import type { TestCase, TestCaseSummary } from './types';
import * as React from 'react';
import { TabbedPane } from './tabbedPane';
import { AutoChip } from './chip';
Expand Down
4 changes: 1 addition & 3 deletions packages/html-reporter/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { Metadata } from '@playwright/test';
import type { TestAnnotation, Metadata } from '@playwright/test';

export type Stats = {
total: number;
Expand Down Expand Up @@ -59,8 +59,6 @@ export type TestFileSummary = {
stats: Stats;
};

export type TestAnnotation = { type: string, description?: string };

export type TestCaseSummary = {
testId: string,
title: string;
Expand Down
1 change: 0 additions & 1 deletion packages/playwright/src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export type FixturesWithLocation = {
fixtures: Fixtures;
location: Location;
};
export type Annotation = { type: string, description?: string };

export const defaultTimeout = 30000;

Expand Down
7 changes: 4 additions & 3 deletions packages/playwright/src/common/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
import { rootTestType } from './testType';
import { computeTestCaseOutcome } from '../isomorphic/teleReceiver';

import type { Annotation, FixturesWithLocation, FullProjectInternal } from './config';
import type { FixturesWithLocation, FullProjectInternal } from './config';
import type { FixturePool } from './fixtures';
import type { TestTypeImpl } from './testType';
import type { TestAnnotation } from '../../types/test';
import type * as reporterTypes from '../../types/testReporter';
import type { FullProject, Location } from '../../types/testReporter';

Expand Down Expand Up @@ -50,7 +51,7 @@ export class Suite extends Base {
_timeout: number | undefined;
_retries: number | undefined;
// Annotations known statically before running the test, e.g. `test.describe.skip()` or `test.describe({ annotation }, body)`.
_staticAnnotations: Annotation[] = [];
_staticAnnotations: TestAnnotation[] = [];
// Explicitly declared tags that are not a part of the title.
_tags: string[] = [];
_modifiers: Modifier[] = [];
Expand Down Expand Up @@ -252,7 +253,7 @@ export class TestCase extends Base implements reporterTypes.TestCase {

expectedStatus: reporterTypes.TestStatus = 'passed';
timeout = 0;
annotations: Annotation[] = [];
annotations: TestAnnotation[] = [];
retries = 0;
repeatEachIndex = 0;

Expand Down
21 changes: 11 additions & 10 deletions packages/playwright/src/common/testType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class TestTypeImpl {
details = fnOrDetails;
}

const validatedDetails = validateTestDetails(details);
const validatedDetails = validateTestDetails(details, location);
const test = new TestCase(title, body, this, location);
test._requireFile = suite._requireFile;
test.annotations.push(...validatedDetails.annotations);
Expand All @@ -114,9 +114,9 @@ export class TestTypeImpl {
if (type === 'only' || type === 'fail.only')
test._only = true;
if (type === 'skip' || type === 'fixme' || type === 'fail')
test.annotations.push({ type });
test.annotations.push({ type, location });
else if (type === 'fail.only')
test.annotations.push({ type: 'fail' });
test.annotations.push({ type: 'fail', location });
}

private _describe(type: 'default' | 'only' | 'serial' | 'serial.only' | 'parallel' | 'parallel.only' | 'skip' | 'fixme', location: Location, titleOrFn: string | Function, fnOrDetails?: TestDetails | Function, fn?: Function) {
Expand All @@ -143,7 +143,7 @@ export class TestTypeImpl {
body = fn!;
}

const validatedDetails = validateTestDetails(details);
const validatedDetails = validateTestDetails(details, location);
const child = new Suite(title, 'describe');
child._requireFile = suite._requireFile;
child.location = location;
Expand All @@ -158,7 +158,7 @@ export class TestTypeImpl {
if (type === 'parallel' || type === 'parallel.only')
child._parallelMode = 'parallel';
if (type === 'skip' || type === 'fixme')
child._staticAnnotations.push({ type });
child._staticAnnotations.push({ type, location });

for (let parent: Suite | undefined = suite; parent; parent = parent.parent) {
if (parent._parallelMode === 'serial' && child._parallelMode === 'parallel')
Expand Down Expand Up @@ -229,7 +229,7 @@ export class TestTypeImpl {
if (modifierArgs.length >= 1 && !modifierArgs[0])
return;
const description = modifierArgs[1];
suite._staticAnnotations.push({ type, description });
suite._staticAnnotations.push({ type, description, location });
}
return;
}
Expand All @@ -239,7 +239,7 @@ export class TestTypeImpl {
throw new Error(`test.${type}() can only be called inside test, describe block or fixture`);
if (typeof modifierArgs[0] === 'function')
throw new Error(`test.${type}() with a function can only be called inside describe block`);
testInfo[type](...modifierArgs as [any, any]);
testInfo._modifier(type, location, modifierArgs as [any, any]);
}

private _setTimeout(location: Location, timeout: number) {
Expand Down Expand Up @@ -276,7 +276,7 @@ export class TestTypeImpl {
let result: Awaited<ReturnType<typeof raceAgainstDeadline<T>>> | undefined = undefined;
result = await raceAgainstDeadline(async () => {
try {
return await step.info._runStepBody(expectation === 'skip', body);
return await step.info._runStepBody(expectation === 'skip', body, step.location);
} catch (e) {
// If the step timed out, the test fixtures will tear down, which in turn
// will abort unfinished actions in the step body. Record such errors here.
Expand Down Expand Up @@ -315,8 +315,9 @@ function throwIfRunningInsideJest() {
}
}

function validateTestDetails(details: TestDetails) {
const annotations = Array.isArray(details.annotation) ? details.annotation : (details.annotation ? [details.annotation] : []);
function validateTestDetails(details: TestDetails, location: Location) {
const originalAnnotations = Array.isArray(details.annotation) ? details.annotation : (details.annotation ? [details.annotation] : []);
const annotations = originalAnnotations.map(annotation => ({ ...annotation, location }));
const tags = Array.isArray(details.tag) ? details.tag : (details.tag ? [details.tag] : []);
for (const tag of tags) {
if (tag[0] !== '@')
Expand Down
25 changes: 16 additions & 9 deletions packages/playwright/src/isomorphic/teleReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
* limitations under the License.
*/

import type { Metadata } from '../../types/test';
import type { Metadata, TestAnnotation } from '../../types/test';
import type * as reporterTypes from '../../types/testReporter';
import type { Annotation } from '../common/config';
import type { ReporterV2 } from '../reporters/reporterV2';

export type StringIntern = (s: string) => string;
Expand Down Expand Up @@ -68,7 +67,7 @@ export type JsonTestCase = {
retries: number;
tags?: string[];
repeatEachIndex: number;
annotations?: Annotation[];
annotations?: TestAnnotation[];
};

export type JsonTestEnd = {
Expand All @@ -95,7 +94,7 @@ export type JsonTestResultEnd = {
status: reporterTypes.TestStatus;
errors: reporterTypes.TestError[];
attachments: JsonAttachment[];
annotations?: Annotation[];
annotations?: TestAnnotation[];
};

export type JsonTestStepStart = {
Expand All @@ -112,7 +111,7 @@ export type JsonTestStepEnd = {
duration: number;
error?: reporterTypes.TestError;
attachments?: number[]; // index of JsonTestResultEnd.attachments
annotations?: Annotation[];
annotations?: TestAnnotation[];
};

export type JsonFullResult = {
Expand Down Expand Up @@ -238,15 +237,15 @@ export class TeleReporterReceiver {
test.expectedStatus = testEndPayload.expectedStatus;
// Should be empty array, but if it's not, it represents all annotations for that test
if (testEndPayload.annotations.length > 0)
test.annotations = testEndPayload.annotations;
test.annotations = this._absoluteAnnotationLocations(testEndPayload.annotations);
const result = test.results.find(r => r._id === payload.id)!;
result.duration = payload.duration;
result.status = payload.status;
result.errors = payload.errors;
result.error = result.errors?.[0];
result.attachments = this._parseAttachments(payload.attachments);
if (payload.annotations)
result.annotations = payload.annotations;
result.annotations = this._absoluteAnnotationLocations(payload.annotations);
this._reporter.onTestEnd?.(test, result);
// Free up the memory as won't see these step ids.
result._stepMap = new Map();
Expand Down Expand Up @@ -376,10 +375,18 @@ export class TeleReporterReceiver {
test.location = this._absoluteLocation(payload.location);
test.retries = payload.retries;
test.tags = payload.tags ?? [];
test.annotations = payload.annotations ?? [];
test.annotations = this._absoluteAnnotationLocations(payload.annotations ?? []);
return test;
}

private _absoluteAnnotationLocations(annotations: TestAnnotation[]): TestAnnotation[] {
return annotations.map(annotation => {
if (annotation.location)
annotation.location = this._absoluteLocation(annotation.location);
return annotation;
});
}

private _absoluteLocation(location: reporterTypes.Location): reporterTypes.Location;
private _absoluteLocation(location?: reporterTypes.Location): reporterTypes.Location | undefined;
private _absoluteLocation(location: reporterTypes.Location | undefined): reporterTypes.Location | undefined {
Expand Down Expand Up @@ -480,7 +487,7 @@ export class TeleTestCase implements reporterTypes.TestCase {

expectedStatus: reporterTypes.TestStatus = 'passed';
timeout = 0;
annotations: Annotation[] = [];
annotations: TestAnnotation[] = [];
retries = 0;
tags: string[] = [];
repeatEachIndex = 0;
Expand Down
6 changes: 3 additions & 3 deletions packages/playwright/src/reporters/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import { codeFrameColumns } from '../transform/babelBundle';
import { resolveReporterOutputPath, stripAnsiEscapes } from '../util';

import type { ReporterV2 } from './reporterV2';
import type { Metadata } from '../../types/test';
import type { Metadata, TestAnnotation } from '../../types/test';
import type * as api from '../../types/testReporter';
import type { HTMLReport, Stats, TestAttachment, TestCase, TestCaseSummary, TestFile, TestFileSummary, TestResult, TestStep, TestAnnotation } from '@html-reporter/types';
import type { HTMLReport, Stats, TestAttachment, TestCase, TestCaseSummary, TestFile, TestFileSummary, TestResult, TestStep } from '@html-reporter/types';
import type { ZipFile } from 'playwright-core/lib/zipBundle';
import type { TransformCallback } from 'stream';

Expand Down Expand Up @@ -503,7 +503,7 @@ class HtmlBuilder {

private _serializeAnnotations(annotations: api.TestCase['annotations']): TestAnnotation[] {
// Annotations can be pushed directly, with a wrong type.
return annotations.map(a => ({ type: a.type, description: a.description ? String(a.description) : a.description }));
return annotations.map(a => ({ type: a.type, description: a.description ? String(a.description) : a.description, location: a.location }));
}

private _createTestResult(test: api.TestCase, result: api.TestResult): TestResult {
Expand Down
18 changes: 14 additions & 4 deletions packages/playwright/src/reporters/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import { createReporters } from '../runner/reporters';
import { relativeFilePath } from '../util';

import type { BlobReportMetadata } from './blob';
import type { ReporterDescription } from '../../types/test';
import type { ReporterDescription, TestAnnotation } from '../../types/test';
import type { TestError } from '../../types/testReporter';
import type { FullConfigInternal } from '../common/config';
import type { JsonConfig, JsonEvent, JsonFullResult, JsonLocation, JsonProject, JsonSuite, JsonTestCase, JsonTestResultEnd, JsonTestStepEnd, JsonTestStepStart } from '../isomorphic/teleReceiver';
import type { JsonConfig, JsonEvent, JsonFullResult, JsonLocation, JsonProject, JsonSuite, JsonTestCase, JsonTestEnd, JsonTestResultEnd, JsonTestStepEnd, JsonTestStepStart } from '../isomorphic/teleReceiver';
import type * as blobV1 from './versions/blobV1';

type StatusCallback = (message: string) => void;
Expand Down Expand Up @@ -474,7 +474,10 @@ class PathSeparatorPatcher {
return;
}
if (jsonEvent.method === 'onTestEnd') {
const test = jsonEvent.params.test as JsonTestEnd;
test.annotations?.forEach(annotation => this._updateAnnotationLocations(annotation));
const testResult = jsonEvent.params.result as JsonTestResultEnd;
testResult.annotations?.forEach(annotation => this._updateAnnotationLocations(annotation));
testResult.errors.forEach(error => this._updateErrorLocations(error));
testResult.attachments.forEach(attachment => {
if (attachment.path)
Expand All @@ -490,6 +493,7 @@ class PathSeparatorPatcher {
if (jsonEvent.method === 'onStepEnd') {
const step = jsonEvent.params.step as JsonTestStepEnd;
this._updateErrorLocations(step.error);
step.annotations?.forEach(annotation => this._updateAnnotationLocations(annotation));
return;
}
}
Expand All @@ -506,10 +510,12 @@ class PathSeparatorPatcher {
if (isFileSuite)
suite.title = this._updatePath(suite.title);
for (const entry of suite.entries) {
if ('testId' in entry)
if ('testId' in entry) {
this._updateLocation(entry.location);
else
entry.annotations?.forEach(annotation => this._updateAnnotationLocations(annotation));
} else {
this._updateSuite(entry);
}
}
}

Expand All @@ -520,6 +526,10 @@ class PathSeparatorPatcher {
}
}

private _updateAnnotationLocations(annotation: TestAnnotation) {
this._updateLocation(annotation.location);
}

private _updateLocation(location?: JsonLocation) {
if (location)
location.file = this._updatePath(location.file);
Expand Down
15 changes: 12 additions & 3 deletions packages/playwright/src/reporters/teleEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { serializeRegexPatterns } from '../isomorphic/teleReceiver';

import type { ReporterV2 } from './reporterV2';
import type * as reporterTypes from '../../types/testReporter';
import type { TestAnnotation } from '../../types/test';
import type * as teleReceiver from '../isomorphic/teleReceiver';

export type TeleReporterEmitterOptions = {
Expand Down Expand Up @@ -216,7 +217,7 @@ export class TeleReporterEmitter implements ReporterV2 {
retries: test.retries,
tags: test.tags,
repeatEachIndex: test.repeatEachIndex,
annotations: test.annotations,
annotations: this._relativeAnnotationLocations(test.annotations),
};
}

Expand All @@ -237,7 +238,7 @@ export class TeleReporterEmitter implements ReporterV2 {
status: result.status,
errors: result.errors,
attachments: this._serializeAttachments(result.attachments),
annotations: result.annotations?.length ? result.annotations : undefined,
annotations: result.annotations?.length ? this._relativeAnnotationLocations(result.annotations) : undefined,
};
}

Expand Down Expand Up @@ -268,10 +269,18 @@ export class TeleReporterEmitter implements ReporterV2 {
duration: step.duration,
error: step.error,
attachments: step.attachments.length ? step.attachments.map(a => result.attachments.indexOf(a)) : undefined,
annotations: step.annotations.length ? step.annotations : undefined,
annotations: step.annotations.length ? this._relativeAnnotationLocations(step.annotations) : undefined,
};
}

private _relativeAnnotationLocations(annotations: TestAnnotation[]): TestAnnotation[] {
return annotations.map(annotation => {
if (annotation.location)
annotation.location = this._relativeLocation(annotation.location);
return annotation;
});
}

private _relativeLocation(location: reporterTypes.Location): reporterTypes.Location;
private _relativeLocation(location?: reporterTypes.Location): reporterTypes.Location | undefined;
private _relativeLocation(location: reporterTypes.Location | undefined): reporterTypes.Location | undefined {
Expand Down
Loading
Loading