Skip to content

Commit

Permalink
feat(trace): add a trace option into the test runner (#6961)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrey Lushnikov <aslushnikov@gmail.com>
  • Loading branch information
pavelfeldman and aslushnikov committed Jun 8, 2021
1 parent 93f6b57 commit 021f51c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/src/test-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ You can specify any options either locally in a test file, or globally in the co
- `'off'` - Do not capture screenshots.
- `'on'` - Capture screenshot after each test.
- `'only-on-failure'` - Capture screenshot after each test failure.
- `trace` option - whether to record trace for each test, off by default. Trace will appear in the test output directory, typically `test-results`.
- `'off'` - Do not record trace.
- `'on'` - Record trace for each test.
- `'retain-on-failure'` - Record trace for each test, but remove it from successful test runs.
- `'retry-with-trace'` - Record trace only when retrying a test.
- `video` option - whether to record video for each test, off by default. Video will appear in the test output directory, typically `test-results`.
- `'off'` - Do not record video.
- `'on'` - Record video for each test.
Expand Down
19 changes: 18 additions & 1 deletion src/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import * as fs from 'fs';
import * as path from 'path';
import type { LaunchOptions, BrowserContextOptions, Page } from '../../types/types';
import type { TestType, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions } from '../../types/test';
import { rootTestType } from './testType';
Expand Down Expand Up @@ -47,6 +48,7 @@ export const test = _baseTest.extend<PlaywrightTestArgs & PlaywrightTestOptions,

screenshot: 'off',
video: 'off',
trace: 'off',
acceptDownloads: undefined,
bypassCSP: undefined,
colorScheme: undefined,
Expand All @@ -68,7 +70,7 @@ export const test = _baseTest.extend<PlaywrightTestArgs & PlaywrightTestOptions,
viewport: undefined,
contextOptions: {},

context: async ({ browser, screenshot, video, acceptDownloads, bypassCSP, colorScheme, deviceScaleFactor, extraHTTPHeaders, hasTouch, geolocation, httpCredentials, ignoreHTTPSErrors, isMobile, javaScriptEnabled, locale, offline, permissions, proxy, storageState, viewport, timezoneId, userAgent, contextOptions }, use, testInfo) => {
context: async ({ browser, screenshot, trace, video, acceptDownloads, bypassCSP, colorScheme, deviceScaleFactor, extraHTTPHeaders, hasTouch, geolocation, httpCredentials, ignoreHTTPSErrors, isMobile, javaScriptEnabled, locale, offline, permissions, proxy, storageState, viewport, timezoneId, userAgent, contextOptions }, use, testInfo) => {
testInfo.snapshotSuffix = process.platform;
if (process.env.PWDEBUG)
testInfo.setTimeout(0);
Expand Down Expand Up @@ -122,9 +124,24 @@ export const test = _baseTest.extend<PlaywrightTestArgs & PlaywrightTestOptions,
const allPages: Page[] = [];
context.on('page', page => allPages.push(page));

const collectingTrace = trace === 'on' || trace === 'retain-on-failure' || (trace === 'retry-with-trace' && testInfo.retry);
if (collectingTrace) {
const name = path.relative(testInfo.project.outputDir, testInfo.outputDir).replace(/[\/\\]/g, '-');
await context.tracing.start({ name, screenshots: true, snapshots: true });
}

await use(context);

const testFailed = testInfo.status !== testInfo.expectedStatus;

const saveTrace = trace === 'on' || (testFailed && trace === 'retain-on-failure') || (trace === 'retry-with-trace' && testInfo.retry);
if (saveTrace) {
const tracePath = testInfo.outputPath(`trace.zip`);
await context.tracing.stop({ path: tracePath });
} else if (collectingTrace) {
await context.tracing.stop();
}

if (screenshot === 'on' || (screenshot === 'only-on-failure' && testFailed)) {
await Promise.all(allPages.map((page, index) => {
const screenshotPath = testInfo.outputPath(`test-${testFailed ? 'failed' : 'finished'}-${++index}.png`);
Expand Down
9 changes: 9 additions & 0 deletions types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,15 @@ export type PlaywrightTestOptions = {
*/
screenshot: 'off' | 'on' | 'only-on-failure';

/**
* Whether to record trace for each test, off by default.
* - `off`: Do not record trace.
* - `on`: Record trace for each test.
* - `retain-on-failure`: Record trace for each test, but remove trace from successful test run.
* - `retry-with-trace`: Record trace only when retrying a test.
*/
trace: 'off' | 'on' | 'retain-on-failure' | 'retry-with-trace';

/**
* Whether to record video for each test, off by default.
* - `off`: Do not record video.
Expand Down

0 comments on commit 021f51c

Please sign in to comment.