mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: open browser for reuse, destroy run server on detach (#16183)
This commit is contained in:
parent
bdba9dbaf9
commit
df9a318d14
@ -39,14 +39,10 @@ export function runDriver() {
|
|||||||
const transport = process.send ? new IpcTransport(process) : new PipeTransport(process.stdout, process.stdin);
|
const transport = process.send ? new IpcTransport(process) : new PipeTransport(process.stdout, process.stdin);
|
||||||
transport.onmessage = message => dispatcherConnection.dispatch(JSON.parse(message));
|
transport.onmessage = message => dispatcherConnection.dispatch(JSON.parse(message));
|
||||||
dispatcherConnection.onmessage = message => transport.send(JSON.stringify(message));
|
dispatcherConnection.onmessage = message => transport.send(JSON.stringify(message));
|
||||||
transport.onclose = async () => {
|
transport.onclose = () => {
|
||||||
// Drop any messages during shutdown on the floor.
|
// Drop any messages during shutdown on the floor.
|
||||||
dispatcherConnection.onmessage = () => {};
|
dispatcherConnection.onmessage = () => {};
|
||||||
// Force exit after 30 seconds.
|
selfDestruct();
|
||||||
setTimeout(() => process.exit(0), 30000);
|
|
||||||
// Meanwhile, try to gracefully close all browsers.
|
|
||||||
await gracefullyCloseAll();
|
|
||||||
process.exit(0);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,6 +51,11 @@ export async function runServer(port: number | undefined, path = '/', maxClients
|
|||||||
const wsEndpoint = await server.listen(port);
|
const wsEndpoint = await server.listen(port);
|
||||||
process.on('exit', () => server.close().catch(console.error));
|
process.on('exit', () => server.close().catch(console.error));
|
||||||
console.log('Listening on ' + wsEndpoint); // eslint-disable-line no-console
|
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) {
|
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);
|
const server = await browserType.launchServer(options);
|
||||||
console.log(server.wsEndpoint());
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@ -22,6 +22,7 @@ import type { Playwright } from '../server/playwright';
|
|||||||
import { createPlaywright } from '../server/playwright';
|
import { createPlaywright } from '../server/playwright';
|
||||||
import { PlaywrightConnection } from './playwrightConnection';
|
import { PlaywrightConnection } from './playwrightConnection';
|
||||||
import { assert } from '../utils';
|
import { assert } from '../utils';
|
||||||
|
import { serverSideCallMetadata } from '../server/instrumentation';
|
||||||
|
|
||||||
const debugLog = debug('pw:server');
|
const debugLog = debug('pw:server');
|
||||||
|
|
||||||
@ -61,6 +62,27 @@ export class PlaywrightServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async listen(port: number = 0): Promise<string> {
|
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) => {
|
const server = http.createServer((request, response) => {
|
||||||
response.end('Running');
|
response.end('Running');
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user