Skip to content

Commit 8c08348

Browse files
authored
fix(launch): handle websocket connect exceptions (#2184)
1 parent 0c51a2e commit 8c08348

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

src/server/chromium.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ export class Chromium extends AbstractBrowserType<CRBrowser> {
130130
}
131131

132132
async connect(options: ConnectOptions): Promise<CRBrowser> {
133-
return await WebSocketTransport.connect(options.wsEndpoint, transport => {
133+
return await WebSocketTransport.connect(options.wsEndpoint, async transport => {
134+
if ((options as any).__testHookBeforeCreateBrowser)
135+
await (options as any).__testHookBeforeCreateBrowser();
134136
return CRBrowser.connect(transport, false, new RootLogger(options.logger), options);
135137
});
136138
}

src/server/firefox.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ export class Firefox extends AbstractBrowserType<FFBrowser> {
148148
}
149149

150150
async connect(options: ConnectOptions): Promise<FFBrowser> {
151-
const logger = new RootLogger(options.logger);
152-
return await WebSocketTransport.connect(options.wsEndpoint, transport => {
153-
return FFBrowser.connect(transport, logger, false, options.slowMo);
151+
return await WebSocketTransport.connect(options.wsEndpoint, async transport => {
152+
if ((options as any).__testHookBeforeCreateBrowser)
153+
await (options as any).__testHookBeforeCreateBrowser();
154+
return FFBrowser.connect(transport, new RootLogger(options.logger), false, options.slowMo);
154155
});
155156
}
156157

src/server/webkit.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ export class WebKit extends AbstractBrowserType<WKBrowser> {
133133
}
134134

135135
async connect(options: ConnectOptions): Promise<WKBrowser> {
136-
return await WebSocketTransport.connect(options.wsEndpoint, transport => {
136+
return await WebSocketTransport.connect(options.wsEndpoint, async transport => {
137+
if ((options as any).__testHookBeforeCreateBrowser)
138+
await (options as any).__testHookBeforeCreateBrowser();
137139
return WKBrowser.connect(transport, new RootLogger(options.logger), options.slowMo);
138140
});
139141
}

src/transport.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,14 @@ export class WebSocketTransport implements ConnectionTransport {
129129
static connect<T>(url: string, onopen: (transport: ConnectionTransport) => Promise<T> | T): Promise<T> {
130130
const transport = new WebSocketTransport(url);
131131
return new Promise<T>((fulfill, reject) => {
132-
transport._ws.addEventListener('open', async () => fulfill(await onopen(transport)));
132+
transport._ws.addEventListener('open', async () => {
133+
try {
134+
fulfill(await onopen(transport));
135+
} catch (e) {
136+
try { transport._ws.close(); } catch (ignored) {}
137+
reject(e);
138+
}
139+
});
133140
transport._ws.addEventListener('error', event => reject(new Error('WebSocket error: ' + event.message)));
134141
});
135142
}

test/launcher.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,15 @@ describe('browserType.connect', function() {
290290
await browserServer._checkLeaks();
291291
await browserServer.close();
292292
});
293+
it.slow()('should handle exceptions during connect', async({browserType, defaultBrowserOptions, server}) => {
294+
const browserServer = await browserType.launchServer(defaultBrowserOptions);
295+
const e = new Error('Dummy');
296+
const __testHookBeforeCreateBrowser = () => { throw e };
297+
const error = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint(), __testHookBeforeCreateBrowser }).catch(e => e);
298+
await browserServer._checkLeaks();
299+
await browserServer.close();
300+
expect(error).toBe(e);
301+
});
293302
});
294303

295304
describe('browserType.launchPersistentContext', function() {

0 commit comments

Comments
 (0)