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';
|
2020-08-20 07:32:12 +03:00
|
|
|
import { TestServer } from '../utils/testserver';
|
2020-08-11 02:48:34 +03:00
|
|
|
import { Connection } from '../lib/rpc/client/connection';
|
|
|
|
import { Transport } from '../lib/rpc/transport';
|
|
|
|
import { setUnderTest } from '../lib/helper';
|
2020-08-20 03:47:24 +03:00
|
|
|
import { installCoverageHooks } from './coverage';
|
2020-08-20 07:32:12 +03:00
|
|
|
import { parameters, registerFixture, registerWorkerFixture, registerParameter } from './runner';
|
2020-08-14 03:32:27 +03:00
|
|
|
|
|
|
|
import {mkdtempAsync, removeFolderAsync} from './utils';
|
2020-08-11 02:48:34 +03:00
|
|
|
|
|
|
|
setUnderTest(); // Note: we must call setUnderTest before requiring Playwright
|
|
|
|
|
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
|
|
|
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>;
|
2020-08-17 20:33:42 +03:00
|
|
|
browserName: string;
|
2020-08-11 02:48:34 +03:00
|
|
|
browser: Browser;
|
|
|
|
}
|
|
|
|
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-20 07:32:12 +03:00
|
|
|
interface FixtureParameters {
|
2020-08-19 09:00:45 +03:00
|
|
|
browserName: string;
|
|
|
|
}
|
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';
|
|
|
|
|
2020-08-20 03:47:24 +03:00
|
|
|
registerWorkerFixture('httpService', async ({}, test) => {
|
2020-08-11 02:48:34 +03:00
|
|
|
const assetsPath = path.join(__dirname, 'assets');
|
|
|
|
const cachedPath = path.join(__dirname, 'assets', 'cached');
|
|
|
|
|
2020-08-20 07:32:12 +03:00
|
|
|
const port = 8907 + parameters.parallelIndex * 2;
|
2020-08-11 02:48:34 +03:00
|
|
|
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-17 20:33:42 +03:00
|
|
|
const getExecutablePath = (browserName) => {
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:33:42 +03:00
|
|
|
registerWorkerFixture('defaultBrowserOptions', async({browserName}, test) => {
|
|
|
|
let executablePath = getExecutablePath(browserName);
|
2020-08-15 05:34:42 +03:00
|
|
|
|
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),
|
2020-08-19 23:30:54 +03:00
|
|
|
headless: options.HEADLESS,
|
2020-08-11 02:48:34 +03:00
|
|
|
executablePath
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-20 03:47:24 +03:00
|
|
|
registerWorkerFixture('playwright', async({browserName}, test) => {
|
2020-08-11 02:48:34 +03:00
|
|
|
const {coverage, uninstall} = installCoverageHooks(browserName);
|
2020-08-19 23:30:54 +03:00
|
|
|
if (options.WIRE) {
|
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-20 07:32:12 +03:00
|
|
|
const coveragePath = path.join(__dirname, 'coverage-report', parameters.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);
|
|
|
|
});
|
|
|
|
|
2020-08-17 20:33:42 +03:00
|
|
|
registerWorkerFixture('browserType', async ({playwright, browserName}, test) => {
|
2020-08-19 00:12:31 +03:00
|
|
|
const browserType = playwright[browserName];
|
2020-08-15 05:34:42 +03:00
|
|
|
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('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) => {
|
2020-08-19 00:12:31 +03:00
|
|
|
await test(p => path.join(browserName, p));
|
2020-08-13 08:41:40 +03:00
|
|
|
});
|
2020-08-19 09:00:45 +03:00
|
|
|
|
2020-08-20 07:32:12 +03:00
|
|
|
registerParameter('browserName', () => {
|
2020-08-19 09:00:45 +03:00
|
|
|
if (process.env.BROWSER)
|
|
|
|
return [process.env.BROWSER];
|
|
|
|
return ['chromium', 'webkit', 'firefox'];
|
|
|
|
});
|
2020-08-19 23:30:54 +03:00
|
|
|
|
2020-08-20 07:32:12 +03:00
|
|
|
export const options = {
|
|
|
|
CHROMIUM: () => parameters.browserName === 'chromium',
|
|
|
|
FIREFOX: () => parameters.browserName === 'firefox',
|
|
|
|
WEBKIT: () => parameters.browserName === 'webkit',
|
|
|
|
HEADLESS : !!valueFromEnv('HEADLESS', true),
|
|
|
|
WIRE: !!process.env.PWWIRE,
|
|
|
|
}
|
2020-08-20 03:47:24 +03:00
|
|
|
|
|
|
|
function valueFromEnv(name, defaultValue) {
|
|
|
|
if (!(name in process.env))
|
|
|
|
return defaultValue;
|
|
|
|
return JSON.parse(process.env[name]);
|
|
|
|
}
|