Skip to content
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
14 changes: 13 additions & 1 deletion packages/playwright-core/src/server/trace/recorder/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,22 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
for (const file of new Set([...this._state.chunkFiles, ...this._state.crossChunkFiles]))
entries.push({ name: file, value: path.join(this._state.tracesDir, file) });

const chunkFiles = this._state.chunkFiles;
this._state.chunkFiles = new Set();

if (params.mode === 'discard')
if (params.mode === 'discard') {
// Live traces are read from the disk in place.
if (this._state.options.live)
return undefined;
this._fs.removeFile(this._state.traceFile);
for (const file of chunkFiles) {
if (this._state.crossChunkFiles.has(file))
continue;
this._allResources.delete(file);
this._fs.removeFile(path.join(this._state.tracesDir, file));
}
return undefined;
}

this._fs.copyFile(this._state.networkFile, newNetworkFile);

Expand Down
3 changes: 3 additions & 0 deletions packages/playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,10 @@ class SnapshotRecorder {
} catch {
}
}));
} else {
await Promise.all(this._temporary.map(file => fs.promises.rm(file, { force: true })));
}
this._temporary = [];
}

async captureTemporary(context: BrowserContextImpl) {
Expand Down
11 changes: 11 additions & 0 deletions packages/utils/serializedFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type SerializedFSOperation = {
op: 'appendFile', file: string, content: string,
} | {
op: 'copyFile', from: string, to: string,
} | {
op: 'removeFile', file: string,
} | {
op: 'zip', entries: NameValue[], zipFileName: string,
};
Expand Down Expand Up @@ -86,6 +88,11 @@ export class SerializedFS {
this._appendOperation({ op: 'copyFile', from, to });
}

removeFile(file: string) {
this._buffers.delete(file);
this._appendOperation({ op: 'removeFile', file });
}

async sync() {
for (const file of this._buffers.keys())
this._flushFile(file);
Expand Down Expand Up @@ -162,6 +169,10 @@ export class SerializedFS {
await fs.promises.appendFile(op.file, op.content);
return;
}
case 'removeFile': {
await fs.promises.rm(op.file, { force: true });
return;
}
case 'zip': {
const zipFile = new yazl.ZipFile();
const result = new ManualPromise<void>();
Expand Down
3 changes: 2 additions & 1 deletion tests/library/chromium/connect-over-cdp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,10 @@ test('CD-007 should write traces to provided artifactsDir on connectOverCDP', as
await context.tracing.start({ name: 'cdp-trace', snapshots: true, screenshots: true });
const page = await context.newPage();
await page.setContent('<button>Hello</button>');
// The trace file of a discarded chunk is removed, check it before the chunk stops.
await expect.poll(() => fs.existsSync(path.join(artifactsDir, 'cdp-trace.trace'))).toBe(true);
await context.tracing.stopChunk();

expect(fs.existsSync(path.join(artifactsDir, 'cdp-trace.trace'))).toBe(true);
expect(fs.existsSync(path.join(artifactsDir, 'cdp-trace.network'))).toBe(true);
expect(fs.existsSync(path.join(artifactsDir, 'resources'))).toBe(true);

Expand Down
23 changes: 22 additions & 1 deletion tests/library/tracing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ test('TR-012 should collect two traces', async ({ context, page, server }, testI
}
});

test('TR-013 should respect tracesDir and name', async ({ browserType, server, mode }, testInfo) => {
test('TR-013 should respect tracesDir and name', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/44086' }
}, async ({ browserType, server, mode }, testInfo) => {
const tracesDir = testInfo.outputPath('traces');
const browser = await browserType.launch({ tracesDir });
const context = await browser.newContext();
Expand All @@ -337,6 +339,25 @@ test('TR-013 should respect tracesDir and name', async ({ browserType, server, m
expect(fs.existsSync(path.join(tracesDir, 'name2.trace'))).toBe(true);
expect(fs.existsSync(path.join(tracesDir, 'name2.network'))).toBe(true);

await context.tracing.start({ name: 'discarded', screenshots: true, snapshots: { dom: true, screen: true, aria: true } });
await page.goto(server.PREFIX + '/har.html');
// A stylesheet changed through CSSOM is saved as a snapshot resource.
await page.evaluate(() => document.styleSheets[0].insertRule('.discarded-rule { color: red }'));
await page.click('body');
const snapshotResources = () => fs.readdirSync(path.join(tracesDir, 'resources')).filter(file => fs.readFileSync(path.join(tracesDir, 'resources', file), 'utf8').includes('.discarded-rule'));
for (const dir of ['screencast', 'screenshots', 'aria'])
await expect.poll(() => fs.readdirSync(path.join(tracesDir, dir)).length).toBeGreaterThan(0);
await expect.poll(snapshotResources).toHaveLength(1);
const networkResources = fs.readdirSync(path.join(tracesDir, 'resources')).filter(file => !snapshotResources().includes(file));
await context.tracing.stopChunk();
// Discarded files are removed in the background.
await expect.poll(() => fs.existsSync(path.join(tracesDir, 'discarded.trace'))).toBe(false);
for (const dir of ['screencast', 'screenshots', 'aria'])
await expect.poll(() => fs.readdirSync(path.join(tracesDir, dir))).toEqual([]);
await expect.poll(() => fs.readdirSync(path.join(tracesDir, 'resources'))).toEqual(networkResources);
expect(fs.existsSync(path.join(tracesDir, 'discarded.network'))).toBe(true);
await context.tracing.stop();

await browser.close();

function resourceNames(resources: Map<string, Buffer>) {
Expand Down
15 changes: 13 additions & 2 deletions tests/playwright-test/playwright.artifacts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ const testFiles = {
import os from 'os';
import path from 'path';

import { test, expect } from '@playwright/test';
import { test as base, expect } from '@playwright/test';

const test = base.extend({
inspectArtifacts: [async ({}, use, workerInfo) => {
await use();
const artifactsDir = path.join(workerInfo.project.outputDir, '.playwright-artifacts-' + workerInfo.workerIndex);
console.log('%%temporary screenshots: ' + JSON.stringify(fs.readdirSync(artifactsDir).filter(file => file.endsWith('.png'))));
}, { scope: 'worker', auto: true }],
});

test.describe('shared', () => {
let page;
Expand Down Expand Up @@ -167,7 +175,9 @@ test('should work with screenshot: on', async ({ runInlineTest }, testInfo) => {
]);
});

test('should work with screenshot: only-on-failure', async ({ runInlineTest }, testInfo) => {
test('should work with screenshot: only-on-failure', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/44086' }
}, async ({ runInlineTest }, testInfo) => {
const result = await runInlineTest({
...testFiles,
'playwright.config.ts': `
Expand All @@ -178,6 +188,7 @@ test('should work with screenshot: only-on-failure', async ({ runInlineTest }, t
expect(result.exitCode).toBe(1);
expect(result.passed).toBe(5);
expect(result.failed).toBe(5);
expect(result.outputLines).toEqual(Array(5).fill('temporary screenshots: []'));
expect(listFiles(testInfo.outputPath('test-results'))).toEqual([
'.last-run.json',
'artifacts-failing',
Expand Down
Loading