mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00: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 type { BrowserContext, BrowserContextOptions, BrowserType, Page } from 'playwright-core';
|
||||||
import { removeFolders } from '../../packages/playwright-core/lib/utils/fileUtils';
|
import { removeFolders } from '../../packages/playwright-core/lib/utils/fileUtils';
|
||||||
import { baseTest } from './baseTest';
|
import { baseTest } from './baseTest';
|
||||||
import type { RemoteServerOptions } from './remoteServer';
|
import { type RemoteServerOptions, type PlaywrightServer, RunServer, RemoteServer } from './remoteServer';
|
||||||
import { RemoteServer } from './remoteServer';
|
|
||||||
import type { Log } from '../../packages/trace/src/har';
|
import type { Log } from '../../packages/trace/src/har';
|
||||||
import { parseHar } from '../config/utils';
|
import { parseHar } from '../config/utils';
|
||||||
|
|
||||||
@ -36,10 +35,15 @@ export type BrowserTestWorkerFixtures = PageWorkerFixtures & {
|
|||||||
isElectron: boolean;
|
isElectron: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface StartRemoteServer {
|
||||||
|
(kind: 'run-server' | 'launchServer'): Promise<PlaywrightServer>;
|
||||||
|
(kind: 'launchServer', options?: RemoteServerOptions): Promise<RemoteServer>;
|
||||||
|
}
|
||||||
|
|
||||||
type BrowserTestTestFixtures = PageTestFixtures & {
|
type BrowserTestTestFixtures = PageTestFixtures & {
|
||||||
createUserDataDir: () => Promise<string>;
|
createUserDataDir: () => Promise<string>;
|
||||||
launchPersistent: (options?: Parameters<BrowserType['launchPersistentContext']>[1]) => Promise<{ context: BrowserContext, page: Page }>;
|
launchPersistent: (options?: Parameters<BrowserType['launchPersistentContext']>[1]) => Promise<{ context: BrowserContext, page: Page }>;
|
||||||
startRemoteServer: (options?: RemoteServerOptions) => Promise<RemoteServer>;
|
startRemoteServer: StartRemoteServer;
|
||||||
contextFactory: (options?: BrowserContextOptions) => Promise<BrowserContext>;
|
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>> }>
|
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) => {
|
startRemoteServer: async ({ childProcess, browserType }, run) => {
|
||||||
let remoteServer: RemoteServer | undefined;
|
let server: PlaywrightServer | undefined;
|
||||||
await run(async options => {
|
const fn = async (kind: 'launchServer' | 'run-server', options?: RemoteServerOptions) => {
|
||||||
if (remoteServer)
|
if (server)
|
||||||
throw new Error('can only start one remote server');
|
throw new Error('can only start one remote server');
|
||||||
remoteServer = new RemoteServer();
|
if (kind === 'launchServer') {
|
||||||
|
const remoteServer = new RemoteServer();
|
||||||
await remoteServer._start(childProcess, browserType, options);
|
await remoteServer._start(childProcess, browserType, options);
|
||||||
return remoteServer;
|
server = remoteServer;
|
||||||
});
|
} else {
|
||||||
if (remoteServer) {
|
const runServer = new RunServer();
|
||||||
await remoteServer.close();
|
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
|
// Give any connected browsers a chance to disconnect to avoid
|
||||||
// poisoning next test with quasy-alive browsers.
|
// poisoning next test with quasy-alive browsers.
|
||||||
await new Promise(f => setTimeout(f, 1000));
|
await new Promise(f => setTimeout(f, 1000));
|
||||||
|
@ -31,7 +31,7 @@ export class TestChildProcess {
|
|||||||
params: TestChildParams;
|
params: TestChildParams;
|
||||||
process: ChildProcess;
|
process: ChildProcess;
|
||||||
output = '';
|
output = '';
|
||||||
onOutput?: () => void;
|
onOutput?: (chunk: string | Buffer) => void;
|
||||||
exited: Promise<{ exitCode: number, signal: string | null }>;
|
exited: Promise<{ exitCode: number, signal: string | null }>;
|
||||||
exitCode: Promise<number>;
|
exitCode: Promise<number>;
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ export class TestChildProcess {
|
|||||||
this.output += String(chunk);
|
this.output += String(chunk);
|
||||||
if (process.env.PWTEST_DEBUG)
|
if (process.env.PWTEST_DEBUG)
|
||||||
process.stdout.write(String(chunk));
|
process.stdout.write(String(chunk));
|
||||||
this.onOutput?.();
|
this.onOutput?.(chunk);
|
||||||
for (const cb of this._outputCallbacks)
|
for (const cb of this._outputCallbacks)
|
||||||
cb();
|
cb();
|
||||||
this._outputCallbacks.clear();
|
this._outputCallbacks.clear();
|
||||||
|
@ -18,6 +18,42 @@ import path from 'path';
|
|||||||
import type { BrowserType, Browser, LaunchOptions } from 'playwright-core';
|
import type { BrowserType, Browser, LaunchOptions } from 'playwright-core';
|
||||||
import type { CommonFixtures, TestChildProcess } from './commonFixtures';
|
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 = {
|
export type RemoteServerOptions = {
|
||||||
stallOnClose?: boolean;
|
stallOnClose?: boolean;
|
||||||
disconnectOnSIGHUP?: boolean;
|
disconnectOnSIGHUP?: boolean;
|
||||||
@ -26,7 +62,7 @@ export type RemoteServerOptions = {
|
|||||||
url?: string;
|
url?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export class RemoteServer {
|
export class RemoteServer implements PlaywrightServer {
|
||||||
private _process: TestChildProcess;
|
private _process: TestChildProcess;
|
||||||
_output: Map<string, string>;
|
_output: Map<string, string>;
|
||||||
_outputCallback: Map<string, () => void>;
|
_outputCallback: Map<string, () => void>;
|
||||||
@ -114,6 +150,6 @@ export class RemoteServer {
|
|||||||
this._browser = undefined;
|
this._browser = undefined;
|
||||||
}
|
}
|
||||||
await this._process.close();
|
await this._process.close();
|
||||||
return await this.childExitCode();
|
await this.childExitCode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,18 +17,53 @@
|
|||||||
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
|
import http from 'http';
|
||||||
|
import type net from 'net';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import { getUserAgent } from '../../packages/playwright-core/lib/common/userAgent';
|
import { getUserAgent } from '../../packages/playwright-core/lib/common/userAgent';
|
||||||
import WebSocket from 'ws';
|
import WebSocket from 'ws';
|
||||||
import { expect, playwrightTest as test } from '../config/browserTest';
|
import { expect, playwrightTest } from '../config/browserTest';
|
||||||
import { parseTrace, suppressCertificateWarning } from '../config/utils';
|
import { parseTrace, suppressCertificateWarning } from '../config/utils';
|
||||||
import formidable from 'formidable';
|
import formidable from 'formidable';
|
||||||
|
import type { Browser, ConnectOptions } from 'playwright-core';
|
||||||
|
|
||||||
|
type ExtraFixtures = {
|
||||||
|
connect: (wsEndpoint: string, options?: ConnectOptions, redirectPortForTest?: number) => Promise<Browser>,
|
||||||
|
dummyServerPort: number,
|
||||||
|
};
|
||||||
|
const test = playwrightTest.extend<ExtraFixtures>({
|
||||||
|
connect: async ({ browserType }, use) => {
|
||||||
|
let browser: Browser | undefined;
|
||||||
|
await use(async (wsEndpoint, options = {}, redirectPortForTest): Promise<Browser> => {
|
||||||
|
(options as any).__testHookRedirectPortForwarding = redirectPortForTest;
|
||||||
|
options.headers = {
|
||||||
|
'x-playwright-launch-options': JSON.stringify((browserType as any)._defaultLaunchOptions || {}),
|
||||||
|
...options.headers,
|
||||||
|
};
|
||||||
|
browser = await browserType.connect(wsEndpoint, options);
|
||||||
|
return browser;
|
||||||
|
});
|
||||||
|
await browser?.close();
|
||||||
|
},
|
||||||
|
|
||||||
|
dummyServerPort: async ({}, use) => {
|
||||||
|
const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
|
||||||
|
res.end('<html><body>from-dummy-server</body></html>');
|
||||||
|
});
|
||||||
|
await new Promise<void>(resolve => server.listen(0, resolve));
|
||||||
|
await use((server.address() as net.AddressInfo).port);
|
||||||
|
await new Promise<Error>(resolve => server.close(resolve));
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
test.slow(true, 'All connect tests are slow');
|
test.slow(true, 'All connect tests are slow');
|
||||||
|
|
||||||
test('should connect over wss', async ({ browserType, startRemoteServer, httpsServer, mode }) => {
|
for (const kind of ['launchServer', 'run-server'] as const) {
|
||||||
|
test.describe(kind, () => {
|
||||||
|
|
||||||
|
test('should connect over wss', async ({ connect, startRemoteServer, httpsServer, mode }) => {
|
||||||
test.skip(mode !== 'default'); // Out of process transport does not allow us to set env vars dynamically.
|
test.skip(mode !== 'default'); // Out of process transport does not allow us to set env vars dynamically.
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
|
|
||||||
const oldValue = process.env['NODE_TLS_REJECT_UNAUTHORIZED'];
|
const oldValue = process.env['NODE_TLS_REJECT_UNAUTHORIZED'];
|
||||||
// https://stackoverflow.com/a/21961005/552185
|
// https://stackoverflow.com/a/21961005/552185
|
||||||
@ -36,9 +71,11 @@ test('should connect over wss', async ({ browserType, startRemoteServer, httpsSe
|
|||||||
suppressCertificateWarning();
|
suppressCertificateWarning();
|
||||||
try {
|
try {
|
||||||
httpsServer.onceWebSocketConnection((ws, request) => {
|
httpsServer.onceWebSocketConnection((ws, request) => {
|
||||||
|
const headers = Object.fromEntries(Object.entries(request.headers).filter(entry => entry[0].startsWith('x-playwright')));
|
||||||
const remote = new WebSocket(remoteServer.wsEndpoint(), [], {
|
const remote = new WebSocket(remoteServer.wsEndpoint(), [], {
|
||||||
perMessageDeflate: false,
|
perMessageDeflate: false,
|
||||||
maxPayload: 256 * 1024 * 1024, // 256Mb,
|
maxPayload: 256 * 1024 * 1024, // 256Mb,
|
||||||
|
headers,
|
||||||
});
|
});
|
||||||
const remoteReadyPromise = new Promise<void>((f, r) => {
|
const remoteReadyPromise = new Promise<void>((f, r) => {
|
||||||
remote.once('open', f);
|
remote.once('open', f);
|
||||||
@ -54,7 +91,7 @@ test('should connect over wss', async ({ browserType, startRemoteServer, httpsSe
|
|||||||
ws.on('close', () => remote.close());
|
ws.on('close', () => remote.close());
|
||||||
ws.on('error', () => remote.close());
|
ws.on('error', () => remote.close());
|
||||||
});
|
});
|
||||||
const browser = await browserType.connect(`wss://localhost:${httpsServer.PORT}/ws`);
|
const browser = await connect(`wss://localhost:${httpsServer.PORT}/ws`);
|
||||||
expect(browser.version()).toBeTruthy();
|
expect(browser.version()).toBeTruthy();
|
||||||
await browser.close();
|
await browser.close();
|
||||||
} finally {
|
} finally {
|
||||||
@ -62,17 +99,17 @@ test('should connect over wss', async ({ browserType, startRemoteServer, httpsSe
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should print HTTP error', async ({ browserType, server, mode }) => {
|
test('should print HTTP error', async ({ connect, server, mode }) => {
|
||||||
test.skip(mode !== 'default'); // Out of process transport does not allow us to set env vars dynamically.
|
test.skip(mode !== 'default'); // Out of process transport does not allow us to set env vars dynamically.
|
||||||
const error = await browserType.connect(`ws://localhost:${server.PORT}/ws-401`).catch(e => e);
|
const error = await connect(`ws://localhost:${server.PORT}/ws-401`).catch(e => e);
|
||||||
expect(error.message).toContain('401');
|
expect(error.message).toContain('401');
|
||||||
expect(error.message).toContain('Unauthorized body');
|
expect(error.message).toContain('Unauthorized body');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should be able to reconnect to a browser', async ({ browserType, startRemoteServer, server }) => {
|
test('should be able to reconnect to a browser', async ({ connect, startRemoteServer, server }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
{
|
{
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const browserContext = await browser.newContext();
|
const browserContext = await browser.newContext();
|
||||||
expect(browserContext.pages().length).toBe(0);
|
expect(browserContext.pages().length).toBe(0);
|
||||||
const page = await browserContext.newPage();
|
const page = await browserContext.newPage();
|
||||||
@ -81,7 +118,7 @@ test('should be able to reconnect to a browser', async ({ browserType, startRemo
|
|||||||
await browser.close();
|
await browser.close();
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const browserContext = await browser.newContext();
|
const browserContext = await browser.newContext();
|
||||||
const page = await browserContext.newPage();
|
const page = await browserContext.newPage();
|
||||||
await page.goto(server.EMPTY_PAGE);
|
await page.goto(server.EMPTY_PAGE);
|
||||||
@ -89,15 +126,15 @@ test('should be able to reconnect to a browser', async ({ browserType, startRemo
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should be able to connect two browsers at the same time', async ({ browserType, startRemoteServer }) => {
|
test('should be able to connect two browsers at the same time', async ({ connect, startRemoteServer }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
|
|
||||||
const browser1 = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser1 = await connect(remoteServer.wsEndpoint());
|
||||||
expect(browser1.contexts().length).toBe(0);
|
expect(browser1.contexts().length).toBe(0);
|
||||||
await browser1.newContext();
|
await browser1.newContext();
|
||||||
expect(browser1.contexts().length).toBe(1);
|
expect(browser1.contexts().length).toBe(1);
|
||||||
|
|
||||||
const browser2 = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser2 = await connect(remoteServer.wsEndpoint());
|
||||||
expect(browser2.contexts().length).toBe(0);
|
expect(browser2.contexts().length).toBe(0);
|
||||||
await browser2.newContext();
|
await browser2.newContext();
|
||||||
expect(browser2.contexts().length).toBe(1);
|
expect(browser2.contexts().length).toBe(1);
|
||||||
@ -111,27 +148,24 @@ test('should be able to connect two browsers at the same time', async ({ browser
|
|||||||
await browser2.close();
|
await browser2.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should timeout in socket while connecting', async ({ browserType, startRemoteServer, server }) => {
|
test('should timeout in socket while connecting', async ({ connect, server }) => {
|
||||||
const e = await browserType.connect({
|
const e = await connect(`ws://localhost:${server.PORT}/ws-slow`, {
|
||||||
wsEndpoint: `ws://localhost:${server.PORT}/ws-slow`,
|
|
||||||
timeout: 1000,
|
timeout: 1000,
|
||||||
}).catch(e => e);
|
}).catch(e => e);
|
||||||
expect(e.message).toContain('browserType.connect: Timeout 1000ms exceeded');
|
expect(e.message).toContain('browserType.connect: Timeout 1000ms exceeded');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should timeout in connect while connecting', async ({ browserType, startRemoteServer, server }) => {
|
test('should timeout in connect while connecting', async ({ connect, server }) => {
|
||||||
const e = await browserType.connect({
|
const e = await connect(`ws://localhost:${server.PORT}/ws`, {
|
||||||
wsEndpoint: `ws://localhost:${server.PORT}/ws`,
|
|
||||||
timeout: 100,
|
timeout: 100,
|
||||||
}).catch(e => e);
|
}).catch(e => e);
|
||||||
expect(e.message).toContain('browserType.connect: Timeout 100ms exceeded');
|
expect(e.message).toContain('browserType.connect: Timeout 100ms exceeded');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should send extra headers with connect request', async ({ browserType, startRemoteServer, server }) => {
|
test('should send extra headers with connect request', async ({ connect, server }) => {
|
||||||
const [request] = await Promise.all([
|
const [request] = await Promise.all([
|
||||||
server.waitForWebSocketConnectionRequest(),
|
server.waitForWebSocketConnectionRequest(),
|
||||||
browserType.connect({
|
connect(`ws://localhost:${server.PORT}/ws`, {
|
||||||
wsEndpoint: `ws://localhost:${server.PORT}/ws`,
|
|
||||||
headers: {
|
headers: {
|
||||||
'User-Agent': 'Playwright',
|
'User-Agent': 'Playwright',
|
||||||
'foo': 'bar',
|
'foo': 'bar',
|
||||||
@ -143,11 +177,10 @@ test('should send extra headers with connect request', async ({ browserType, sta
|
|||||||
expect(request.headers['foo']).toBe('bar');
|
expect(request.headers['foo']).toBe('bar');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should send default User-Agent and X-Playwright-Browser headers with connect request', async ({ browserType, browserName, server }) => {
|
test('should send default User-Agent and X-Playwright-Browser headers with connect request', async ({ connect, browserName, server }) => {
|
||||||
const [request] = await Promise.all([
|
const [request] = await Promise.all([
|
||||||
server.waitForWebSocketConnectionRequest(),
|
server.waitForWebSocketConnectionRequest(),
|
||||||
browserType.connect({
|
connect(`ws://localhost:${server.PORT}/ws`, {
|
||||||
wsEndpoint: `ws://localhost:${server.PORT}/ws`,
|
|
||||||
headers: {
|
headers: {
|
||||||
'foo': 'bar',
|
'foo': 'bar',
|
||||||
},
|
},
|
||||||
@ -159,23 +192,23 @@ test('should send default User-Agent and X-Playwright-Browser headers with conne
|
|||||||
expect(request.headers['foo']).toBe('bar');
|
expect(request.headers['foo']).toBe('bar');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should support slowmo option', async ({ browserType, startRemoteServer }) => {
|
test('should support slowmo option', async ({ connect, startRemoteServer }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
|
|
||||||
const browser1 = await browserType.connect(remoteServer.wsEndpoint(), { slowMo: 200 });
|
const browser1 = await connect(remoteServer.wsEndpoint(), { slowMo: 200 });
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
await browser1.newContext();
|
await browser1.newContext();
|
||||||
await browser1.close();
|
await browser1.close();
|
||||||
expect(Date.now() - start).toBeGreaterThan(199);
|
expect(Date.now() - start).toBeGreaterThan(199);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('disconnected event should be emitted when browser is closed or server is closed', async ({ browserType, startRemoteServer }) => {
|
test('disconnected event should be emitted when browser is closed or server is closed', async ({ connect, startRemoteServer }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
|
|
||||||
const browser1 = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser1 = await connect(remoteServer.wsEndpoint());
|
||||||
await browser1.newPage();
|
await browser1.newPage();
|
||||||
|
|
||||||
const browser2 = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser2 = await connect(remoteServer.wsEndpoint());
|
||||||
await browser2.newPage();
|
await browser2.newPage();
|
||||||
|
|
||||||
let disconnected1 = 0;
|
let disconnected1 = 0;
|
||||||
@ -198,9 +231,9 @@ test('disconnected event should be emitted when browser is closed or server is c
|
|||||||
expect(disconnected2).toBe(1);
|
expect(disconnected2).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('disconnected event should have browser as argument', async ({ browserType, startRemoteServer }) => {
|
test('disconnected event should have browser as argument', async ({ connect, startRemoteServer }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const [disconnected] = await Promise.all([
|
const [disconnected] = await Promise.all([
|
||||||
new Promise(f => browser.on('disconnected', f)),
|
new Promise(f => browser.on('disconnected', f)),
|
||||||
browser.close(),
|
browser.close(),
|
||||||
@ -208,26 +241,26 @@ test('disconnected event should have browser as argument', async ({ browserType,
|
|||||||
expect(disconnected).toBe(browser);
|
expect(disconnected).toBe(browser);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle exceptions during connect', async ({ browserType, startRemoteServer, mode }) => {
|
test('should handle exceptions during connect', async ({ connect, startRemoteServer, mode }) => {
|
||||||
test.skip(mode !== 'default');
|
test.skip(mode !== 'default');
|
||||||
|
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const __testHookBeforeCreateBrowser = () => { throw new Error('Dummy'); };
|
const __testHookBeforeCreateBrowser = () => { throw new Error('Dummy'); };
|
||||||
const error = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint(), __testHookBeforeCreateBrowser } as any).catch(e => e);
|
const error = await connect(remoteServer.wsEndpoint(), { __testHookBeforeCreateBrowser } as any).catch(e => e);
|
||||||
expect(error.message).toContain('Dummy');
|
expect(error.message).toContain('Dummy');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should set the browser connected state', async ({ browserType, startRemoteServer }) => {
|
test('should set the browser connected state', async ({ connect, startRemoteServer }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const remote = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const remote = await connect(remoteServer.wsEndpoint());
|
||||||
expect(remote.isConnected()).toBe(true);
|
expect(remote.isConnected()).toBe(true);
|
||||||
await remote.close();
|
await remote.close();
|
||||||
expect(remote.isConnected()).toBe(false);
|
expect(remote.isConnected()).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should throw when used after isConnected returns false', async ({ browserType, startRemoteServer }) => {
|
test('should throw when used after isConnected returns false', async ({ connect, startRemoteServer }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
remoteServer.close(),
|
remoteServer.close(),
|
||||||
@ -238,9 +271,9 @@ test('should throw when used after isConnected returns false', async ({ browserT
|
|||||||
expect(error.message).toContain('has been closed');
|
expect(error.message).toContain('has been closed');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should throw when calling waitForNavigation after disconnect', async ({ browserType, startRemoteServer }) => {
|
test('should throw when calling waitForNavigation after disconnect', async ({ connect, startRemoteServer }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
remoteServer.close(),
|
remoteServer.close(),
|
||||||
@ -251,10 +284,10 @@ test('should throw when calling waitForNavigation after disconnect', async ({ br
|
|||||||
expect(error.message).toContain('Navigation failed because page was closed');
|
expect(error.message).toContain('Navigation failed because page was closed');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should reject navigation when browser closes', async ({ browserType, startRemoteServer, server }) => {
|
test('should reject navigation when browser closes', async ({ connect, startRemoteServer, server }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
server.setRoute('/one-style.css', () => {});
|
server.setRoute('/one-style.css', () => {});
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
const navigationPromise = page.goto(server.PREFIX + '/one-style.html', { timeout: 60000 }).catch(e => e);
|
const navigationPromise = page.goto(server.PREFIX + '/one-style.html', { timeout: 60000 }).catch(e => e);
|
||||||
await server.waitForRequest('/one-style.css');
|
await server.waitForRequest('/one-style.css');
|
||||||
@ -263,10 +296,10 @@ test('should reject navigation when browser closes', async ({ browserType, start
|
|||||||
expect(error.message).toContain('has been closed');
|
expect(error.message).toContain('has been closed');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should reject waitForSelector when browser closes', async ({ browserType, startRemoteServer, server }) => {
|
test('should reject waitForSelector when browser closes', async ({ connect, startRemoteServer, server }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
server.setRoute('/empty.html', () => {});
|
server.setRoute('/empty.html', () => {});
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
const watchdog = page.waitForSelector('div', { state: 'attached', timeout: 60000 }).catch(e => e);
|
const watchdog = page.waitForSelector('div', { state: 'attached', timeout: 60000 }).catch(e => e);
|
||||||
|
|
||||||
@ -278,9 +311,9 @@ test('should reject waitForSelector when browser closes', async ({ browserType,
|
|||||||
expect(error.message).toContain('has been closed');
|
expect(error.message).toContain('has been closed');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should emit close events on pages and contexts', async ({ browserType, startRemoteServer }) => {
|
test('should emit close events on pages and contexts', async ({ connect, startRemoteServer }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const context = await browser.newContext();
|
const context = await browser.newContext();
|
||||||
const page = await context.newPage();
|
const page = await context.newPage();
|
||||||
let pageClosed = false;
|
let pageClosed = false;
|
||||||
@ -292,9 +325,9 @@ test('should emit close events on pages and contexts', async ({ browserType, sta
|
|||||||
expect(pageClosed).toBeTruthy();
|
expect(pageClosed).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should terminate network waiters', async ({ browserType, startRemoteServer, server }) => {
|
test('should terminate network waiters', async ({ connect, startRemoteServer, server }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const newPage = await browser.newPage();
|
const newPage = await browser.newPage();
|
||||||
const results = await Promise.all([
|
const results = await Promise.all([
|
||||||
newPage.waitForRequest(server.EMPTY_PAGE).catch(e => e),
|
newPage.waitForRequest(server.EMPTY_PAGE).catch(e => e),
|
||||||
@ -308,8 +341,12 @@ test('should terminate network waiters', async ({ browserType, startRemoteServer
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should respect selectors', async ({ playwright, browserType, startRemoteServer }) => {
|
test('should respect selectors', async ({ playwright, connect, startRemoteServer }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
|
|
||||||
|
const mycss1 = 'mycss1-' + kind;
|
||||||
|
const mycss2 = 'mycss2-' + kind;
|
||||||
|
const mycss3 = 'mycss3-' + kind;
|
||||||
|
|
||||||
const mycss = () => ({
|
const mycss = () => ({
|
||||||
query(root, selector) {
|
query(root, selector) {
|
||||||
@ -320,38 +357,38 @@ test('should respect selectors', async ({ playwright, browserType, startRemoteSe
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Register one engine before connecting.
|
// Register one engine before connecting.
|
||||||
await playwright.selectors.register('mycss1', mycss);
|
await playwright.selectors.register(mycss1, mycss);
|
||||||
|
|
||||||
const browser1 = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser1 = await connect(remoteServer.wsEndpoint());
|
||||||
const context1 = await browser1.newContext();
|
const context1 = await browser1.newContext();
|
||||||
|
|
||||||
// Register another engine after creating context.
|
// Register another engine after creating context.
|
||||||
await playwright.selectors.register('mycss2', mycss);
|
await playwright.selectors.register(mycss2, mycss);
|
||||||
|
|
||||||
const page1 = await context1.newPage();
|
const page1 = await context1.newPage();
|
||||||
await page1.setContent(`<div>hello</div>`);
|
await page1.setContent(`<div>hello</div>`);
|
||||||
expect(await page1.innerHTML('css=div')).toBe('hello');
|
expect(await page1.innerHTML('css=div')).toBe('hello');
|
||||||
expect(await page1.innerHTML('mycss1=div')).toBe('hello');
|
expect(await page1.innerHTML(`${mycss1}=div`)).toBe('hello');
|
||||||
expect(await page1.innerHTML('mycss2=div')).toBe('hello');
|
expect(await page1.innerHTML(`${mycss2}=div`)).toBe('hello');
|
||||||
|
|
||||||
const browser2 = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser2 = await connect(remoteServer.wsEndpoint());
|
||||||
|
|
||||||
// Register third engine after second connect.
|
// Register third engine after second connect.
|
||||||
await playwright.selectors.register('mycss3', mycss);
|
await playwright.selectors.register(mycss3, mycss);
|
||||||
|
|
||||||
const page2 = await browser2.newPage();
|
const page2 = await browser2.newPage();
|
||||||
await page2.setContent(`<div>hello</div>`);
|
await page2.setContent(`<div>hello</div>`);
|
||||||
expect(await page2.innerHTML('css=div')).toBe('hello');
|
expect(await page2.innerHTML('css=div')).toBe('hello');
|
||||||
expect(await page2.innerHTML('mycss1=div')).toBe('hello');
|
expect(await page2.innerHTML(`${mycss1}=div`)).toBe('hello');
|
||||||
expect(await page2.innerHTML('mycss2=div')).toBe('hello');
|
expect(await page2.innerHTML(`${mycss2}=div`)).toBe('hello');
|
||||||
expect(await page2.innerHTML('mycss3=div')).toBe('hello');
|
expect(await page2.innerHTML(`${mycss3}=div`)).toBe('hello');
|
||||||
|
|
||||||
await browser1.close();
|
await browser1.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should not throw on close after disconnect', async ({ browserType, startRemoteServer }) => {
|
test('should not throw on close after disconnect', async ({ connect, startRemoteServer }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
await browser.newPage();
|
await browser.newPage();
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
new Promise(f => browser.on('disconnected', f)),
|
new Promise(f => browser.on('disconnected', f)),
|
||||||
@ -360,9 +397,9 @@ test('should not throw on close after disconnect', async ({ browserType, startRe
|
|||||||
await browser.close();
|
await browser.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should not throw on context.close after disconnect', async ({ browserType, startRemoteServer }) => {
|
test('should not throw on context.close after disconnect', async ({ connect, startRemoteServer }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const context = await browser.newContext();
|
const context = await browser.newContext();
|
||||||
await context.newPage();
|
await context.newPage();
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
@ -372,9 +409,9 @@ test('should not throw on context.close after disconnect', async ({ browserType,
|
|||||||
await context.close();
|
await context.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should not throw on page.close after disconnect', async ({ browserType, startRemoteServer }) => {
|
test('should not throw on page.close after disconnect', async ({ connect, startRemoteServer }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
new Promise(f => browser.on('disconnected', f)),
|
new Promise(f => browser.on('disconnected', f)),
|
||||||
@ -383,9 +420,9 @@ test('should not throw on page.close after disconnect', async ({ browserType, st
|
|||||||
await page.close();
|
await page.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should saveAs videos from remote browser', async ({ browserType, startRemoteServer }, testInfo) => {
|
test('should saveAs videos from remote browser', async ({ connect, startRemoteServer }, testInfo) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const videosPath = testInfo.outputPath();
|
const videosPath = testInfo.outputPath();
|
||||||
const context = await browser.newContext({
|
const context = await browser.newContext({
|
||||||
recordVideo: { dir: videosPath, size: { width: 320, height: 240 } },
|
recordVideo: { dir: videosPath, size: { width: 320, height: 240 } },
|
||||||
@ -402,8 +439,8 @@ test('should saveAs videos from remote browser', async ({ browserType, startRemo
|
|||||||
expect(error.message).toContain('Path is not available when connecting remotely. Use saveAs() to save a local copy.');
|
expect(error.message).toContain('Path is not available when connecting remotely. Use saveAs() to save a local copy.');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should be able to connect 20 times to a single server without warnings', async ({ browserType, startRemoteServer }) => {
|
test('should be able to connect 20 times to a single server without warnings', async ({ connect, startRemoteServer }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
|
|
||||||
let warning = null;
|
let warning = null;
|
||||||
const warningHandler = w => warning = w;
|
const warningHandler = w => warning = w;
|
||||||
@ -411,22 +448,22 @@ test('should be able to connect 20 times to a single server without warnings', a
|
|||||||
|
|
||||||
const browsers = [];
|
const browsers = [];
|
||||||
for (let i = 0; i < 20; i++)
|
for (let i = 0; i < 20; i++)
|
||||||
browsers.push(await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() }));
|
browsers.push(await connect(remoteServer.wsEndpoint()));
|
||||||
await Promise.all([browsers.map(browser => browser.close())]);
|
await Promise.all([browsers.map(browser => browser.close())]);
|
||||||
|
|
||||||
process.off('warning', warningHandler);
|
process.off('warning', warningHandler);
|
||||||
expect(warning).toBe(null);
|
expect(warning).toBe(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should save download', async ({ server, browserType, startRemoteServer }, testInfo) => {
|
test('should save download', async ({ server, connect, startRemoteServer }, testInfo) => {
|
||||||
server.setRoute('/download', (req, res) => {
|
server.setRoute('/download', (req, res) => {
|
||||||
res.setHeader('Content-Type', 'application/octet-stream');
|
res.setHeader('Content-Type', 'application/octet-stream');
|
||||||
res.setHeader('Content-Disposition', 'attachment');
|
res.setHeader('Content-Disposition', 'attachment');
|
||||||
res.end(`Hello world`);
|
res.end(`Hello world`);
|
||||||
});
|
});
|
||||||
|
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
|
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
|
||||||
const [download] = await Promise.all([
|
const [download] = await Promise.all([
|
||||||
@ -442,15 +479,15 @@ test('should save download', async ({ server, browserType, startRemoteServer },
|
|||||||
await browser.close();
|
await browser.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should error when saving download after deletion', async ({ server, browserType, startRemoteServer }, testInfo) => {
|
test('should error when saving download after deletion', async ({ server, connect, startRemoteServer }, testInfo) => {
|
||||||
server.setRoute('/download', (req, res) => {
|
server.setRoute('/download', (req, res) => {
|
||||||
res.setHeader('Content-Type', 'application/octet-stream');
|
res.setHeader('Content-Type', 'application/octet-stream');
|
||||||
res.setHeader('Content-Disposition', 'attachment');
|
res.setHeader('Content-Disposition', 'attachment');
|
||||||
res.end(`Hello world`);
|
res.end(`Hello world`);
|
||||||
});
|
});
|
||||||
|
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
|
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
|
||||||
const [download] = await Promise.all([
|
const [download] = await Promise.all([
|
||||||
@ -464,17 +501,10 @@ test('should error when saving download after deletion', async ({ server, browse
|
|||||||
await browser.close();
|
await browser.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should work with cluster', async ({ browserType, startRemoteServer }) => {
|
test('should properly disconnect when connection closes from the client side', async ({ connect, startRemoteServer, server }) => {
|
||||||
const remoteServer = await startRemoteServer({ inCluster: true });
|
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
|
||||||
const page = await browser.newPage();
|
|
||||||
expect(await page.evaluate('1 + 2')).toBe(3);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should properly disconnect when connection closes from the client side', async ({ browserType, startRemoteServer, server }) => {
|
|
||||||
server.setRoute('/one-style.css', () => {});
|
server.setRoute('/one-style.css', () => {});
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
const navigationPromise = page.goto(server.PREFIX + '/one-style.html', { timeout: 60000 }).catch(e => e);
|
const navigationPromise = page.goto(server.PREFIX + '/one-style.html', { timeout: 60000 }).catch(e => e);
|
||||||
const waitForNavigationPromise = page.waitForNavigation().catch(e => e);
|
const waitForNavigationPromise = page.waitForNavigation().catch(e => e);
|
||||||
@ -494,39 +524,22 @@ test('should properly disconnect when connection closes from the client side', a
|
|||||||
expect((await page.waitForNavigation().catch(e => e)).message).toContain('Navigation failed because page was closed');
|
expect((await page.waitForNavigation().catch(e => e)).message).toContain('Navigation failed because page was closed');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should properly disconnect when connection closes from the server side', async ({ browserType, startRemoteServer, server, platform }) => {
|
test('should be able to connect when the wsEndpont is passed as an option', async ({ browserType, startRemoteServer }) => {
|
||||||
test.skip(platform === 'win32', 'Cannot send signals');
|
const remoteServer = await startRemoteServer(kind);
|
||||||
|
const browser = await browserType.connect({
|
||||||
server.setRoute('/one-style.css', () => {});
|
wsEndpoint: remoteServer.wsEndpoint(),
|
||||||
const remoteServer = await startRemoteServer({ disconnectOnSIGHUP: true });
|
headers: {
|
||||||
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
|
'x-playwright-launch-options': JSON.stringify((browserType as any)._defaultLaunchOptions || {}),
|
||||||
const page = await browser.newPage();
|
},
|
||||||
const navigationPromise = page.goto(server.PREFIX + '/one-style.html', { timeout: 60000 }).catch(e => e);
|
|
||||||
const waitForNavigationPromise = page.waitForNavigation().catch(e => e);
|
|
||||||
|
|
||||||
const disconnectedPromise = new Promise(f => browser.once('disconnected', f));
|
|
||||||
// This closes the websocket server.
|
|
||||||
process.kill(remoteServer.child().pid, 'SIGHUP');
|
|
||||||
await disconnectedPromise;
|
|
||||||
expect(browser.isConnected()).toBe(false);
|
|
||||||
|
|
||||||
expect((await navigationPromise).message).toContain('has been closed');
|
|
||||||
expect((await waitForNavigationPromise).message).toContain('Navigation failed because page was closed');
|
|
||||||
expect((await page.goto(server.EMPTY_PAGE).catch(e => e)).message).toContain('has been closed');
|
|
||||||
expect((await page.waitForNavigation().catch(e => e)).message).toContain('Navigation failed because page was closed');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should be able to connect when the wsEndpont is passed as the first argument', async ({ browserType, startRemoteServer }) => {
|
|
||||||
const remoteServer = await startRemoteServer();
|
|
||||||
const browser = await browserType.connect(remoteServer.wsEndpoint());
|
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
expect(await page.evaluate('1 + 2')).toBe(3);
|
expect(await page.evaluate('1 + 2')).toBe(3);
|
||||||
await browser.close();
|
await browser.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should save har', async ({ browserType, startRemoteServer, server }, testInfo) => {
|
test('should save har', async ({ connect, startRemoteServer, server }, testInfo) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect(remoteServer.wsEndpoint());
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const harPath = testInfo.outputPath('test.har');
|
const harPath = testInfo.outputPath('test.har');
|
||||||
const context = await browser.newContext({
|
const context = await browser.newContext({
|
||||||
recordHar: {
|
recordHar: {
|
||||||
@ -545,10 +558,10 @@ test('should save har', async ({ browserType, startRemoteServer, server }, testI
|
|||||||
expect(entry.request.url).toBe(server.EMPTY_PAGE);
|
expect(entry.request.url).toBe(server.EMPTY_PAGE);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should record trace with sources', async ({ browserType, startRemoteServer, server, trace }, testInfo) => {
|
test('should record trace with sources', async ({ connect, startRemoteServer, server, trace }, testInfo) => {
|
||||||
test.skip(trace === 'on');
|
test.skip(trace === 'on');
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect(remoteServer.wsEndpoint());
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const context = await browser.newContext();
|
const context = await browser.newContext();
|
||||||
const page = await context.newPage();
|
const page = await context.newPage();
|
||||||
|
|
||||||
@ -569,9 +582,9 @@ test('should record trace with sources', async ({ browserType, startRemoteServer
|
|||||||
expect(sourceFile).toEqual(thisFile);
|
expect(sourceFile).toEqual(thisFile);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should fulfill with global fetch result', async ({ browserType, startRemoteServer, playwright, server }) => {
|
test('should fulfill with global fetch result', async ({ connect, startRemoteServer, playwright, server }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect(remoteServer.wsEndpoint());
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const context = await browser.newContext();
|
const context = await browser.newContext();
|
||||||
const page = await context.newPage();
|
const page = await context.newPage();
|
||||||
|
|
||||||
@ -585,11 +598,11 @@ test('should fulfill with global fetch result', async ({ browserType, startRemot
|
|||||||
expect(await response.json()).toEqual({ 'foo': 'bar' });
|
expect(await response.json()).toEqual({ 'foo': 'bar' });
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should upload large file', async ({ browserType, startRemoteServer, server, browserName, isMac }, testInfo) => {
|
test('should upload large file', async ({ connect, startRemoteServer, server, browserName, isMac }, testInfo) => {
|
||||||
test.skip(browserName === 'webkit' && isMac && parseInt(os.release(), 10) < 20, 'WebKit for macOS 10.15 is frozen and does not have corresponding protocol features.');
|
test.skip(browserName === 'webkit' && isMac && parseInt(os.release(), 10) < 20, 'WebKit for macOS 10.15 is frozen and does not have corresponding protocol features.');
|
||||||
test.slow();
|
test.slow();
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
const browser = await browserType.connect(remoteServer.wsEndpoint());
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
const context = await browser.newContext();
|
const context = await browser.newContext();
|
||||||
const page = await context.newPage();
|
const page = await context.newPage();
|
||||||
|
|
||||||
@ -637,8 +650,8 @@ test('should upload large file', async ({ browserType, startRemoteServer, server
|
|||||||
await Promise.all([uploadFile, file1.filepath].map(fs.promises.unlink));
|
await Promise.all([uploadFile, file1.filepath].map(fs.promises.unlink));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should connect when launching', async ({ browserType, startRemoteServer, httpsServer, mode }) => {
|
test('should connect when launching', async ({ browserType, startRemoteServer }) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
(browserType as any)._defaultConnectOptions = {
|
(browserType as any)._defaultConnectOptions = {
|
||||||
wsEndpoint: remoteServer.wsEndpoint()
|
wsEndpoint: remoteServer.wsEndpoint()
|
||||||
};
|
};
|
||||||
@ -653,12 +666,172 @@ test('should connect when launching', async ({ browserType, startRemoteServer, h
|
|||||||
(browserType as any)._defaultConnectOptions = undefined;
|
(browserType as any)._defaultConnectOptions = undefined;
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should connect over http', async ({ browserType, startRemoteServer, mode }) => {
|
test('should connect over http', async ({ connect, startRemoteServer, mode }) => {
|
||||||
test.skip(mode !== 'default');
|
test.skip(mode !== 'default');
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer(kind);
|
||||||
|
|
||||||
const url = new URL(remoteServer.wsEndpoint());
|
const url = new URL(remoteServer.wsEndpoint());
|
||||||
const browser = await browserType.connect(`http://localhost:${url.port}`);
|
const browser = await connect(`http://localhost:${url.port}`);
|
||||||
expect(browser.version()).toBeTruthy();
|
expect(browser.version()).toBeTruthy();
|
||||||
await browser.close();
|
await browser.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test.describe('socks proxy', () => {
|
||||||
|
test.fixme(({ platform, browserName }) => browserName === 'webkit' && platform === 'win32');
|
||||||
|
test.skip(({ mode }) => mode !== 'default');
|
||||||
|
test.skip(kind === 'launchServer', 'This feature is not yet supported in launchServer');
|
||||||
|
|
||||||
|
test('should forward non-forwarded requests', async ({ server, startRemoteServer, connect }) => {
|
||||||
|
let reachedOriginalTarget = false;
|
||||||
|
server.setRoute('/foo.html', async (req, res) => {
|
||||||
|
reachedOriginalTarget = true;
|
||||||
|
res.end('<html><body>original-target</body></html>');
|
||||||
|
});
|
||||||
|
const remoteServer = await startRemoteServer(kind);
|
||||||
|
const browser = await connect(remoteServer.wsEndpoint(), {
|
||||||
|
headers: { 'x-playwright-proxy': '*' }
|
||||||
|
});
|
||||||
|
const page = await browser.newPage();
|
||||||
|
await page.goto(server.PREFIX + '/foo.html');
|
||||||
|
expect(await page.content()).toContain('original-target');
|
||||||
|
expect(reachedOriginalTarget).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should proxy localhost requests @smoke', async ({ startRemoteServer, server, browserName, connect, platform, dummyServerPort }, testInfo) => {
|
||||||
|
test.skip(browserName === 'webkit' && platform === 'darwin', 'no localhost proxying');
|
||||||
|
|
||||||
|
let reachedOriginalTarget = false;
|
||||||
|
server.setRoute('/foo.html', async (req, res) => {
|
||||||
|
reachedOriginalTarget = true;
|
||||||
|
res.end('<html><body></body></html>');
|
||||||
|
});
|
||||||
|
const examplePort = 20_000 + testInfo.workerIndex * 3;
|
||||||
|
const remoteServer = await startRemoteServer(kind);
|
||||||
|
const browser = await connect(remoteServer.wsEndpoint(), {
|
||||||
|
headers: { 'x-playwright-proxy': '*' }
|
||||||
|
}, dummyServerPort);
|
||||||
|
const page = await browser.newPage();
|
||||||
|
await page.goto(`http://127.0.0.1:${examplePort}/foo.html`);
|
||||||
|
expect(await page.content()).toContain('from-dummy-server');
|
||||||
|
expect(reachedOriginalTarget).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should proxy localhost requests from fetch api', async ({ startRemoteServer, server, browserName, connect, channel, platform, dummyServerPort }, workerInfo) => {
|
||||||
|
test.skip(browserName === 'webkit' && platform === 'darwin', 'no localhost proxying');
|
||||||
|
|
||||||
|
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 remoteServer = await startRemoteServer(kind);
|
||||||
|
const browser = await connect(remoteServer.wsEndpoint(), {
|
||||||
|
headers: { 'x-playwright-proxy': '*' }
|
||||||
|
}, dummyServerPort);
|
||||||
|
const page = await browser.newPage();
|
||||||
|
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-dummy-server');
|
||||||
|
expect(reachedOriginalTarget).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should proxy local.playwright requests', async ({ connect, server, dummyServerPort, startRemoteServer }, workerInfo) => {
|
||||||
|
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 remoteServer = await startRemoteServer(kind);
|
||||||
|
const browser = await connect(remoteServer.wsEndpoint(), {
|
||||||
|
headers: { 'x-playwright-proxy': '*' }
|
||||||
|
}, dummyServerPort);
|
||||||
|
const page = await browser.newPage();
|
||||||
|
await page.goto(`http://local.playwright:${examplePort}/foo.html`);
|
||||||
|
expect(await page.content()).toContain('from-dummy-server');
|
||||||
|
expect(reachedOriginalTarget).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should lead to the error page for forwarded requests when the connection is refused', async ({ connect, startRemoteServer, browserName }, workerInfo) => {
|
||||||
|
const examplePort = 20_000 + workerInfo.workerIndex * 3;
|
||||||
|
const remoteServer = await startRemoteServer(kind);
|
||||||
|
const browser = await connect(remoteServer.wsEndpoint(), {
|
||||||
|
headers: { 'x-playwright-proxy': '*' }
|
||||||
|
});
|
||||||
|
const page = await browser.newPage();
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should proxy based on the pattern', async ({ connect, startRemoteServer, server, browserName, platform, dummyServerPort }, workerInfo) => {
|
||||||
|
test.skip(browserName === 'webkit' && platform === 'darwin', 'no localhost proxying');
|
||||||
|
|
||||||
|
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 remoteServer = await startRemoteServer(kind);
|
||||||
|
const browser = await connect(remoteServer.wsEndpoint(), {
|
||||||
|
headers: { 'x-playwright-proxy': 'localhost' }
|
||||||
|
}, dummyServerPort);
|
||||||
|
const page = await browser.newPage();
|
||||||
|
|
||||||
|
// localhost should be proxied.
|
||||||
|
await page.goto(`http://localhost:${examplePort}/foo.html`);
|
||||||
|
expect(await page.content()).toContain('from-dummy-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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test.describe('launchServer only', () => {
|
||||||
|
test('should work with cluster', async ({ connect, startRemoteServer }) => {
|
||||||
|
const remoteServer = await startRemoteServer('launchServer', { inCluster: true });
|
||||||
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
|
const page = await browser.newPage();
|
||||||
|
expect(await page.evaluate('1 + 2')).toBe(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should properly disconnect when connection closes from the server side', async ({ connect, startRemoteServer, server, platform }) => {
|
||||||
|
test.skip(platform === 'win32', 'Cannot send signals');
|
||||||
|
|
||||||
|
server.setRoute('/one-style.css', () => {});
|
||||||
|
const remoteServer = await startRemoteServer('launchServer', { disconnectOnSIGHUP: true });
|
||||||
|
const browser = await connect(remoteServer.wsEndpoint());
|
||||||
|
const page = await browser.newPage();
|
||||||
|
const navigationPromise = page.goto(server.PREFIX + '/one-style.html', { timeout: 60000 }).catch(e => e);
|
||||||
|
const waitForNavigationPromise = page.waitForNavigation().catch(e => e);
|
||||||
|
|
||||||
|
const disconnectedPromise = new Promise(f => browser.once('disconnected', f));
|
||||||
|
// This closes the websocket server.
|
||||||
|
process.kill(remoteServer.child().pid, 'SIGHUP');
|
||||||
|
await disconnectedPromise;
|
||||||
|
expect(browser.isConnected()).toBe(false);
|
||||||
|
|
||||||
|
expect((await navigationPromise).message).toContain('has been closed');
|
||||||
|
expect((await waitForNavigationPromise).message).toContain('Navigation failed because page was closed');
|
||||||
|
expect((await page.goto(server.EMPTY_PAGE).catch(e => e)).message).toContain('has been closed');
|
||||||
|
expect((await page.waitForNavigation().catch(e => e)).message).toContain('Navigation failed because page was closed');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
@ -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.slow();
|
||||||
|
|
||||||
test('should close the browser when the node process closes', async ({ startRemoteServer, isWindows, server }) => {
|
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 {
|
try {
|
||||||
if (isWindows)
|
if (isWindows)
|
||||||
execSync(`taskkill /pid ${remoteServer.child().pid} /T /F`, { stdio: 'ignore' });
|
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) => {
|
test('should remove temp dir on process.exit', async ({ startRemoteServer, server }, testInfo) => {
|
||||||
const file = testInfo.outputPath('exit.file');
|
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 tempDir = await remoteServer.out('tempDir');
|
||||||
const before = fs.existsSync(tempDir);
|
const before = fs.existsSync(tempDir);
|
||||||
fs.writeFileSync(file, 'data', 'utf-8');
|
fs.writeFileSync(file, 'data', 'utf-8');
|
||||||
@ -59,7 +59,7 @@ test.describe('signals', () => {
|
|||||||
test('should report browser close signal', async ({ startRemoteServer, server, headless }) => {
|
test('should report browser close signal', async ({ startRemoteServer, server, headless }) => {
|
||||||
test.skip(!headless, 'Wrong exit code in headed');
|
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');
|
const pid = await remoteServer.out('pid');
|
||||||
process.kill(-pid, 'SIGTERM');
|
process.kill(-pid, 'SIGTERM');
|
||||||
expect(await remoteServer.out('exitCode')).toBe('null');
|
expect(await remoteServer.out('exitCode')).toBe('null');
|
||||||
@ -69,7 +69,7 @@ test.describe('signals', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should report browser close signal 2', async ({ startRemoteServer, server }) => {
|
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');
|
const pid = await remoteServer.out('pid');
|
||||||
process.kill(-pid, 'SIGKILL');
|
process.kill(-pid, 'SIGKILL');
|
||||||
expect(await remoteServer.out('exitCode')).toBe('null');
|
expect(await remoteServer.out('exitCode')).toBe('null');
|
||||||
@ -79,7 +79,7 @@ test.describe('signals', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should close the browser on SIGINT', async ({ startRemoteServer, server }) => {
|
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');
|
process.kill(remoteServer.child().pid, 'SIGINT');
|
||||||
expect(await remoteServer.out('exitCode')).toBe('0');
|
expect(await remoteServer.out('exitCode')).toBe('0');
|
||||||
expect(await remoteServer.out('signal')).toBe('null');
|
expect(await remoteServer.out('signal')).toBe('null');
|
||||||
@ -87,7 +87,7 @@ test.describe('signals', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should close the browser on SIGTERM', async ({ startRemoteServer, server }) => {
|
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');
|
process.kill(remoteServer.child().pid, 'SIGTERM');
|
||||||
expect(await remoteServer.out('exitCode')).toBe('0');
|
expect(await remoteServer.out('exitCode')).toBe('0');
|
||||||
expect(await remoteServer.out('signal')).toBe('null');
|
expect(await remoteServer.out('signal')).toBe('null');
|
||||||
@ -95,7 +95,7 @@ test.describe('signals', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should close the browser on SIGHUP', async ({ startRemoteServer, server }) => {
|
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');
|
process.kill(remoteServer.child().pid, 'SIGHUP');
|
||||||
expect(await remoteServer.out('exitCode')).toBe('0');
|
expect(await remoteServer.out('exitCode')).toBe('0');
|
||||||
expect(await remoteServer.out('signal')).toBe('null');
|
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 }) => {
|
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 tempDir = await remoteServer.out('tempDir');
|
||||||
const before = fs.existsSync(tempDir);
|
const before = fs.existsSync(tempDir);
|
||||||
process.kill(remoteServer.child().pid, 'SIGINT');
|
process.kill(remoteServer.child().pid, 'SIGINT');
|
||||||
@ -118,7 +118,7 @@ test.describe('signals', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should kill the browser on SIGINT + SIGTERM', async ({ startRemoteServer, server }) => {
|
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');
|
process.kill(remoteServer.child().pid, 'SIGINT');
|
||||||
await remoteServer.out('stalled');
|
await remoteServer.out('stalled');
|
||||||
process.kill(remoteServer.child().pid, 'SIGTERM');
|
process.kill(remoteServer.child().pid, 'SIGTERM');
|
||||||
@ -128,7 +128,7 @@ test.describe('signals', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should kill the browser on SIGTERM + SIGINT', async ({ startRemoteServer, server }) => {
|
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');
|
process.kill(remoteServer.child().pid, 'SIGTERM');
|
||||||
await remoteServer.out('stalled');
|
await remoteServer.out('stalled');
|
||||||
process.kill(remoteServer.child().pid, 'SIGINT');
|
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);
|
this._wsServer.once('connection', handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user