Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(har): recalculate receive time after response ended (#19646)
Pavel Feldman8b80e22a03ff
diff --git a/packages/playwright-core/src/server/har/harTracer.ts b/packages/playwright-core/src/server/har/harTracer.tsindex 2d442fc5a1..a57d27e0df 100644--- a/packages/playwright-core/src/server/har/harTracer.ts+++ b/packages/playwright-core/src/server/har/harTracer.ts@@ -332,6 +332,11 @@ export class HarTracer {});this._addBarrier(page || request.serviceWorker(), promise);+ // Respose end timing is only available after the response event was received.+ const timing = response.timing();+ harEntry.timings.receive = response.request()._responseEndTiming !== -1 ? helper.millisToRoundishMillis(response.request()._responseEndTiming - timing.responseStart) : -1;+ this._computeHarEntryTotalTime(harEntry);+if (!this._options.omitSizes) {this._addBarrier(page || request.serviceWorker(), response.sizes().then(sizes => {harEntry.response.bodySize = sizes.responseBodySize;@@ -417,7 +422,7 @@ export class HarTracer {const connect = timing.connectEnd !== -1 ? helper.millisToRoundishMillis(timing.connectEnd - timing.connectStart) : -1;const ssl = timing.connectEnd !== -1 ? helper.millisToRoundishMillis(timing.connectEnd - timing.secureConnectionStart) : -1;const wait = timing.responseStart !== -1 ? helper.millisToRoundishMillis(timing.responseStart - timing.requestStart) : -1;- const receive = response.request()._responseEndTiming !== -1 ? helper.millisToRoundishMillis(response.request()._responseEndTiming - timing.responseStart) : -1;+ const receive = -1;harEntry.timings = {dns,@@ -427,7 +432,7 @@ export class HarTracer {wait,receive,};- harEntry.time = [dns, connect, ssl, wait, receive].reduce((pre, cur) => cur > 0 ? cur + pre : pre, 0);+ this._computeHarEntryTotalTime(harEntry);}if (!this._options.omitServerIP) {@@ -460,6 +465,16 @@ export class HarTracer {}));}+ private _computeHarEntryTotalTime(harEntry: har.Entry) {+ harEntry.time = [+ harEntry.timings.dns,+ harEntry.timings.connect,+ harEntry.timings.ssl,+ harEntry.timings.wait,+ harEntry.timings.receive+ ].reduce((pre, cur) => (cur || -1) > 0 ? cur! + pre! : pre, 0)!;+ }+async flush() {await Promise.all(this._barrierPromises);}diff --git a/tests/library/har.spec.ts b/tests/library/har.spec.tsindex 183a152048..cd9c51da62 100644--- a/tests/library/har.spec.ts+++ b/tests/library/har.spec.ts@@ -421,6 +421,13 @@ it('should calculate time', async ({ contextFactory, server }, testInfo) => {expect(log.entries[0].time).toBeGreaterThan(0);});+it('should return receive time', async ({ contextFactory, server }, testInfo) => {+ const { page, getLog } = await pageWithHar(contextFactory, testInfo);+ await page.goto(server.PREFIX + '/har.html');+ const log = await getLog();+ expect(log.entries[0].timings.receive).toBeGreaterThan(0);+});+it('should report the correct _transferSize with PNG files', async ({ contextFactory, server, asset }, testInfo) => {const { page, getLog } = await pageWithHar(contextFactory, testInfo);await page.goto(server.EMPTY_PAGE);