mirror of
https://github.com/microsoft/playwright.git
synced 2024-09-21 01:17:43 +03:00
tests: run browserType.connect tests against launchServer and run-server (#19340)
This commit is contained in:
parent
7aa3935dcc
commit
622c1a8da6
@ -21,8 +21,7 @@ import * as path from 'path';
|
||||
import type { BrowserContext, BrowserContextOptions, BrowserType, Page } from 'playwright-core';
|
||||
import { removeFolders } from '../../packages/playwright-core/lib/utils/fileUtils';
|
||||
import { baseTest } from './baseTest';
|
||||
import type { RemoteServerOptions } from './remoteServer';
|
||||
import { RemoteServer } from './remoteServer';
|
||||
import { type RemoteServerOptions, type PlaywrightServer, RunServer, RemoteServer } from './remoteServer';
|
||||
import type { Log } from '../../packages/trace/src/har';
|
||||
import { parseHar } from '../config/utils';
|
||||
|
||||
@ -36,10 +35,15 @@ export type BrowserTestWorkerFixtures = PageWorkerFixtures & {
|
||||
isElectron: boolean;
|
||||
};
|
||||
|
||||
interface StartRemoteServer {
|
||||
(kind: 'run-server' | 'launchServer'): Promise<PlaywrightServer>;
|
||||
(kind: 'launchServer', options?: RemoteServerOptions): Promise<RemoteServer>;
|
||||
}
|
||||
|
||||
type BrowserTestTestFixtures = PageTestFixtures & {
|
||||
createUserDataDir: () => Promise<string>;
|
||||
launchPersistent: (options?: Parameters<BrowserType['launchPersistentContext']>[1]) => Promise<{ context: BrowserContext, page: Page }>;
|
||||
startRemoteServer: (options?: RemoteServerOptions) => Promise<RemoteServer>;
|
||||
startRemoteServer: StartRemoteServer;
|
||||
contextFactory: (options?: BrowserContextOptions) => Promise<BrowserContext>;
|
||||
pageWithHar(options?: { outputPath?: string, content?: 'embed' | 'attach' | 'omit', omitContent?: boolean }): Promise<{ context: BrowserContext, page: Page, getLog: () => Promise<Log>, getZip: () => Promise<Map<string, Buffer>> }>
|
||||
};
|
||||
@ -118,16 +122,24 @@ const test = baseTest.extend<BrowserTestTestFixtures, BrowserTestWorkerFixtures>
|
||||
},
|
||||
|
||||
startRemoteServer: async ({ childProcess, browserType }, run) => {
|
||||
let remoteServer: RemoteServer | undefined;
|
||||
await run(async options => {
|
||||
if (remoteServer)
|
||||
let server: PlaywrightServer | undefined;
|
||||
const fn = async (kind: 'launchServer' | 'run-server', options?: RemoteServerOptions) => {
|
||||
if (server)
|
||||
throw new Error('can only start one remote server');
|
||||
remoteServer = new RemoteServer();
|
||||
await remoteServer._start(childProcess, browserType, options);
|
||||
return remoteServer;
|
||||
});
|
||||
if (remoteServer) {
|
||||
await remoteServer.close();
|
||||
if (kind === 'launchServer') {
|
||||
const remoteServer = new RemoteServer();
|
||||
await remoteServer._start(childProcess, browserType, options);
|
||||
server = remoteServer;
|
||||
} else {
|
||||
const runServer = new RunServer();
|
||||
await runServer._start(childProcess);
|
||||
server = runServer;
|
||||
}
|
||||
return server;
|
||||
};
|
||||
await run(fn as any);
|
||||
if (server) {
|
||||
await server.close();
|
||||
// Give any connected browsers a chance to disconnect to avoid
|
||||
// poisoning next test with quasy-alive browsers.
|
||||
await new Promise(f => setTimeout(f, 1000));
|
||||
|
@ -31,7 +31,7 @@ export class TestChildProcess {
|
||||
params: TestChildParams;
|
||||
process: ChildProcess;
|
||||
output = '';
|
||||
onOutput?: () => void;
|
||||
onOutput?: (chunk: string | Buffer) => void;
|
||||
exited: Promise<{ exitCode: number, signal: string | null }>;
|
||||
exitCode: Promise<number>;
|
||||
|
||||
@ -59,7 +59,7 @@ export class TestChildProcess {
|
||||
this.output += String(chunk);
|
||||
if (process.env.PWTEST_DEBUG)
|
||||
process.stdout.write(String(chunk));
|
||||
this.onOutput?.();
|
||||
this.onOutput?.(chunk);
|
||||
for (const cb of this._outputCallbacks)
|
||||
cb();
|
||||
this._outputCallbacks.clear();
|
||||
|
@ -18,6 +18,42 @@ import path from 'path';
|
||||
import type { BrowserType, Browser, LaunchOptions } from 'playwright-core';
|
||||
import type { CommonFixtures, TestChildProcess } from './commonFixtures';
|
||||
|
||||
export interface PlaywrightServer {
|
||||
wsEndpoint(): string;
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
export class RunServer implements PlaywrightServer {
|
||||
private _process: TestChildProcess;
|
||||
_wsEndpoint: string;
|
||||
|
||||
async _start(childProcess: CommonFixtures['childProcess']) {
|
||||
this._process = childProcess({
|
||||
command: ['node', path.join(__dirname, '..', '..', 'packages', 'playwright-core', 'lib', 'cli', 'cli.js'), 'run-server'],
|
||||
});
|
||||
|
||||
let wsEndpointCallback;
|
||||
const wsEndpointPromise = new Promise<string>(f => wsEndpointCallback = f);
|
||||
this._process.onOutput = data => {
|
||||
const prefix = 'Listening on ';
|
||||
const line = data.toString();
|
||||
if (line.startsWith(prefix))
|
||||
wsEndpointCallback(line.substr(prefix.length));
|
||||
};
|
||||
|
||||
this._wsEndpoint = await wsEndpointPromise;
|
||||
}
|
||||
|
||||
wsEndpoint() {
|
||||
return this._wsEndpoint;
|
||||
}
|
||||
|
||||
async close() {
|
||||
await this._process.close();
|
||||
await this._process.exitCode;
|
||||
}
|
||||
}
|
||||
|
||||
export type RemoteServerOptions = {
|
||||
stallOnClose?: boolean;
|
||||
disconnectOnSIGHUP?: boolean;
|
||||
@ -26,7 +62,7 @@ export type RemoteServerOptions = {
|
||||
url?: string;
|
||||
};
|
||||
|
||||
export class RemoteServer {
|
||||
export class RemoteServer implements PlaywrightServer {
|
||||
private _process: TestChildProcess;
|
||||
_output: Map<string, string>;
|
||||
_outputCallback: Map<string, () => void>;
|
||||
@ -114,6 +150,6 @@ export class RemoteServer {
|
||||
this._browser = undefined;
|
||||
}
|
||||
await this._process.close();
|
||||
return await this.childExitCode();
|
||||
await this.childExitCode();
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,202 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import childProcess from 'child_process';
|
||||
import http from 'http';
|
||||
import path from 'path';
|
||||
import type net from 'net';
|
||||
|
||||
import { contextTest, expect } from '../config/browserTest';
|
||||
import type { Page, Browser } from 'playwright-core';
|
||||
|
||||
class OutOfProcessPlaywrightServer {
|
||||
private _driverProcess: childProcess.ChildProcess;
|
||||
private _receivedPortPromise: Promise<string>;
|
||||
|
||||
constructor(port: number, proxyPort: number) {
|
||||
this._driverProcess = childProcess.fork(path.join(__dirname, '..', '..', 'packages', 'playwright-core', 'lib', 'cli', 'cli.js'), ['run-server', '--port', port.toString()], {
|
||||
stdio: 'pipe',
|
||||
detached: true,
|
||||
env: {
|
||||
...process.env
|
||||
}
|
||||
});
|
||||
this._driverProcess.unref();
|
||||
this._receivedPortPromise = new Promise<string>((resolve, reject) => {
|
||||
this._driverProcess.stdout.on('data', (data: Buffer) => {
|
||||
const prefix = 'Listening on ';
|
||||
const line = data.toString();
|
||||
if (line.startsWith(prefix))
|
||||
resolve(line.substr(prefix.length));
|
||||
});
|
||||
this._driverProcess.stderr.on('data', (data: Buffer) => {
|
||||
console.log(data.toString());
|
||||
});
|
||||
this._driverProcess.on('exit', () => reject());
|
||||
});
|
||||
}
|
||||
async kill() {
|
||||
const waitForExit = new Promise<void>(resolve => this._driverProcess.on('exit', () => resolve()));
|
||||
this._driverProcess.kill('SIGKILL');
|
||||
await waitForExit;
|
||||
}
|
||||
public async wsEndpoint(): Promise<string> {
|
||||
return await this._receivedPortPromise;
|
||||
}
|
||||
}
|
||||
|
||||
const it = contextTest.extend<{ pageFactory: (redirectPortForTest?: number, pattern?: string) => Promise<Page> }>({
|
||||
pageFactory: async ({ browserType, browserName, channel }, run, testInfo) => {
|
||||
const playwrightServers: OutOfProcessPlaywrightServer[] = [];
|
||||
const browsers: Browser[] = [];
|
||||
await run(async (redirectPortForTest?: number, pattern = '*'): Promise<Page> => {
|
||||
const server = new OutOfProcessPlaywrightServer(0, 3200 + testInfo.workerIndex);
|
||||
playwrightServers.push(server);
|
||||
const browser = await browserType.connect({
|
||||
wsEndpoint: await server.wsEndpoint() + `?proxy=${pattern}&browser=` + browserName,
|
||||
headers: { 'x-playwright-launch-options': JSON.stringify({ channel }) },
|
||||
__testHookRedirectPortForwarding: redirectPortForTest,
|
||||
} as any);
|
||||
browsers.push(browser);
|
||||
return await browser.newPage();
|
||||
});
|
||||
for (const playwrightServer of playwrightServers)
|
||||
await playwrightServer.kill();
|
||||
await Promise.all(browsers.map(async browser => {
|
||||
if (browser.isConnected())
|
||||
await new Promise(f => browser.once('disconnected', f));
|
||||
}));
|
||||
},
|
||||
});
|
||||
|
||||
it.fixme(({ platform, browserName }) => browserName === 'webkit' && platform === 'win32');
|
||||
it.skip(({ mode }) => mode !== 'default');
|
||||
|
||||
async function startTestServer() {
|
||||
const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
|
||||
res.end('<html><body>from-retargeted-server</body></html>');
|
||||
});
|
||||
await new Promise<void>(resolve => server.listen(0, resolve));
|
||||
return {
|
||||
testServerPort: (server.address() as net.AddressInfo).port,
|
||||
stopTestServer: () => server.close()
|
||||
};
|
||||
}
|
||||
|
||||
it('should forward non-forwarded requests', async ({ pageFactory, server }) => {
|
||||
let reachedOriginalTarget = false;
|
||||
server.setRoute('/foo.html', async (req, res) => {
|
||||
reachedOriginalTarget = true;
|
||||
res.end('<html><body>original-target</body></html>');
|
||||
});
|
||||
const page = await pageFactory();
|
||||
await page.goto(server.PREFIX + '/foo.html');
|
||||
expect(await page.content()).toContain('original-target');
|
||||
expect(reachedOriginalTarget).toBe(true);
|
||||
});
|
||||
|
||||
it('should proxy localhost requests @smoke', async ({ pageFactory, server, browserName, platform }, workerInfo) => {
|
||||
it.skip(browserName === 'webkit' && platform === 'darwin');
|
||||
const { testServerPort, stopTestServer } = await startTestServer();
|
||||
let reachedOriginalTarget = false;
|
||||
server.setRoute('/foo.html', async (req, res) => {
|
||||
reachedOriginalTarget = true;
|
||||
res.end('<html><body></body></html>');
|
||||
});
|
||||
const examplePort = 20_000 + workerInfo.workerIndex * 3;
|
||||
const page = await pageFactory(testServerPort);
|
||||
await page.goto(`http://127.0.0.1:${examplePort}/foo.html`);
|
||||
expect(await page.content()).toContain('from-retargeted-server');
|
||||
expect(reachedOriginalTarget).toBe(false);
|
||||
stopTestServer();
|
||||
});
|
||||
|
||||
it('should proxy localhost requests from fetch api', async ({ pageFactory, server, browserName, platform }, workerInfo) => {
|
||||
it.skip(browserName === 'webkit' && platform === 'darwin');
|
||||
|
||||
const { testServerPort, stopTestServer } = await startTestServer();
|
||||
let reachedOriginalTarget = false;
|
||||
server.setRoute('/foo.html', async (req, res) => {
|
||||
reachedOriginalTarget = true;
|
||||
res.end('<html><body></body></html>');
|
||||
});
|
||||
const examplePort = 20_000 + workerInfo.workerIndex * 3;
|
||||
const page = await pageFactory(testServerPort);
|
||||
const response = await page.request.get(`http://127.0.0.1:${examplePort}/foo.html`);
|
||||
expect(response.status()).toBe(200);
|
||||
expect(await response.text()).toContain('from-retargeted-server');
|
||||
expect(reachedOriginalTarget).toBe(false);
|
||||
stopTestServer();
|
||||
});
|
||||
|
||||
it('should proxy local.playwright requests', async ({ pageFactory, server, browserName }, workerInfo) => {
|
||||
const { testServerPort, stopTestServer } = await startTestServer();
|
||||
let reachedOriginalTarget = false;
|
||||
server.setRoute('/foo.html', async (req, res) => {
|
||||
reachedOriginalTarget = true;
|
||||
res.end('<html><body></body></html>');
|
||||
});
|
||||
const examplePort = 20_000 + workerInfo.workerIndex * 3;
|
||||
const page = await pageFactory(testServerPort);
|
||||
await page.goto(`http://local.playwright:${examplePort}/foo.html`);
|
||||
expect(await page.content()).toContain('from-retargeted-server');
|
||||
expect(reachedOriginalTarget).toBe(false);
|
||||
stopTestServer();
|
||||
});
|
||||
|
||||
it('should lead to the error page for forwarded requests when the connection is refused', async ({ pageFactory, browserName }, workerInfo) => {
|
||||
const examplePort = 20_000 + workerInfo.workerIndex * 3;
|
||||
const page = await pageFactory();
|
||||
const error = await page.goto(`http://127.0.0.1:${examplePort}`).catch(e => e);
|
||||
if (browserName === 'chromium')
|
||||
expect(error.message).toContain('net::ERR_SOCKS_CONNECTION_FAILED at http://127.0.0.1:20');
|
||||
else if (browserName === 'webkit')
|
||||
expect(error.message).toBeTruthy();
|
||||
else if (browserName === 'firefox')
|
||||
expect(error.message.includes('NS_ERROR_NET_RESET') || error.message.includes('NS_ERROR_CONNECTION_REFUSED')).toBe(true);
|
||||
});
|
||||
|
||||
it('should proxy based on the pattern', async ({ pageFactory, server, browserName, platform }, workerInfo) => {
|
||||
it.skip(browserName === 'webkit' && platform === 'darwin');
|
||||
|
||||
const { testServerPort, stopTestServer } = await startTestServer();
|
||||
let reachedOriginalTarget = false;
|
||||
server.setRoute('/foo.html', async (req, res) => {
|
||||
reachedOriginalTarget = true;
|
||||
res.end('<html><body>from-original-server</body></html>');
|
||||
});
|
||||
const examplePort = 20_000 + workerInfo.workerIndex * 3;
|
||||
const page = await pageFactory(testServerPort, 'localhost');
|
||||
|
||||
// localhost should be proxied.
|
||||
await page.goto(`http://localhost:${examplePort}/foo.html`);
|
||||
expect(await page.content()).toContain('from-retargeted-server');
|
||||
expect(reachedOriginalTarget).toBe(false);
|
||||
|
||||
// 127.0.0.1 should be served directly.
|
||||
await page.goto(`http://127.0.0.1:${server.PORT}/foo.html`);
|
||||
expect(await page.content()).toContain('from-original-server');
|
||||
expect(reachedOriginalTarget).toBe(true);
|
||||
|
||||
// Random domain should be served directly and fail.
|
||||
let failed = false;
|
||||
await page.goto(`http://does-not-exist-bad-domain.oh-no-should-not-work`).catch(e => {
|
||||
failed = true;
|
||||
});
|
||||
expect(failed).toBe(true);
|
||||
|
||||
stopTestServer();
|
||||
});
|
@ -22,7 +22,7 @@ import fs from 'fs';
|
||||
test.slow();
|
||||
|
||||
test('should close the browser when the node process closes', async ({ startRemoteServer, isWindows, server }) => {
|
||||
const remoteServer = await startRemoteServer({ url: server.EMPTY_PAGE });
|
||||
const remoteServer = await startRemoteServer('launchServer', { url: server.EMPTY_PAGE });
|
||||
try {
|
||||
if (isWindows)
|
||||
execSync(`taskkill /pid ${remoteServer.child().pid} /T /F`, { stdio: 'ignore' });
|
||||
@ -43,7 +43,7 @@ test('should close the browser when the node process closes', async ({ startRemo
|
||||
|
||||
test('should remove temp dir on process.exit', async ({ startRemoteServer, server }, testInfo) => {
|
||||
const file = testInfo.outputPath('exit.file');
|
||||
const remoteServer = await startRemoteServer({ url: server.EMPTY_PAGE, exitOnFile: file });
|
||||
const remoteServer = await startRemoteServer('launchServer', { url: server.EMPTY_PAGE, exitOnFile: file });
|
||||
const tempDir = await remoteServer.out('tempDir');
|
||||
const before = fs.existsSync(tempDir);
|
||||
fs.writeFileSync(file, 'data', 'utf-8');
|
||||
@ -59,7 +59,7 @@ test.describe('signals', () => {
|
||||
test('should report browser close signal', async ({ startRemoteServer, server, headless }) => {
|
||||
test.skip(!headless, 'Wrong exit code in headed');
|
||||
|
||||
const remoteServer = await startRemoteServer({ url: server.EMPTY_PAGE });
|
||||
const remoteServer = await startRemoteServer('launchServer', { url: server.EMPTY_PAGE });
|
||||
const pid = await remoteServer.out('pid');
|
||||
process.kill(-pid, 'SIGTERM');
|
||||
expect(await remoteServer.out('exitCode')).toBe('null');
|
||||
@ -69,7 +69,7 @@ test.describe('signals', () => {
|
||||
});
|
||||
|
||||
test('should report browser close signal 2', async ({ startRemoteServer, server }) => {
|
||||
const remoteServer = await startRemoteServer({ url: server.EMPTY_PAGE });
|
||||
const remoteServer = await startRemoteServer('launchServer', { url: server.EMPTY_PAGE });
|
||||
const pid = await remoteServer.out('pid');
|
||||
process.kill(-pid, 'SIGKILL');
|
||||
expect(await remoteServer.out('exitCode')).toBe('null');
|
||||
@ -79,7 +79,7 @@ test.describe('signals', () => {
|
||||
});
|
||||
|
||||
test('should close the browser on SIGINT', async ({ startRemoteServer, server }) => {
|
||||
const remoteServer = await startRemoteServer({ url: server.EMPTY_PAGE });
|
||||
const remoteServer = await startRemoteServer('launchServer', { url: server.EMPTY_PAGE });
|
||||
process.kill(remoteServer.child().pid, 'SIGINT');
|
||||
expect(await remoteServer.out('exitCode')).toBe('0');
|
||||
expect(await remoteServer.out('signal')).toBe('null');
|
||||
@ -87,7 +87,7 @@ test.describe('signals', () => {
|
||||
});
|
||||
|
||||
test('should close the browser on SIGTERM', async ({ startRemoteServer, server }) => {
|
||||
const remoteServer = await startRemoteServer({ url: server.EMPTY_PAGE });
|
||||
const remoteServer = await startRemoteServer('launchServer', { url: server.EMPTY_PAGE });
|
||||
process.kill(remoteServer.child().pid, 'SIGTERM');
|
||||
expect(await remoteServer.out('exitCode')).toBe('0');
|
||||
expect(await remoteServer.out('signal')).toBe('null');
|
||||
@ -95,7 +95,7 @@ test.describe('signals', () => {
|
||||
});
|
||||
|
||||
test('should close the browser on SIGHUP', async ({ startRemoteServer, server }) => {
|
||||
const remoteServer = await startRemoteServer({ url: server.EMPTY_PAGE });
|
||||
const remoteServer = await startRemoteServer('launchServer', { url: server.EMPTY_PAGE });
|
||||
process.kill(remoteServer.child().pid, 'SIGHUP');
|
||||
expect(await remoteServer.out('exitCode')).toBe('0');
|
||||
expect(await remoteServer.out('signal')).toBe('null');
|
||||
@ -103,7 +103,7 @@ test.describe('signals', () => {
|
||||
});
|
||||
|
||||
test('should kill the browser on double SIGINT and remove temp dir', async ({ startRemoteServer, server }) => {
|
||||
const remoteServer = await startRemoteServer({ stallOnClose: true, url: server.EMPTY_PAGE });
|
||||
const remoteServer = await startRemoteServer('launchServer', { stallOnClose: true, url: server.EMPTY_PAGE });
|
||||
const tempDir = await remoteServer.out('tempDir');
|
||||
const before = fs.existsSync(tempDir);
|
||||
process.kill(remoteServer.child().pid, 'SIGINT');
|
||||
@ -118,7 +118,7 @@ test.describe('signals', () => {
|
||||
});
|
||||
|
||||
test('should kill the browser on SIGINT + SIGTERM', async ({ startRemoteServer, server }) => {
|
||||
const remoteServer = await startRemoteServer({ stallOnClose: true, url: server.EMPTY_PAGE });
|
||||
const remoteServer = await startRemoteServer('launchServer', { stallOnClose: true, url: server.EMPTY_PAGE });
|
||||
process.kill(remoteServer.child().pid, 'SIGINT');
|
||||
await remoteServer.out('stalled');
|
||||
process.kill(remoteServer.child().pid, 'SIGTERM');
|
||||
@ -128,7 +128,7 @@ test.describe('signals', () => {
|
||||
});
|
||||
|
||||
test('should kill the browser on SIGTERM + SIGINT', async ({ startRemoteServer, server }) => {
|
||||
const remoteServer = await startRemoteServer({ stallOnClose: true, url: server.EMPTY_PAGE });
|
||||
const remoteServer = await startRemoteServer('launchServer', { stallOnClose: true, url: server.EMPTY_PAGE });
|
||||
process.kill(remoteServer.child().pid, 'SIGTERM');
|
||||
await remoteServer.out('stalled');
|
||||
process.kill(remoteServer.child().pid, 'SIGINT');
|
||||
|
@ -281,7 +281,7 @@ export class TestServer {
|
||||
}
|
||||
}
|
||||
|
||||
onceWebSocketConnection(handler) {
|
||||
onceWebSocketConnection(handler: (socket: ws.WebSocket, request: http.IncomingMessage) => void) {
|
||||
this._wsServer.once('connection', handler);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user