Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: record trace/videos on retries #4009

Merged
merged 2 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import childProcess from 'child_process';
import fs from 'fs';
import path from 'path';
import util from 'util';
import os from 'os';
import type { Browser, BrowserContext, BrowserType, Page } from '../index';
import { Connection } from '../lib/client/connection';
import { Transport } from '../lib/protocol/transport';
Expand All @@ -31,6 +32,7 @@ export { expect } from '@playwright/test/out/matcher.fixtures';
export { config } from '@playwright/test-runner';

const removeFolderAsync = util.promisify(require('rimraf'));
const mkdtempAsync = util.promisify(fs.mkdtemp);

type AllParameters = {
wire: boolean;
Expand Down Expand Up @@ -126,19 +128,20 @@ fixtures.overrideWorkerFixture('playwright', async ({ browserName, testWorkerInd
}
});

fixtures.defineTestFixture('createUserDataDir', async ({testOutputPath}, runTest) => {
let counter = 0;
fixtures.defineTestFixture('createUserDataDir', async ({}, runTest) => {
const dirs: string[] = [];
async function createUserDataDir() {
const dir = testOutputPath(`user-data-dir-${counter++}`);
// We do not put user data dir in testOutputPath,
// because we do not want to upload them as test result artifacts.
//
// Additionally, it is impossible to upload user data dir after test run:
// - Firefox removes lock file later, presumably from another watchdog process?
// - WebKit has circular symlinks that makes CI go crazy.
const dir = await mkdtempAsync(path.join(os.tmpdir(), 'playwright-test-'));
dirs.push(dir);
await fs.promises.mkdir(dir, { recursive: true });
return dir;
}
await runTest(createUserDataDir);
// Remove user data dirs, because we cannot upload them as test result artifacts.
// - Firefox removes lock file later, repsumably from another watchdog process?
// - WebKit has circular symlinks that makes CI go crazy.
await Promise.all(dirs.map(dir => removeFolderAsync(dir).catch(e => {})));
});

Expand Down
32 changes: 10 additions & 22 deletions test/playwright.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,8 @@
*/

import { config, fixtures as baseFixtures } from '@playwright/test-runner';
import fs from 'fs';
import os from 'os';
import path from 'path';
import util from 'util';
import type { Browser, BrowserContext, BrowserContextOptions, BrowserType, LaunchOptions, Page } from '../index';

const mkdtempAsync = util.promisify(fs.mkdtemp);
const removeFolderAsync = util.promisify(require('rimraf'));


// Parameter declarations ------------------------------------------------------

type PlaywrightParameters = {
Expand Down Expand Up @@ -86,8 +78,6 @@ type PlaywrightTestFixtures = {
context: BrowserContext;
// Page instance for test.
page: Page;
// Temporary directory for this test's artifacts.
tmpDir: string;
};

export const fixtures = baseFixtures
Expand Down Expand Up @@ -162,12 +152,16 @@ fixtures.defineWorkerFixture('isLinux', async ({platform}, test) => {

// Test fixtures definitions ---------------------------------------------------

fixtures.defineTestFixture('defaultContextOptions', async ({ testRelativeArtifactsPath, trace }, runTest) => {
await runTest({
relativeArtifactsPath: testRelativeArtifactsPath,
recordTrace: trace,
recordVideos: trace,
});
fixtures.defineTestFixture('defaultContextOptions', async ({ testRelativeArtifactsPath, trace, testInfo }, runTest) => {
if (trace || testInfo.retry) {
await runTest({
relativeArtifactsPath: testRelativeArtifactsPath,
recordTrace: true,
recordVideos: true,
});
} else {
await runTest({});
}
});

fixtures.defineTestFixture('contextFactory', async ({ browser, defaultContextOptions, testInfo, screenshotOnFailure, testOutputPath }, runTest) => {
Expand Down Expand Up @@ -203,12 +197,6 @@ fixtures.defineTestFixture('page', async ({context}, runTest) => {
// Context fixture is taking care of closing the page.
});

fixtures.defineTestFixture('tmpDir', async ({ }, runTest) => {
const tmpDir = await mkdtempAsync(path.join(os.tmpdir(), 'playwright-test-'));
await runTest(tmpDir);
await removeFolderAsync(tmpDir).catch(e => { });
});

fixtures.overrideTestFixture('testParametersArtifactsPath', async ({ browserName, platform }, runTest) => {
await runTest(browserName + '-' + platform);
});
9 changes: 5 additions & 4 deletions test/trace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import type * as trace from '../types/trace';
import * as path from 'path';
import * as fs from 'fs';

it('should record trace', async ({browserType, defaultBrowserOptions, server, tmpDir}) => {
it('should record trace', async ({browserType, defaultBrowserOptions, server, testOutputPath}) => {
const artifactsPath = testOutputPath('trace');
const browser = await browserType.launch({
...defaultBrowserOptions,
artifactsPath: tmpDir,
artifactsPath,
});
const context = await browser.newContext({ recordTrace: true });
const page = await context.newPage();
Expand All @@ -31,7 +32,7 @@ it('should record trace', async ({browserType, defaultBrowserOptions, server, tm
await context.close();
await browser.close();

const traceFile = path.join(tmpDir, 'playwright.trace');
const traceFile = path.join(artifactsPath, 'playwright.trace');
const traceFileContent = await fs.promises.readFile(traceFile, 'utf8');
const traceEvents = traceFileContent.split('\n').filter(line => !!line).map(line => JSON.parse(line)) as trace.TraceEvent[];

Expand All @@ -51,7 +52,7 @@ it('should record trace', async ({browserType, defaultBrowserOptions, server, tm
expect(gotoEvent.value).toBe(url);

expect(gotoEvent.snapshot).toBeTruthy();
expect(fs.existsSync(path.join(tmpDir, 'trace-resources', gotoEvent.snapshot!.sha1))).toBe(true);
expect(fs.existsSync(path.join(artifactsPath, 'trace-resources', gotoEvent.snapshot!.sha1))).toBe(true);
});

it('should require artifactsPath', async ({browserType, defaultBrowserOptions}) => {
Expand Down