Skip to content

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

Revert "feat(tracing): add live option to startHar" (#40621)

Yury Semikhatsky7147fa202de4
diff --git a/docs/src/api/class-tracing.md b/docs/src/api/class-tracing.md
index 8454bcc0b9..1af0427bfd 100644
--- a/docs/src/api/class-tracing.md
+++ b/docs/src/api/class-tracing.md
@@ -380,13 +380,6 @@ A glob or regex pattern to filter requests that are stored in the HAR. Defaults
Only used together with `content: 'attach'`. When set, response bodies are placed in this directory instead of next to
the HAR file. Not compatible with a `.zip` HAR file.
-### option: Tracing.startHar.live
-* since: v1.60
-* langs: js
-- `live` <[boolean]>
-
-When set to `true`, the HAR file is rewritten on disk every time a network request finishes, so that the file can be read while recording. The file is finalized on [`method: Tracing.stopHar`]. Not compatible with a `.zip` HAR file or with remote connections.
-
## async method: Tracing.group
* since: v1.49
- returns: <[Disposable]>
diff --git a/packages/playwright-client/types/types.d.ts b/packages/playwright-client/types/types.d.ts
index 441d3f0fbd..bf34b0dabd 100644
--- a/packages/playwright-client/types/types.d.ts
+++ b/packages/playwright-client/types/types.d.ts
@@ -22170,14 +22170,6 @@ export interface Tracing {
*/
content?: "omit"|"embed"|"attach";
- /**
- * When set to `true`, the HAR file is rewritten on disk every time a network request finishes, so that the file can
- * be read while recording. The file is finalized on
- * [tracing.stopHar()](https://playwright.dev/docs/api/class-tracing#tracing-stop-har). Not compatible with a `.zip`
- * HAR file or with remote connections.
- */
- live?: boolean;
-
/**
* When set to `minimal`, only record information necessary for routing from HAR. This omits sizes, timing, page,
* cookies, security and other types of HAR information that are not used when replaying from HAR. Defaults to `full`.
diff --git a/packages/playwright-core/src/client/tracing.ts b/packages/playwright-core/src/client/tracing.ts
index a84e645c6b..5d771656b2 100644
--- a/packages/playwright-core/src/client/tracing.ts
+++ b/packages/playwright-core/src/client/tracing.ts
@@ -96,23 +96,18 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
});
}
- async startHar(path: string, options: { content?: 'embed' | 'attach' | 'omit', mode?: 'full' | 'minimal', urlFilter?: string | RegExp, resourcesDir?: string, live?: boolean } = {}) {
+ async startHar(path: string, options: { content?: 'embed' | 'attach' | 'omit', mode?: 'full' | 'minimal', urlFilter?: string | RegExp, resourcesDir?: string } = {}) {
await this._wrapApiCall(async () => {
if (this._harId)
throw new Error('HAR recording has already been started');
if (options.resourcesDir && path.endsWith('.zip'))
throw new Error('resourcesDir option is not compatible with a .zip har file');
- if (options.live && path.endsWith('.zip'))
- throw new Error('live option is not compatible with a .zip har file');
- if (options.live && this._connection.isRemote())
- throw new Error('live option is not supported for remote connections');
const defaultContent = path.endsWith('.zip') ? 'attach' : 'embed';
this._harId = await this._recordIntoHAR(path, null, {
url: options.urlFilter,
updateContent: options.content ?? defaultContent,
updateMode: options.mode ?? 'full',
resourcesDir: options.resourcesDir,
- live: options.live,
});
});
return new DisposableStub(() => this.stopHar());
@@ -128,7 +123,7 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
});
}
- async _recordIntoHAR(har: string, page: Page | null, options: { url?: string | RegExp, updateContent?: 'attach' | 'embed' | 'omit', updateMode?: 'minimal' | 'full', resourcesDir?: string, live?: boolean } = {}): Promise<string> {
+ async _recordIntoHAR(har: string, page: Page | null, options: { url?: string | RegExp, updateContent?: 'attach' | 'embed' | 'omit', updateMode?: 'minimal' | 'full', resourcesDir?: string } = {}): Promise<string> {
const isZip = har.endsWith('.zip');
const { harId } = await this._channel.harStart({
page: page?._channel,
@@ -140,7 +135,6 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
mode: options.updateMode ?? 'minimal',
harPath: isZip ? undefined : har,
resourcesDir: options.resourcesDir,
- live: options.live,
},
});
this._harRecorders.set(harId, { path: har, resourcesDir: options.resourcesDir });
diff --git a/packages/playwright-core/src/protocol/validator.ts b/packages/playwright-core/src/protocol/validator.ts
index 49c567f58c..891326d306 100644
--- a/packages/playwright-core/src/protocol/validator.ts
+++ b/packages/playwright-core/src/protocol/validator.ts
@@ -185,7 +185,6 @@ scheme.RecordHarOptions = tObject({
urlRegexFlags: tOptional(tString),
harPath: tOptional(tString),
resourcesDir: tOptional(tString),
- live: tOptional(tBoolean),
});
scheme.FormField = tObject({
name: tString,
diff --git a/packages/playwright-core/src/server/har/harRecorder.ts b/packages/playwright-core/src/server/har/harRecorder.ts
index dc8bbc463b..3367c1acdb 100644
--- a/packages/playwright-core/src/server/har/harRecorder.ts
+++ b/packages/playwright-core/src/server/har/harRecorder.ts
@@ -37,8 +37,6 @@ export class HarRecorder implements HarTracerDelegate {
private _tracer: HarTracer;
private _entries: har.Entry[] = [];
private _writtenContentEntries = new Set<string>();
- private _live: boolean;
- private _harDirEnsured = false;
constructor(context: BrowserContext | APIRequestContext, fallbackDir: string, harId: string, page: Page | null, options: channels.RecordHarOptions) {
this._context = context;
@@ -52,7 +50,6 @@ export class HarRecorder implements HarTracerDelegate {
this._resourcesDir = path.join(fallbackDir, `${harId}-resources`);
const urlFilterRe = options.urlRegexSource !== undefined && options.urlRegexFlags !== undefined ? new RegExp(options.urlRegexSource, options.urlRegexFlags) : undefined;
const content = options.content || 'embed';
- this._live = !!options.live;
this._tracer = new HarTracer(context, page, this, {
content,
slimMode: options.mode === 'minimal',
@@ -69,17 +66,6 @@ export class HarRecorder implements HarTracerDelegate {
}
onEntryFinished(entry: har.Entry) {
- if (this._live)
- this._writeLog(this._tracer._snapshot());
- }
-
- private _writeLog(log: har.Log) {
- log.entries = this._entries;
- if (!this._harDirEnsured) {
- this._fs.mkdir(path.dirname(this._harFilePath));
- this._harDirEnsured = true;
- }
- this._fs.writeFile(this._harFilePath, jsonStringify({ log }));
}
onContentBlob(sha1: string, buffer: Buffer) {
@@ -96,7 +82,12 @@ export class HarRecorder implements HarTracerDelegate {
return;
this._isFlushed = true;
await this._tracer.flush();
- this._writeLog(this._tracer.stop());
+
+ const log = this._tracer.stop();
+ log.entries = this._entries;
+
+ this._fs.mkdir(path.dirname(this._harFilePath));
+ this._fs.writeFile(this._harFilePath, jsonStringify({ log }));
}
async flush() {
diff --git a/packages/playwright-core/src/server/har/harTracer.ts b/packages/playwright-core/src/server/har/harTracer.ts
index f695e6cd9e..4281e7b686 100644
--- a/packages/playwright-core/src/server/har/harTracer.ts
+++ b/packages/playwright-core/src/server/har/harTracer.ts
@@ -532,18 +532,13 @@ export class HarTracer {
await Promise.all(this._barrierPromises);
}
- _snapshot(): har.Log {
+ stop() {
+ this._started = false;
+ eventsHelper.removeEventListeners(this._eventListeners);
+ this._barrierPromises.clear();
+
const context = this._context instanceof BrowserContext ? this._context : undefined;
- const pages = this._pageEntries.size ? Array.from(this._pageEntries.values()).map(pageEntry => {
- const copy = { ...pageEntry, pageTimings: { ...pageEntry.pageTimings } };
- if (!this._options.omitTiming) {
- const startDateTime = ((pageEntry as any)[startedDateSymbol] as Date).valueOf();
- copy.pageTimings.onContentLoad = relativeTiming(copy.pageTimings.onContentLoad, startDateTime);
- copy.pageTimings.onLoad = relativeTiming(copy.pageTimings.onLoad, startDateTime);
- }
- return copy;
- }) : undefined;
- return {
+ const log: har.Log = {
version: '1.2',
creator: {
name: 'Playwright',
@@ -553,16 +548,22 @@ export class HarTracer {
name: context?._browser.options.name || '',
version: context?._browser.version() || ''
},
- pages,
+ pages: this._pageEntries.size ? Array.from(this._pageEntries.values()) : undefined,
entries: [],
};
- }
-
- stop() {
- this._started = false;
- eventsHelper.removeEventListeners(this._eventListeners);
- this._barrierPromises.clear();
- const log = this._snapshot();
+ if (!this._options.omitTiming) {
+ for (const pageEntry of log.pages || []) {
+ const startDateTime = ((pageEntry as any)[startedDateSymbol] as Date).valueOf();
+ if (typeof pageEntry.pageTimings.onContentLoad === 'number' && pageEntry.pageTimings.onContentLoad >= 0)
+ pageEntry.pageTimings.onContentLoad -= startDateTime;
+ else
+ pageEntry.pageTimings.onContentLoad = -1;
+ if (typeof pageEntry.pageTimings.onLoad === 'number' && pageEntry.pageTimings.onLoad >= 0)
+ pageEntry.pageTimings.onLoad -= startDateTime;
+ else
+ pageEntry.pageTimings.onLoad = -1;
+ }
+ }
this._pageEntries.clear();
return log;
}
@@ -611,10 +612,6 @@ export class HarTracer {
}
-function relativeTiming(value: number | undefined, startDateTime: number): number {
- return typeof value === 'number' && value >= 0 ? value - startDateTime : -1;
-}
-
function createHarEntry(pageRef: string | undefined, method: string, url: URL, frameref: string | undefined, options: HarTracerOptions): har.Entry {
const harEntry: har.Entry = {
pageref: pageRef,
diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts
index 441d3f0fbd..bf34b0dabd 100644
--- a/packages/playwright-core/types/types.d.ts
+++ b/packages/playwright-core/types/types.d.ts
@@ -22170,14 +22170,6 @@ export interface Tracing {
*/
content?: "omit"|"embed"|"attach";
- /**
- * When set to `true`, the HAR file is rewritten on disk every time a network request finishes, so that the file can
- * be read while recording. The file is finalized on
- * [tracing.stopHar()](https://playwright.dev/docs/api/class-tracing#tracing-stop-har). Not compatible with a `.zip`
- * HAR file or with remote connections.
- */
- live?: boolean;
-
/**
* When set to `minimal`, only record information necessary for routing from HAR. This omits sizes, timing, page,
* cookies, security and other types of HAR information that are not used when replaying from HAR. Defaults to `full`.
diff --git a/packages/protocol/src/channels.d.ts b/packages/protocol/src/channels.d.ts
index c051dc84e9..e43bf63f90 100644
--- a/packages/protocol/src/channels.d.ts
+++ b/packages/protocol/src/channels.d.ts
@@ -317,7 +317,6 @@ export type RecordHarOptions = {
urlRegexFlags?: string,
harPath?: string,
resourcesDir?: string,
- live?: boolean,
};
export type FormField = {
diff --git a/packages/protocol/src/protocol.yml b/packages/protocol/src/protocol.yml
index 48e5147122..fe16fe1e55 100644
--- a/packages/protocol/src/protocol.yml
+++ b/packages/protocol/src/protocol.yml
@@ -326,7 +326,6 @@ RecordHarOptions:
urlRegexFlags: string?
harPath: string?
resourcesDir: string?
- live: boolean?
FormField:
diff --git a/packages/utils/serializedFS.ts b/packages/utils/serializedFS.ts
index 6551f60c66..711d13126a 100644
--- a/packages/utils/serializedFS.ts
+++ b/packages/utils/serializedFS.ts
@@ -101,11 +101,6 @@ export class SerializedFS {
last.content += op.content;
return;
}
- if (last?.op === 'writeFile' && op.op === 'writeFile' && last.file === op.file && !last.skipIfExists && !op.skipIfExists) {
- // The latest write supersedes the previous one queued for the same file.
- last.content = op.content;
- return;
- }
this._operations.push(op);
if (this._operationsDone.isDone())
diff --git a/tests/library/har.spec.ts b/tests/library/har.spec.ts
index c99ccf7c31..6deab064cf 100644
--- a/tests/library/har.spec.ts
+++ b/tests/library/har.spec.ts
@@ -972,57 +972,4 @@ it.describe('tracing.startHar', () => {
await expect(context.tracing.startHar(harPath, { content: 'attach', resourcesDir })).rejects.toThrow(/resourcesDir option is not compatible with a \.zip har file/);
await context.close();
});
-
- it('should write HAR live while recording', async ({ contextFactory, server }, testInfo) => {
- let cssResponse: import('http').ServerResponse | undefined;
- const cssRequested = new Promise<void>(resolve => {
- server.setRoute('/one-style.css', (_, res) => {
- cssResponse = res;
- resolve();
- });
- });
-
- const context = await contextFactory();
- const harPath = testInfo.outputPath('tracing.har');
- await context.tracing.startHar(harPath, { live: true });
- const page = await context.newPage();
- const navigation = page.goto(server.PREFIX + '/one-style.html');
-
- await cssRequested;
-
- const readEntries = () => {
- try {
- const log = JSON.parse(fs.readFileSync(harPath).toString()).log as Log;
- return log.entries.map(e => ({ url: e.request.url, status: e.response.status }));
- } catch {
- return [];
- }
- };
-
- // The HAR file is on disk before stopHar(), with the in-flight CSS entry shown as pending.
- await expect.poll(readEntries).toEqual(expect.arrayContaining([
- { url: server.PREFIX + '/one-style.html', status: 200 },
- { url: server.PREFIX + '/one-style.css', status: -1 },
- ]));
-
- cssResponse!.setHeader('Content-Type', 'text/css');
- cssResponse!.end('body { color: red; }');
- await navigation;
-
- await context.tracing.stopHar();
- await context.close();
-
- const log = JSON.parse(fs.readFileSync(harPath).toString()).log as Log;
- expect(log.entries.map(e => ({ url: e.request.url, status: e.response.status }))).toEqual(expect.arrayContaining([
- { url: server.PREFIX + '/one-style.html', status: 200 },
- { url: server.PREFIX + '/one-style.css', status: 200 },
- ]));
- });
-
- it('should reject live together with a .zip har file', async ({ contextFactory }, testInfo) => {
- const context = await contextFactory();
- const harPath = testInfo.outputPath('tracing.har.zip');
- await expect(context.tracing.startHar(harPath, { live: true })).rejects.toThrow(/live option is not compatible with a \.zip har file/);
- await context.close();
- });
});