fix(launch): handle websocket connect exceptions (#2184)

This commit is contained in:
Dmitry Gozman 2020-05-11 13:49:57 -07:00 committed by GitHub
parent 0c51a2e89d
commit 8c083486a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 6 deletions

View File

@ -130,7 +130,9 @@ export class Chromium extends AbstractBrowserType<CRBrowser> {
}
async connect(options: ConnectOptions): Promise<CRBrowser> {
return await WebSocketTransport.connect(options.wsEndpoint, transport => {
return await WebSocketTransport.connect(options.wsEndpoint, async transport => {
if ((options as any).__testHookBeforeCreateBrowser)
await (options as any).__testHookBeforeCreateBrowser();
return CRBrowser.connect(transport, false, new RootLogger(options.logger), options);
});
}

View File

@ -148,9 +148,10 @@ export class Firefox extends AbstractBrowserType<FFBrowser> {
}
async connect(options: ConnectOptions): Promise<FFBrowser> {
const logger = new RootLogger(options.logger);
return await WebSocketTransport.connect(options.wsEndpoint, transport => {
return FFBrowser.connect(transport, logger, false, options.slowMo);
return await WebSocketTransport.connect(options.wsEndpoint, async transport => {
if ((options as any).__testHookBeforeCreateBrowser)
await (options as any).__testHookBeforeCreateBrowser();
return FFBrowser.connect(transport, new RootLogger(options.logger), false, options.slowMo);
});
}

View File

@ -133,7 +133,9 @@ export class WebKit extends AbstractBrowserType<WKBrowser> {
}
async connect(options: ConnectOptions): Promise<WKBrowser> {
return await WebSocketTransport.connect(options.wsEndpoint, transport => {
return await WebSocketTransport.connect(options.wsEndpoint, async transport => {
if ((options as any).__testHookBeforeCreateBrowser)
await (options as any).__testHookBeforeCreateBrowser();
return WKBrowser.connect(transport, new RootLogger(options.logger), options.slowMo);
});
}

View File

@ -129,7 +129,14 @@ export class WebSocketTransport implements ConnectionTransport {
static connect<T>(url: string, onopen: (transport: ConnectionTransport) => Promise<T> | T): Promise<T> {
const transport = new WebSocketTransport(url);
return new Promise<T>((fulfill, reject) => {
transport._ws.addEventListener('open', async () => fulfill(await onopen(transport)));
transport._ws.addEventListener('open', async () => {
try {
fulfill(await onopen(transport));
} catch (e) {
try { transport._ws.close(); } catch (ignored) {}
reject(e);
}
});
transport._ws.addEventListener('error', event => reject(new Error('WebSocket error: ' + event.message)));
});
}

View File

@ -290,6 +290,15 @@ describe('browserType.connect', function() {
await browserServer._checkLeaks();
await browserServer.close();
});
it.slow()('should handle exceptions during connect', async({browserType, defaultBrowserOptions, server}) => {
const browserServer = await browserType.launchServer(defaultBrowserOptions);
const e = new Error('Dummy');
const __testHookBeforeCreateBrowser = () => { throw e };
const error = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint(), __testHookBeforeCreateBrowser }).catch(e => e);
await browserServer._checkLeaks();
await browserServer.close();
expect(error).toBe(e);
});
});
describe('browserType.launchPersistentContext', function() {