Skip to content

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

fix(har): HAR artifacts need to be marked remote (#8462)

Ross Wollmana1313727e87d
diff --git a/src/client/browserContext.ts b/src/client/browserContext.ts
index 6d2377e745..000f404f02 100644
--- a/src/client/browserContext.ts
+++ b/src/client/browserContext.ts
@@ -35,6 +35,7 @@ import * as structs from '../../types/structs';
import { CDPSession } from './cdpSession';
import { Tracing } from './tracing';
import type { BrowserType } from './browserType';
+import { Artifact } from './artifact';
export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel, channels.BrowserContextInitializer> implements api.BrowserContext {
_pages = new Set<Page>();
@@ -346,7 +347,11 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel,
await this._browserType?._onWillCloseContext?.(this);
if (this._options.recordHar) {
const har = await this._channel.harExport();
- await har.artifact.saveAs({ path: this._options.recordHar.path });
+ const artifact = Artifact.from(har.artifact);
+ if (this.browser()?._remoteType)
+ artifact._isRemote = true;
+ await artifact.saveAs(this._options.recordHar.path);
+ await artifact.delete();
}
await channel.close();
await this._closedPromise;
diff --git a/tests/browsertype-connect.spec.ts b/tests/browsertype-connect.spec.ts
index 12caa547e4..4a0f6f8c36 100644
--- a/tests/browsertype-connect.spec.ts
+++ b/tests/browsertype-connect.spec.ts
@@ -454,3 +454,24 @@ test('should be able to connect when the wsEndpont is passed as the first argume
expect(await page.evaluate('1 + 2')).toBe(3);
await browser.close();
});
+
+test('should save har', async ({browserType, startRemoteServer, server}, testInfo) => {
+ const remoteServer = await startRemoteServer();
+ const browser = await browserType.connect(remoteServer.wsEndpoint());
+ const harPath = testInfo.outputPath('test.har');
+ const context = await browser.newContext({
+ recordHar: {
+ path: harPath,
+ }
+ });
+ const page = await context.newPage();
+ await page.goto(server.EMPTY_PAGE);
+ await context.close();
+ await browser.close();
+
+ const log = JSON.parse(fs.readFileSync(harPath).toString())['log'];
+ expect(log.entries.length).toBe(1);
+ const entry = log.entries[0];
+ expect(entry.pageref).toBe(log.pages[0].id);
+ expect(entry.request.url).toBe(server.EMPTY_PAGE);
+});