Skip to content

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

fix(electron): record har file (#30748)

Yury Semikhatsky64b4ac173269
diff --git a/packages/playwright-core/src/client/electron.ts b/packages/playwright-core/src/client/electron.ts
index f20bbfba19..a9e90b77f2 100644
--- a/packages/playwright-core/src/client/electron.ts
+++ b/packages/playwright-core/src/client/electron.ts
@@ -57,7 +57,7 @@ export class Electron extends ChannelOwner<channels.ElectronChannel> implements
tracesDir: options.tracesDir,
};
const app = ElectronApplication.from((await this._channel.launch(params)).electronApplication);
- app._context._options = params;
+ app._context._setOptions(params, options);
return app;
}
}
@@ -66,7 +66,6 @@ export class ElectronApplication extends ChannelOwner<channels.ElectronApplicati
readonly _context: BrowserContext;
private _windows = new Set<Page>();
private _timeoutSettings = new TimeoutSettings();
- private _isClosed = false;
static from(electronApplication: channels.ElectronApplicationChannel): ElectronApplication {
return (electronApplication as any)._object;
@@ -79,7 +78,6 @@ export class ElectronApplication extends ChannelOwner<channels.ElectronApplicati
this._onPage(page);
this._context.on(Events.BrowserContext.Page, page => this._onPage(page));
this._channel.on('close', () => {
- this._isClosed = true;
this.emit(Events.ElectronApplication.Close);
});
this._channel.on('console', event => this.emit(Events.ElectronApplication.Console, new ConsoleMessage(event)));
@@ -118,9 +116,7 @@ export class ElectronApplication extends ChannelOwner<channels.ElectronApplicati
}
async close() {
- if (this._isClosed)
- return;
- await this._channel.close().catch(() => {});
+ await this._context.close().catch(() => {});
}
async waitForEvent(event: string, optionsOrPredicate: WaitForEventOptions = {}): Promise<any> {
diff --git a/packages/playwright-core/src/protocol/validator.ts b/packages/playwright-core/src/protocol/validator.ts
index 5e31640224..f97cf10969 100644
--- a/packages/playwright-core/src/protocol/validator.ts
+++ b/packages/playwright-core/src/protocol/validator.ts
@@ -2313,8 +2313,6 @@ scheme.ElectronApplicationUpdateSubscriptionParams = tObject({
enabled: tBoolean,
});
scheme.ElectronApplicationUpdateSubscriptionResult = tOptional(tObject({}));
-scheme.ElectronApplicationCloseParams = tOptional(tObject({}));
-scheme.ElectronApplicationCloseResult = tOptional(tObject({}));
scheme.AndroidInitializer = tOptional(tObject({}));
scheme.AndroidDevicesParams = tObject({
host: tOptional(tString),
diff --git a/packages/protocol/src/channels.ts b/packages/protocol/src/channels.ts
index 7e683b9161..2024e02b56 100644
--- a/packages/protocol/src/channels.ts
+++ b/packages/protocol/src/channels.ts
@@ -4124,7 +4124,6 @@ export interface ElectronApplicationChannel extends ElectronApplicationEventTarg
evaluateExpression(params: ElectronApplicationEvaluateExpressionParams, metadata?: CallMetadata): Promise<ElectronApplicationEvaluateExpressionResult>;
evaluateExpressionHandle(params: ElectronApplicationEvaluateExpressionHandleParams, metadata?: CallMetadata): Promise<ElectronApplicationEvaluateExpressionHandleResult>;
updateSubscription(params: ElectronApplicationUpdateSubscriptionParams, metadata?: CallMetadata): Promise<ElectronApplicationUpdateSubscriptionResult>;
- close(params?: ElectronApplicationCloseParams, metadata?: CallMetadata): Promise<ElectronApplicationCloseResult>;
}
export type ElectronApplicationCloseEvent = {};
export type ElectronApplicationConsoleEvent = {
@@ -4176,9 +4175,6 @@ export type ElectronApplicationUpdateSubscriptionOptions = {
};
export type ElectronApplicationUpdateSubscriptionResult = void;
-export type ElectronApplicationCloseParams = {};
-export type ElectronApplicationCloseOptions = {};
-export type ElectronApplicationCloseResult = void;
export interface ElectronApplicationEvents {
'close': ElectronApplicationCloseEvent;
diff --git a/packages/protocol/src/protocol.yml b/packages/protocol/src/protocol.yml
index 8ffe531754..d3b5f9d00c 100644
--- a/packages/protocol/src/protocol.yml
+++ b/packages/protocol/src/protocol.yml
@@ -3243,8 +3243,6 @@ ElectronApplication:
- console
enabled: boolean
- close:
-
events:
close:
console:
diff --git a/tests/electron/electron-app.spec.ts b/tests/electron/electron-app.spec.ts
index 0ecfb2b42d..37bb69ea9e 100644
--- a/tests/electron/electron-app.spec.ts
+++ b/tests/electron/electron-app.spec.ts
@@ -261,6 +261,18 @@ test('should record video', async ({ launchElectronApp }, testInfo) => {
expect(fs.statSync(videoPath).size).toBeGreaterThan(0);
});
+test('should record har', async ({ launchElectronApp, server }, testInfo) => {
+ test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/30747' });
+ const app = await launchElectronApp('electron-window-app.js', [], {
+ recordHar: { path: testInfo.outputPath('har.zip') }
+ });
+ const page = await app.firstWindow();
+ await page.goto(server.EMPTY_PAGE);
+ await app.close();
+ expect(fs.existsSync(testInfo.outputPath('har.zip'))).toBeTruthy();
+ expect(fs.statSync(testInfo.outputPath('har.zip')).size).toBeGreaterThan(0);
+});
+
test('should be able to get the first window when with a delayed navigation', async ({ launchElectronApp }) => {
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/17765' });