Skip to content

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

fix(webkit): band-aid double connection fix to unblock tests (#422)

Dmitry Gozman66e8a9c04ff0
diff --git a/src/server/wkPlaywright.ts b/src/server/wkPlaywright.ts
index eee4fbb7f6..bf4a277821 100644
--- a/src/server/wkPlaywright.ts
+++ b/src/server/wkPlaywright.ts
@@ -67,7 +67,7 @@ export class WKBrowserServer {
async close(): Promise<void> {
const transport = await createTransport(this._connectOptions);
- const connection = new WKConnection(transport);
+ const connection = WKConnection.from(transport);
await connection.send('Browser.close');
connection.dispose();
}
diff --git a/src/webkit/wkBrowser.ts b/src/webkit/wkBrowser.ts
index 82670173a4..bd92fe4728 100644
--- a/src/webkit/wkBrowser.ts
+++ b/src/webkit/wkBrowser.ts
@@ -51,7 +51,7 @@ export class WKBrowser extends browser.Browser {
constructor(transport: ConnectionTransport) {
super();
- this._connection = new WKConnection(transport);
+ this._connection = WKConnection.from(transport);
this._defaultContext = this._createBrowserContext(undefined, {});
diff --git a/src/webkit/wkConnection.ts b/src/webkit/wkConnection.ts
index 25ae96ef7b..078cdce77c 100644
--- a/src/webkit/wkConnection.ts
+++ b/src/webkit/wkConnection.ts
@@ -34,6 +34,8 @@ export const WKPageProxySessionEvents = {
DidCommitProvisionalTarget: Symbol('PageProxyEvents.DidCommitProvisionalTarget'),
};
+const kConnectionSymbol = Symbol();
+
export class WKConnection extends platform.EventEmitter {
private _lastId = 0;
private readonly _callbacks = new Map<number, {resolve:(o: any) => void, reject: (e: Error) => void, error: Error, method: string}>();
@@ -42,6 +44,15 @@ export class WKConnection extends platform.EventEmitter {
private _closed = false;
+ static from(transport: ConnectionTransport): WKConnection {
+ let connection = (transport as any)[kConnectionSymbol];
+ if (!connection) {
+ connection = new WKConnection(transport);
+ (transport as any)[kConnectionSymbol] = connection;
+ }
+ return connection;
+ }
+
constructor(transport: ConnectionTransport) {
super();
this._transport = transport;