Skip to content

Commit f9a61d5

Browse files
committed
chore(bidi): create as Bidi session before browser.close
1 parent ae18bae commit f9a61d5

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

packages/playwright-core/src/server/bidi/bidiConnection.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import type * as bidi from './third_party/bidiProtocol';
2828

2929
// BidiPlaywright uses this special id to issue Browser.close command which we
3030
// should ignore.
31-
export const kBrowserCloseMessageId = 0;
31+
export const kBrowserCloseMessageId = Number.MAX_SAFE_INTEGER - 1;
32+
export const kShutdownSessionNewMessageId = kBrowserCloseMessageId - 1;
3233

3334
export class BidiConnection {
3435
private readonly _transport: ConnectionTransport;
@@ -209,7 +210,7 @@ export class BidiSession extends EventEmitter {
209210

210211
dispatchMessage(message: any) {
211212
const object = message as bidi.Message;
212-
if (object.id === kBrowserCloseMessageId)
213+
if (object.id === kBrowserCloseMessageId || object.id === kShutdownSessionNewMessageId)
213214
return;
214215
if (object.id && this._callbacks.has(object.id)) {
215216
const callback = this._callbacks.get(object.id)!;

packages/playwright-core/src/server/bidi/bidiFirefox.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import path from 'path';
2020
import { wrapInASCIIBox } from '../utils/ascii';
2121
import { BrowserType, kNoXServerRunningError } from '../browserType';
2222
import { BidiBrowser } from './bidiBrowser';
23-
import { kBrowserCloseMessageId } from './bidiConnection';
23+
import { kBrowserCloseMessageId, kShutdownSessionNewMessageId } from './bidiConnection';
2424
import { createProfile } from './third_party/firefoxPrefs';
2525
import { ManualPromise } from '../../utils/isomorphic/manualPromise';
2626

@@ -78,6 +78,17 @@ export class BidiFirefox extends BrowserType {
7878
}
7979

8080
override attemptToGracefullyCloseBrowser(transport: ConnectionTransport): void {
81+
// browser.close does not work without an active session, make sure
82+
// to create a new session first. The command will fail if a session
83+
// already exists, we'll just ignore the error.
84+
transport.send({ method: 'session.new', params: {
85+
capabilities: {
86+
alwaysMatch: {
87+
unhandledPromptBehavior: { default: 'ignore' },
88+
webSocketUrl: true
89+
}
90+
}
91+
}, id: kShutdownSessionNewMessageId });
8192
transport.send({ method: 'browser.close', params: {}, id: kBrowserCloseMessageId });
8293
}
8394

0 commit comments

Comments
 (0)