2021-04-29 21:11:32 +03:00
|
|
|
/**
|
|
|
|
* 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 { TestServer } from '../../utils/testserver';
|
2021-06-08 18:35:04 +03:00
|
|
|
import { Fixtures, _baseTest } from './test-runner';
|
2021-04-29 21:11:32 +03:00
|
|
|
import * as path from 'path';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import socks from 'socksv5';
|
|
|
|
import { installCoverageHooks } from './coverage';
|
|
|
|
import * as childProcess from 'child_process';
|
|
|
|
import { start } from '../../lib/outofprocess';
|
|
|
|
import { PlaywrightClient } from '../../lib/remote/playwrightClient';
|
2021-05-17 05:58:26 +03:00
|
|
|
import type { LaunchOptions } from '../../index';
|
2021-04-29 21:11:32 +03:00
|
|
|
|
|
|
|
export type BrowserName = 'chromium' | 'firefox' | 'webkit';
|
|
|
|
type Mode = 'default' | 'driver' | 'service';
|
2021-05-15 17:42:35 +03:00
|
|
|
type BaseOptions = {
|
2021-04-29 21:11:32 +03:00
|
|
|
mode: Mode;
|
2021-05-15 17:42:35 +03:00
|
|
|
browserName: BrowserName;
|
2021-05-17 05:58:26 +03:00
|
|
|
channel: LaunchOptions['channel'];
|
|
|
|
video: boolean | undefined;
|
|
|
|
headless: boolean | undefined;
|
2021-05-15 17:42:35 +03:00
|
|
|
};
|
2021-05-17 05:58:26 +03:00
|
|
|
type BaseFixtures = {
|
2021-04-29 21:11:32 +03:00
|
|
|
platform: 'win32' | 'darwin' | 'linux';
|
|
|
|
playwright: typeof import('../../index');
|
|
|
|
toImpl: (rpcObject: any) => any;
|
|
|
|
isWindows: boolean;
|
|
|
|
isMac: boolean;
|
|
|
|
isLinux: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DriverMode {
|
|
|
|
private _playwrightObject: any;
|
|
|
|
|
2021-05-17 05:58:26 +03:00
|
|
|
async setup(workerIndex: number) {
|
2021-04-29 21:11:32 +03:00
|
|
|
this._playwrightObject = await start();
|
|
|
|
return this._playwrightObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
async teardown() {
|
|
|
|
await this._playwrightObject.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ServiceMode {
|
|
|
|
private _playwrightObejct: any;
|
|
|
|
private _client: any;
|
|
|
|
private _serviceProcess: childProcess.ChildProcess;
|
|
|
|
|
2021-05-17 05:58:26 +03:00
|
|
|
async setup(workerIndex: number) {
|
|
|
|
const port = 10507 + workerIndex;
|
2021-04-29 21:11:32 +03:00
|
|
|
this._serviceProcess = childProcess.fork(path.join(__dirname, '..', '..', 'lib', 'cli', 'cli.js'), ['run-server', String(port)], {
|
|
|
|
stdio: 'pipe'
|
|
|
|
});
|
|
|
|
this._serviceProcess.stderr.pipe(process.stderr);
|
|
|
|
await new Promise<void>(f => {
|
|
|
|
this._serviceProcess.stdout.on('data', data => {
|
|
|
|
if (data.toString().includes('Listening on'))
|
|
|
|
f();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this._serviceProcess.on('exit', this._onExit);
|
2021-06-03 03:19:01 +03:00
|
|
|
this._client = await PlaywrightClient.connect({wsEndpoint: `ws://localhost:${port}/ws`});
|
2021-04-29 21:11:32 +03:00
|
|
|
this._playwrightObejct = this._client.playwright();
|
|
|
|
return this._playwrightObejct;
|
|
|
|
}
|
|
|
|
|
|
|
|
async teardown() {
|
|
|
|
await this._client.close();
|
|
|
|
this._serviceProcess.removeListener('exit', this._onExit);
|
|
|
|
const processExited = new Promise(f => this._serviceProcess.on('exit', f));
|
|
|
|
this._serviceProcess.kill();
|
|
|
|
await processExited;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _onExit(exitCode, signal) {
|
|
|
|
throw new Error(`Server closed with exitCode=${exitCode} signal=${signal}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DefaultMode {
|
2021-05-17 05:58:26 +03:00
|
|
|
async setup(workerIndex: number) {
|
2021-04-29 21:11:32 +03:00
|
|
|
return require('../../index');
|
|
|
|
}
|
|
|
|
|
|
|
|
async teardown() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 03:09:53 +03:00
|
|
|
const baseFixtures: Fixtures<{}, BaseOptions & BaseFixtures> = {
|
2021-05-17 05:58:26 +03:00
|
|
|
mode: [ 'default', { scope: 'worker' } ],
|
|
|
|
browserName: [ 'chromium' , { scope: 'worker' } ],
|
|
|
|
channel: [ undefined, { scope: 'worker' } ],
|
|
|
|
video: [ undefined, { scope: 'worker' } ],
|
|
|
|
headless: [ undefined, { scope: 'worker' } ],
|
|
|
|
platform: [ process.platform as 'win32' | 'darwin' | 'linux', { scope: 'worker' } ],
|
|
|
|
playwright: [ async ({ mode }, run, workerInfo) => {
|
|
|
|
const modeImpl = {
|
2021-04-29 21:11:32 +03:00
|
|
|
default: new DefaultMode(),
|
|
|
|
service: new ServiceMode(),
|
|
|
|
driver: new DriverMode(),
|
2021-05-17 05:58:26 +03:00
|
|
|
}[mode];
|
2021-04-29 21:11:32 +03:00
|
|
|
require('../../lib/utils/utils').setUnderTest();
|
2021-05-17 05:58:26 +03:00
|
|
|
const playwright = await modeImpl.setup(workerInfo.workerIndex);
|
|
|
|
await run(playwright);
|
|
|
|
await modeImpl.teardown();
|
|
|
|
}, { scope: 'worker' } ],
|
|
|
|
toImpl: [ async ({ playwright }, run) => run((playwright as any)._toImpl), { scope: 'worker' } ],
|
|
|
|
isWindows: [ process.platform === 'win32', { scope: 'worker' } ],
|
|
|
|
isMac: [ process.platform === 'darwin', { scope: 'worker' } ],
|
|
|
|
isLinux: [ process.platform === 'linux', { scope: 'worker' } ],
|
2021-04-29 21:11:32 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
type ServerOptions = {
|
|
|
|
loopback?: string;
|
|
|
|
};
|
2021-05-17 05:58:26 +03:00
|
|
|
type ServerFixtures = {
|
|
|
|
server: TestServer;
|
|
|
|
httpsServer: TestServer;
|
|
|
|
socksPort: number;
|
|
|
|
asset: (p: string) => string;
|
|
|
|
};
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-05-17 05:58:26 +03:00
|
|
|
type ServersInternal = ServerFixtures & { socksServer: any };
|
2021-06-07 03:09:53 +03:00
|
|
|
const serverFixtures: Fixtures<ServerFixtures, ServerOptions & { __servers: ServersInternal }> = {
|
2021-05-17 05:58:26 +03:00
|
|
|
loopback: [ undefined, { scope: 'worker' } ],
|
|
|
|
__servers: [ async ({ loopback }, run, workerInfo) => {
|
2021-04-29 21:11:32 +03:00
|
|
|
const assetsPath = path.join(__dirname, '..', 'assets');
|
|
|
|
const cachedPath = path.join(__dirname, '..', 'assets', 'cached');
|
|
|
|
|
|
|
|
const port = 8907 + workerInfo.workerIndex * 3;
|
2021-05-17 05:58:26 +03:00
|
|
|
const server = await TestServer.create(assetsPath, port, loopback);
|
|
|
|
server.enableHTTPCache(cachedPath);
|
2021-04-29 21:11:32 +03:00
|
|
|
|
|
|
|
const httpsPort = port + 1;
|
2021-05-17 05:58:26 +03:00
|
|
|
const httpsServer = await TestServer.createHTTPS(assetsPath, httpsPort, loopback);
|
|
|
|
httpsServer.enableHTTPCache(cachedPath);
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-05-17 05:58:26 +03:00
|
|
|
const socksServer = socks.createServer((info, accept, deny) => {
|
2021-04-29 21:11:32 +03:00
|
|
|
let socket;
|
|
|
|
if ((socket = accept(true))) {
|
|
|
|
// Catch and ignore ECONNRESET errors.
|
|
|
|
socket.on('error', () => {});
|
|
|
|
const body = '<html><title>Served by the SOCKS proxy</title></html>';
|
|
|
|
socket.end([
|
|
|
|
'HTTP/1.1 200 OK',
|
|
|
|
'Connection: close',
|
|
|
|
'Content-Type: text/html',
|
|
|
|
'Content-Length: ' + Buffer.byteLength(body),
|
|
|
|
'',
|
|
|
|
body
|
|
|
|
].join('\r\n'));
|
|
|
|
}
|
|
|
|
});
|
2021-05-17 05:58:26 +03:00
|
|
|
const socksPort = port + 2;
|
|
|
|
socksServer.listen(socksPort, 'localhost');
|
|
|
|
socksServer.useAuth(socks.auth.None());
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-05-17 05:58:26 +03:00
|
|
|
await run({
|
|
|
|
asset: (p: string) => path.join(__dirname, '..', 'assets', ...p.split('/')),
|
|
|
|
server,
|
|
|
|
httpsServer,
|
|
|
|
socksPort,
|
|
|
|
socksServer,
|
|
|
|
});
|
2021-04-30 23:26:13 +03:00
|
|
|
|
2021-04-29 21:11:32 +03:00
|
|
|
await Promise.all([
|
2021-05-17 05:58:26 +03:00
|
|
|
server.stop(),
|
|
|
|
httpsServer.stop(),
|
|
|
|
socksServer.close(),
|
2021-04-29 21:11:32 +03:00
|
|
|
]);
|
2021-05-17 05:58:26 +03:00
|
|
|
}, { scope: 'worker' } ],
|
|
|
|
|
|
|
|
server: async ({ __servers }, run) => {
|
|
|
|
__servers.server.reset();
|
|
|
|
await run(__servers.server);
|
|
|
|
},
|
|
|
|
|
|
|
|
httpsServer: async ({ __servers }, run) => {
|
|
|
|
__servers.httpsServer.reset();
|
|
|
|
await run(__servers.httpsServer);
|
|
|
|
},
|
|
|
|
|
|
|
|
socksPort: async ({ __servers }, run) => {
|
|
|
|
await run(__servers.socksPort);
|
|
|
|
},
|
|
|
|
|
|
|
|
asset: async ({ __servers }, run) => {
|
|
|
|
await run(__servers.asset);
|
|
|
|
},
|
|
|
|
};
|
2021-04-29 21:11:32 +03:00
|
|
|
|
|
|
|
type CoverageOptions = {
|
|
|
|
coverageName?: string;
|
|
|
|
};
|
|
|
|
|
2021-06-07 03:09:53 +03:00
|
|
|
const coverageFixtures: Fixtures<{}, CoverageOptions & { __collectCoverage: void }> = {
|
2021-05-17 05:58:26 +03:00
|
|
|
coverageName: [ undefined, { scope: 'worker' } ],
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-05-17 05:58:26 +03:00
|
|
|
__collectCoverage: [ async ({ coverageName }, run, workerInfo) => {
|
|
|
|
if (!coverageName) {
|
|
|
|
await run();
|
2021-04-29 21:11:32 +03:00
|
|
|
return;
|
2021-05-17 05:58:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const { coverage, uninstall } = installCoverageHooks(coverageName);
|
|
|
|
await run();
|
2021-04-29 21:11:32 +03:00
|
|
|
uninstall();
|
|
|
|
const coveragePath = path.join(__dirname, '..', 'coverage-report', workerInfo.workerIndex + '.json');
|
|
|
|
const coverageJSON = Array.from(coverage.keys()).filter(key => coverage.get(key));
|
|
|
|
await fs.promises.mkdir(path.dirname(coveragePath), { recursive: true });
|
|
|
|
await fs.promises.writeFile(coveragePath, JSON.stringify(coverageJSON, undefined, 2), 'utf8');
|
2021-05-17 05:58:26 +03:00
|
|
|
}, { scope: 'worker', auto: true } ],
|
|
|
|
};
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-05-08 01:25:55 +03:00
|
|
|
export type CommonOptions = BaseOptions & ServerOptions & CoverageOptions;
|
2021-05-17 05:58:26 +03:00
|
|
|
export type CommonWorkerFixtures = CommonOptions & BaseFixtures;
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-06-08 18:35:04 +03:00
|
|
|
export const baseTest = _baseTest.extend<{}, CoverageOptions>(coverageFixtures).extend<ServerFixtures>(serverFixtures).extend<{}, BaseOptions & BaseFixtures>(baseFixtures);
|