chore: open browser for reuse, destroy run server on detach (#16183)

This commit is contained in:
Pavel Feldman 2022-08-02 21:09:39 -07:00 committed by GitHub
parent bdba9dbaf9
commit df9a318d14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 6 deletions

View File

@ -39,14 +39,10 @@ export function runDriver() {
const transport = process.send ? new IpcTransport(process) : new PipeTransport(process.stdout, process.stdin);
transport.onmessage = message => dispatcherConnection.dispatch(JSON.parse(message));
dispatcherConnection.onmessage = message => transport.send(JSON.stringify(message));
transport.onclose = async () => {
transport.onclose = () => {
// Drop any messages during shutdown on the floor.
dispatcherConnection.onmessage = () => {};
// Force exit after 30 seconds.
setTimeout(() => process.exit(0), 30000);
// Meanwhile, try to gracefully close all browsers.
await gracefullyCloseAll();
process.exit(0);
selfDestruct();
};
}
@ -55,6 +51,11 @@ export async function runServer(port: number | undefined, path = '/', maxClients
const wsEndpoint = await server.listen(port);
process.on('exit', () => server.close().catch(console.error));
console.log('Listening on ' + wsEndpoint); // eslint-disable-line no-console
process.stdin.on('close', () => selfDestruct());
process.stdin.on('data', data => {
if (data.toString() === '<EOL>')
selfDestruct();
});
}
export async function launchBrowserServer(browserName: string, configFile?: string) {
@ -65,3 +66,12 @@ export async function launchBrowserServer(browserName: string, configFile?: stri
const server = await browserType.launchServer(options);
console.log(server.wsEndpoint());
}
function selfDestruct() {
// Force exit after 30 seconds.
setTimeout(() => process.exit(0), 30000);
// Meanwhile, try to gracefully close all browsers.
gracefullyCloseAll().then(() => {
process.exit(0);
});
}

View File

@ -22,6 +22,7 @@ import type { Playwright } from '../server/playwright';
import { createPlaywright } from '../server/playwright';
import { PlaywrightConnection } from './playwrightConnection';
import { assert } from '../utils';
import { serverSideCallMetadata } from '../server/instrumentation';
const debugLog = debug('pw:server');
@ -61,6 +62,27 @@ export class PlaywrightServer {
}
async listen(port: number = 0): Promise<string> {
if (this._mode === 'reuse-browser') {
const callMetadata = serverSideCallMetadata();
const browser = await this._preLaunchedPlaywright!.chromium.launch(callMetadata, { headless: false });
const { context } = await browser.newContextForReuse({ viewport: { width: 800, height: 600 } }, callMetadata);
const page = await context.newPage(callMetadata);
await page.mainFrame().setContent(callMetadata, `
<style>
html, body {
width: 100%;
height: 100%;
}
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
</style>
<div>Playwright will use this page to run tests</div>`);
}
const server = http.createServer((request, response) => {
response.end('Running');
});