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,
errorPromise,
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;
} finally {

View File

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

View File

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