chore: PlaywrightClient/Server enhancements (#7980)
- ensure timeout results in a meaningful message - add onDisconnect handler
This commit is contained in:
parent
4e8e75beb1
commit
869f8d541b
|
|
@ -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 {
|
||||||
|
|
|
||||||
|
|
@ -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?.();
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -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}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue