test: move browser._launchServer in child process (#37114)

Co-authored-by: Max Schmitt <max@schmitt.mx>
This commit is contained in:
Simon Knott 2025-08-19 15:58:02 +02:00 committed by GitHub
parent a5daba92ac
commit face8d44ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 13 deletions

View File

@ -2,7 +2,8 @@ const fs = require('fs');
const cluster = require('cluster');
async function start() {
const { browserTypeName, launchOptions, stallOnClose, disconnectOnSIGHUP, exitOnFile, exitOnWarning, startStopAndRunHttp } = JSON.parse(process.argv[2]);
/** @type {import("./remoteServer").RemoteServerOptions} */
const { browserTypeName, launchOptions, stallOnClose, disconnectOnSIGHUP, exitOnFile, exitOnWarning, startStopAndRunHttp, existingBrowser } = JSON.parse(process.argv[2]);
if (stallOnClose) {
launchOptions.__testHookGracefullyClose = () => {
console.log(`(stalled=>true)`);
@ -25,7 +26,15 @@ async function start() {
return;
}
const browserServer = await playwright[browserTypeName].launchServer(launchOptions);
let browserServer;
if (existingBrowser) {
const browser = await playwright[browserTypeName].launch(launchOptions);
const page = await browser.newPage();
await page.setContent(existingBrowser.content);
browserServer = await browser._launchServer();
} else {
browserServer = await playwright[browserTypeName].launchServer(launchOptions);
}
if (disconnectOnSIGHUP)
process.on('SIGHUP', () => browserServer._disconnectForTest());

View File

@ -69,6 +69,7 @@ export type RemoteServerOptions = {
url?: string;
startStopAndRunHttp?: boolean;
sharedBrowser?: boolean;
existingBrowser?: { content: string };
};
export class RemoteServer implements PlaywrightServer {

View File

@ -1047,18 +1047,12 @@ test.describe('launchServer only', () => {
await expect(browser._parent.launch({ timeout: 0 })).rejects.toThrowError('Launching more browsers is not allowed.');
});
test('should work with existing browser', async ({ connect, browserType, mode }) => {
test('should work with existing browser', async ({ connect, startRemoteServer, mode }) => {
test.skip(mode === 'driver', 'Driver mode does not support browserType.launchServer');
// can't use browser fixture because it's shared across the worker, launching a server on that would infect other tests
const browser = await browserType.launch();
const page = await browser.newPage();
await page.setContent('hello world');
const server = await (browser as any)._launchServer();
const secondBrowser = await connect(server.wsEndpoint());
const secondPage = secondBrowser.contexts()[0].pages()[0];
expect(await secondPage.content()).toContain('hello world');
await server.close();
await browser.close();
const remoteServer = await startRemoteServer('launchServer', { existingBrowser: { content: 'hello world' } });
const browser = await connect(remoteServer.wsEndpoint());
const page = browser.contexts()[0].pages()[0];
expect(await page.content()).toContain('hello world');
});
});