2020-08-11 02:48:34 +03:00
|
|
|
/**
|
|
|
|
* Copyright Microsoft Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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 fs from 'fs';
|
2020-08-13 08:41:40 +03:00
|
|
|
import path from 'path';
|
2020-08-14 03:32:27 +03:00
|
|
|
import os from 'os';
|
2020-08-11 02:48:34 +03:00
|
|
|
import childProcess from 'child_process';
|
|
|
|
import { LaunchOptions, BrowserType, Browser, BrowserContext, Page, BrowserServer } from '../index';
|
|
|
|
import { TestServer } from '../utils/testserver/';
|
|
|
|
import { Connection } from '../lib/rpc/client/connection';
|
|
|
|
import { Transport } from '../lib/rpc/transport';
|
|
|
|
import { setUnderTest } from '../lib/helper';
|
2020-08-12 23:47:44 +03:00
|
|
|
import { installCoverageHooks } from './runner/coverage';
|
|
|
|
import { valueFromEnv } from './runner/utils';
|
2020-08-14 03:32:27 +03:00
|
|
|
import { registerFixture, registerWorkerFixture} from './runner/fixtures';
|
|
|
|
|
|
|
|
import {mkdtempAsync, removeFolderAsync} from './utils';
|
2020-08-11 02:48:34 +03:00
|
|
|
|
|
|
|
setUnderTest(); // Note: we must call setUnderTest before requiring Playwright
|
|
|
|
|
|
|
|
const browserName = process.env.BROWSER || 'chromium';
|
2020-08-14 17:22:54 +03:00
|
|
|
const platform = os.platform();
|
2020-08-11 02:48:34 +03:00
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface WorkerState {
|
2020-08-14 03:32:27 +03:00
|
|
|
asset: (path: string) => string;
|
2020-08-11 02:48:34 +03:00
|
|
|
parallelIndex: number;
|
|
|
|
defaultBrowserOptions: LaunchOptions;
|
2020-08-14 03:32:27 +03:00
|
|
|
golden: (path: string) => string;
|
2020-08-11 02:48:34 +03:00
|
|
|
playwright: typeof import('../index');
|
|
|
|
browserType: BrowserType<Browser>;
|
|
|
|
browser: Browser;
|
2020-08-14 03:32:27 +03:00
|
|
|
tmpDir: string;
|
2020-08-11 02:48:34 +03:00
|
|
|
}
|
|
|
|
interface FixtureState {
|
|
|
|
toImpl: (rpcObject: any) => any;
|
|
|
|
context: BrowserContext;
|
|
|
|
server: TestServer;
|
|
|
|
page: Page;
|
|
|
|
httpsServer: TestServer;
|
|
|
|
browserServer: BrowserServer;
|
2020-08-14 02:00:23 +03:00
|
|
|
}
|
2020-08-11 02:48:34 +03:00
|
|
|
}
|
|
|
|
|
2020-08-14 17:22:54 +03:00
|
|
|
(global as any).MAC = platform === 'darwin';
|
|
|
|
(global as any).LINUX = platform === 'linux';
|
|
|
|
(global as any).WIN = platform === 'win32';
|
|
|
|
(global as any).CHROMIUM = browserName === 'chromium';
|
|
|
|
(global as any).FFOX = browserName === 'firefox';
|
|
|
|
(global as any).WEBKIT = browserName === 'webkit';
|
|
|
|
|
2020-08-11 02:48:34 +03:00
|
|
|
registerWorkerFixture('parallelIndex', async ({}, test) => {
|
|
|
|
await test(parseInt(process.env.JEST_WORKER_ID, 10) - 1);
|
|
|
|
});
|
|
|
|
|
2020-08-14 03:32:27 +03:00
|
|
|
registerWorkerFixture('httpService', async ({parallelIndex}, test) => {
|
2020-08-11 02:48:34 +03:00
|
|
|
const assetsPath = path.join(__dirname, 'assets');
|
|
|
|
const cachedPath = path.join(__dirname, 'assets', 'cached');
|
|
|
|
|
|
|
|
const port = 8907 + parallelIndex * 2;
|
|
|
|
const server = await TestServer.create(assetsPath, port);
|
|
|
|
server.enableHTTPCache(cachedPath);
|
|
|
|
|
|
|
|
const httpsPort = port + 1;
|
|
|
|
const httpsServer = await TestServer.createHTTPS(assetsPath, httpsPort);
|
|
|
|
httpsServer.enableHTTPCache(cachedPath);
|
|
|
|
|
|
|
|
await test({server, httpsServer});
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
server.stop(),
|
|
|
|
httpsServer.stop(),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2020-08-15 05:34:42 +03:00
|
|
|
const getExecutablePath = () => {
|
2020-08-11 02:48:34 +03:00
|
|
|
if (browserName === 'chromium' && process.env.CRPATH)
|
2020-08-15 05:34:42 +03:00
|
|
|
return process.env.CRPATH;
|
2020-08-11 02:48:34 +03:00
|
|
|
if (browserName === 'firefox' && process.env.FFPATH)
|
2020-08-15 05:34:42 +03:00
|
|
|
return process.env.FFPATH;
|
2020-08-11 02:48:34 +03:00
|
|
|
if (browserName === 'webkit' && process.env.WKPATH)
|
2020-08-15 05:34:42 +03:00
|
|
|
return process.env.WKPATH;
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
registerWorkerFixture('defaultBrowserOptions', async({}, test) => {
|
|
|
|
let executablePath = getExecutablePath();
|
|
|
|
|
2020-08-11 02:48:34 +03:00
|
|
|
if (executablePath)
|
|
|
|
console.error(`Using executable at ${executablePath}`);
|
|
|
|
await test({
|
|
|
|
handleSIGINT: false,
|
|
|
|
slowMo: valueFromEnv('SLOW_MO', 0),
|
|
|
|
headless: !!valueFromEnv('HEADLESS', true),
|
|
|
|
executablePath
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-12 21:48:30 +03:00
|
|
|
registerWorkerFixture('playwright', async({parallelIndex}, test) => {
|
2020-08-11 02:48:34 +03:00
|
|
|
const {coverage, uninstall} = installCoverageHooks(browserName);
|
2020-08-14 02:00:23 +03:00
|
|
|
if (process.env.PWWIRE) {
|
2020-08-11 02:48:34 +03:00
|
|
|
const connection = new Connection();
|
|
|
|
const spawnedProcess = childProcess.fork(path.join(__dirname, '..', 'lib', 'rpc', 'server'), [], {
|
|
|
|
stdio: 'pipe',
|
|
|
|
detached: true,
|
|
|
|
});
|
|
|
|
spawnedProcess.unref();
|
|
|
|
const onExit = (exitCode, signal) => {
|
|
|
|
throw new Error(`Server closed with exitCode=${exitCode} signal=${signal}`);
|
|
|
|
};
|
|
|
|
spawnedProcess.on('exit', onExit);
|
|
|
|
const transport = new Transport(spawnedProcess.stdin, spawnedProcess.stdout);
|
|
|
|
connection.onmessage = message => transport.send(JSON.stringify(message));
|
|
|
|
transport.onmessage = message => connection.dispatch(JSON.parse(message));
|
|
|
|
const playwrightObject = await connection.waitForObjectWithKnownName('Playwright');
|
2020-08-13 03:51:07 +03:00
|
|
|
await test(playwrightObject);
|
|
|
|
spawnedProcess.removeListener('exit', onExit);
|
|
|
|
spawnedProcess.stdin.destroy();
|
|
|
|
spawnedProcess.stdout.destroy();
|
|
|
|
spawnedProcess.stderr.destroy();
|
|
|
|
await teardownCoverage();
|
2020-08-11 02:48:34 +03:00
|
|
|
} else {
|
2020-08-13 03:51:07 +03:00
|
|
|
await test(require('../index'))
|
|
|
|
await teardownCoverage();
|
2020-08-11 02:48:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async function teardownCoverage() {
|
|
|
|
uninstall();
|
2020-08-12 21:48:30 +03:00
|
|
|
const coveragePath = path.join(path.join(__dirname, 'output-' + browserName), 'coverage', parallelIndex + '.json');
|
2020-08-11 02:48:34 +03:00
|
|
|
const coverageJSON = [...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');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
registerFixture('toImpl', async ({playwright}, test) => {
|
|
|
|
await test((playwright as any)._toImpl);
|
|
|
|
});
|
|
|
|
|
|
|
|
registerWorkerFixture('browserType', async ({playwright}, test) => {
|
2020-08-15 05:34:42 +03:00
|
|
|
const browserType = playwright[process.env.BROWSER || 'chromium']
|
|
|
|
const executablePath = getExecutablePath()
|
|
|
|
if (executablePath)
|
|
|
|
browserType._executablePath = executablePath
|
|
|
|
await test(browserType);
|
2020-08-11 02:48:34 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
registerWorkerFixture('browser', async ({browserType, defaultBrowserOptions}, test) => {
|
|
|
|
const browser = await browserType.launch(defaultBrowserOptions);
|
2020-08-13 03:51:07 +03:00
|
|
|
await test(browser);
|
|
|
|
if (browser.contexts().length !== 0) {
|
|
|
|
console.warn(`\nWARNING: test did not close all created contexts! ${new Error().stack}\n`);
|
|
|
|
await Promise.all(browser.contexts().map(context => context.close())).catch(e => void 0);
|
2020-08-11 02:48:34 +03:00
|
|
|
}
|
2020-08-13 03:51:07 +03:00
|
|
|
await browser.close();
|
2020-08-11 02:48:34 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
registerFixture('context', async ({browser}, test) => {
|
|
|
|
const context = await browser.newContext();
|
2020-08-13 03:51:07 +03:00
|
|
|
await test(context);
|
|
|
|
await context.close();
|
2020-08-11 02:48:34 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
registerFixture('page', async ({context}, test) => {
|
|
|
|
const page = await context.newPage();
|
|
|
|
await test(page);
|
|
|
|
});
|
|
|
|
|
2020-08-14 03:32:27 +03:00
|
|
|
registerFixture('server', async ({httpService}, test) => {
|
|
|
|
httpService.server.reset();
|
|
|
|
await test(httpService.server);
|
2020-08-11 02:48:34 +03:00
|
|
|
});
|
|
|
|
|
2020-08-14 03:32:27 +03:00
|
|
|
registerFixture('browserName', async ({}, test) => {
|
|
|
|
await test(browserName);
|
2020-08-11 02:48:34 +03:00
|
|
|
});
|
2020-08-13 08:41:40 +03:00
|
|
|
|
2020-08-14 03:32:27 +03:00
|
|
|
registerFixture('httpsServer', async ({httpService}, test) => {
|
|
|
|
httpService.httpsServer.reset();
|
|
|
|
await test(httpService.httpsServer);
|
|
|
|
});
|
|
|
|
|
|
|
|
registerFixture('tmpDir', async ({}, test) => {
|
|
|
|
const tmpDir = await mkdtempAsync(path.join(os.tmpdir(), 'playwright-test-'));
|
|
|
|
await test(tmpDir);
|
2020-08-14 17:22:54 +03:00
|
|
|
await removeFolderAsync(tmpDir).catch(e => {});
|
2020-08-14 03:32:27 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
registerWorkerFixture('asset', async ({}, test) => {
|
|
|
|
await test(p => path.join(__dirname, `assets`, p));
|
|
|
|
});
|
|
|
|
|
|
|
|
registerWorkerFixture('golden', async ({browserName}, test) => {
|
|
|
|
await test(p => path.join(__dirname, `golden-${browserName}`, p));
|
2020-08-13 08:41:40 +03:00
|
|
|
});
|