chore: PlaywrightClient/Server enhancements (#7980)

- ensure timeout results in a meaningful message
- add onDisconnect handler
This commit is contained in:
Max Schmitt 2021-08-04 19:45:33 +02:00 committed by GitHub
parent 4e8e75beb1
commit 869f8d541b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 6 deletions

View file

@ -49,7 +49,7 @@ export class PlaywrightClient {
playwrightClientPromise, playwrightClientPromise,
errorPromise, errorPromise,
closePromise, closePromise,
new Promise((_, reject) => timer = setTimeout(reject, timeout)) new Promise((_, reject) => timer = setTimeout(() => reject(`Timeout of ${timeout}ms exceeded while connecting.`), timeout))
]); ]);
return await playwrightClientPromise; return await playwrightClientPromise;
} finally { } finally {

View file

@ -33,6 +33,7 @@ export interface PlaywrightServerDelegate {
export type PlaywrightServerOptions = { export type PlaywrightServerOptions = {
acceptForwardedPorts?: boolean acceptForwardedPorts?: boolean
onDisconnect?: () => void;
}; };
export class PlaywrightServer { export class PlaywrightServer {
@ -40,7 +41,7 @@ export class PlaywrightServer {
private _clientsCount = 0; private _clientsCount = 0;
private _delegate: PlaywrightServerDelegate; private _delegate: PlaywrightServerDelegate;
static async startDefault({ acceptForwardedPorts }: PlaywrightServerOptions = {}): Promise<PlaywrightServer> { static async startDefault({ acceptForwardedPorts, onDisconnect }: PlaywrightServerOptions = {}): Promise<PlaywrightServer> {
const cleanup = async () => { const cleanup = async () => {
await gracefullyCloseAll().catch(e => {}); await gracefullyCloseAll().catch(e => {});
}; };
@ -57,6 +58,7 @@ export class PlaywrightServer {
cleanup(); cleanup();
playwright._disablePortForwarding(); playwright._disablePortForwarding();
playwright.selectors.unregisterAll(); playwright.selectors.unregisterAll();
onDisconnect?.();
}; };
}, },
}; };

View file

@ -57,7 +57,6 @@ class DriverMode {
} }
class ServiceMode { class ServiceMode {
private _playwrightObejct: any;
private _client: any; private _client: any;
private _serviceProcess: childProcess.ChildProcess; private _serviceProcess: childProcess.ChildProcess;
@ -75,8 +74,7 @@ class ServiceMode {
}); });
this._serviceProcess.on('exit', this._onExit); this._serviceProcess.on('exit', this._onExit);
this._client = await PlaywrightClient.connect({wsEndpoint: `ws://localhost:${port}/ws`}); this._client = await PlaywrightClient.connect({wsEndpoint: `ws://localhost:${port}/ws`});
this._playwrightObejct = this._client.playwright(); return this._client.playwright();
return this._playwrightObejct;
} }
async teardown() { async teardown() {
@ -87,7 +85,7 @@ class ServiceMode {
await processExited; await processExited;
} }
private _onExit(exitCode, signal) { private _onExit(exitCode: number, signal: string) {
throw new Error(`Server closed with exitCode=${exitCode} signal=${signal}`); throw new Error(`Server closed with exitCode=${exitCode} signal=${signal}`);
} }
} }