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';
|
2020-08-26 12:52:23 +03:00
|
|
|
import type { LaunchOptions, BrowserType, Browser, BrowserContext, Page, BrowserServer } from '../index';
|
2020-08-20 07:32:12 +03:00
|
|
|
import { TestServer } from '../utils/testserver';
|
2020-08-23 01:13:51 +03:00
|
|
|
import { Connection } from '../lib/client/connection';
|
2020-08-22 04:46:11 +03:00
|
|
|
import { Transport } from '../lib/protocol/transport';
|
2020-08-20 03:47:24 +03:00
|
|
|
import { installCoverageHooks } from './coverage';
|
2020-08-21 02:04:27 +03:00
|
|
|
import { parameters, registerFixture, registerWorkerFixture } from '../test-runner';
|
2020-08-28 07:08:33 +03:00
|
|
|
import { mkdtempAsync, removeFolderAsync } from './utils';
|
2020-08-11 02:48:34 +03:00
|
|
|
|
2020-08-23 02:44:56 +03:00
|
|
|
export const options = {
|
|
|
|
CHROMIUM: parameters.browserName === 'chromium',
|
|
|
|
FIREFOX: parameters.browserName === 'firefox',
|
|
|
|
WEBKIT: parameters.browserName === 'webkit',
|
2020-08-28 14:20:29 +03:00
|
|
|
HEADLESS: !!valueFromEnv('HEADLESS', true),
|
2020-08-23 02:44:56 +03:00
|
|
|
WIRE: !!process.env.PWWIRE,
|
|
|
|
SLOW_MO: valueFromEnv('SLOW_MO', 0),
|
2020-08-28 23:50:06 +03:00
|
|
|
// Tracing is currently not implemented under wire.
|
|
|
|
TRACING: valueFromEnv('TRACING', false) && !process.env.PWWIRE,
|
2020-08-28 14:20:29 +03:00
|
|
|
};
|
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>;
|
|
|
|
browser: Browser;
|
2020-08-21 02:04:27 +03:00
|
|
|
httpService: {server: TestServer, httpsServer: TestServer}
|
2020-08-21 03:45:10 +03:00
|
|
|
toImpl: (rpcObject: any) => any;
|
2020-08-11 02:48:34 +03:00
|
|
|
}
|
2020-08-20 22:51:05 +03:00
|
|
|
interface TestState {
|
2020-08-11 02:48:34 +03:00
|
|
|
context: BrowserContext;
|
|
|
|
server: TestServer;
|
|
|
|
page: Page;
|
|
|
|
httpsServer: TestServer;
|
|
|
|
browserServer: BrowserServer;
|
2020-08-25 06:26:06 +03:00
|
|
|
launchPersistent: (options?: Parameters<BrowserType<Browser>['launchPersistentContext']>[1]) => Promise<{context: BrowserContext, page: Page}>;
|
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-21 19:53:02 +03:00
|
|
|
declare global {
|
|
|
|
const MAC: boolean;
|
|
|
|
const LINUX: boolean;
|
|
|
|
const WIN: boolean;
|
|
|
|
}
|
2020-08-23 02:44:56 +03:00
|
|
|
const platform = os.platform();
|
2020-08-21 19:53:02 +03:00
|
|
|
global['MAC'] = platform === 'darwin';
|
|
|
|
global['LINUX'] = platform === 'linux';
|
|
|
|
global['WIN'] = platform === 'win32';
|
2020-08-14 17:22:54 +03:00
|
|
|
|
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-28 14:20:29 +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-28 14:20:29 +03:00
|
|
|
};
|
2020-08-15 05:34:42 +03:00
|
|
|
|
2020-08-28 14:20:29 +03:00
|
|
|
registerWorkerFixture('defaultBrowserOptions', async ({browserName}, test) => {
|
|
|
|
const 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,
|
2020-08-23 02:44:56 +03:00
|
|
|
slowMo: options.SLOW_MO,
|
|
|
|
headless: options.HEADLESS,
|
2020-08-11 02:48:34 +03:00
|
|
|
executablePath
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-28 14:20:29 +03:00
|
|
|
registerWorkerFixture('playwright', async ({browserName}, test) => {
|
2020-08-11 02:48:34 +03:00
|
|
|
const {coverage, uninstall} = installCoverageHooks(browserName);
|
2020-08-23 02:44:56 +03:00
|
|
|
if (options.WIRE) {
|
2020-08-11 02:48:34 +03:00
|
|
|
const connection = new Connection();
|
2020-08-25 03:32:55 +03:00
|
|
|
const spawnedProcess = childProcess.fork(path.join(__dirname, '..', 'lib', 'server.js'), [], {
|
2020-08-11 02:48:34 +03:00
|
|
|
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-28 14:20:29 +03:00
|
|
|
await test(require('../index'));
|
2020-08-13 03:51:07 +03:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2020-08-21 03:45:10 +03:00
|
|
|
registerWorkerFixture('toImpl', async ({playwright}, test) => {
|
2020-08-11 02:48:34 +03:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-08-20 22:51:05 +03:00
|
|
|
registerWorkerFixture('browserName', async ({}, test) => {
|
|
|
|
await test('chromium');
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-08-21 03:45:10 +03:00
|
|
|
registerWorkerFixture('asset', async ({}, test) => {
|
|
|
|
await test(p => path.join(__dirname, `assets`, p));
|
|
|
|
});
|
|
|
|
|
|
|
|
registerWorkerFixture('golden', async ({browserName}, test) => {
|
|
|
|
await test(p => path.join(browserName, p));
|
|
|
|
});
|
|
|
|
|
2020-08-28 20:51:55 +03:00
|
|
|
registerFixture('context', async ({browser, toImpl}, runTest, info) => {
|
2020-08-11 02:48:34 +03:00
|
|
|
const context = await browser.newContext();
|
2020-08-28 20:51:55 +03:00
|
|
|
const { test, config } = info;
|
2020-08-28 23:50:06 +03:00
|
|
|
if (options.TRACING) {
|
2020-08-28 20:51:55 +03:00
|
|
|
const traceStorageDir = path.join(config.outputDir, 'trace-storage');
|
|
|
|
const relativePath = path.relative(config.testDir, test.file).replace(/\.spec\.[jt]s/, '');
|
|
|
|
const sanitizedTitle = test.title.replace(/[^\w\d]+/g, '_');
|
|
|
|
const traceFile = path.join(config.outputDir, relativePath, sanitizedTitle + '.trace');
|
|
|
|
const tracerFactory = require('../lib/trace/tracer').Tracer;
|
|
|
|
(context as any).__tracer = new tracerFactory(traceStorageDir, traceFile);
|
|
|
|
(context as any).__snapshotter = await toImpl(context)._initSnapshotter((context as any).__tracer);
|
|
|
|
}
|
|
|
|
await runTest(context);
|
2020-08-13 03:51:07 +03:00
|
|
|
await context.close();
|
2020-08-28 20:51:55 +03:00
|
|
|
if ((context as any).__tracer)
|
|
|
|
await (context as any).__tracer.dispose();
|
2020-08-11 02:48:34 +03:00
|
|
|
});
|
|
|
|
|
2020-08-28 20:51:55 +03:00
|
|
|
registerFixture('page', async ({context, toImpl}, runTest, info) => {
|
2020-08-11 02:48:34 +03:00
|
|
|
const page = await context.newPage();
|
2020-08-23 02:44:56 +03:00
|
|
|
await runTest(page);
|
2020-08-27 00:14:23 +03:00
|
|
|
const { test, config, result } = info;
|
|
|
|
if (result.status === 'failed' || result.status === 'timedOut') {
|
2020-08-22 03:14:11 +03:00
|
|
|
const relativePath = path.relative(config.testDir, test.file).replace(/\.spec\.[jt]s/, '');
|
|
|
|
const sanitizedTitle = test.title.replace(/[^\w\d]+/g, '_');
|
|
|
|
const assetPath = path.join(config.outputDir, relativePath, sanitizedTitle) + '-failed.png';
|
2020-08-28 20:51:55 +03:00
|
|
|
await page.screenshot({ timeout: 5000, path: assetPath });
|
|
|
|
if ((context as any).__snapshotter)
|
|
|
|
await (context as any).__snapshotter.captureSnapshot(toImpl(page), { timeout: 5000, label: 'Test Failed' });
|
2020-08-21 03:45:10 +03:00
|
|
|
}
|
2020-08-11 02:48:34 +03:00
|
|
|
});
|
|
|
|
|
2020-08-25 06:26:06 +03:00
|
|
|
registerFixture('launchPersistent', async ({tmpDir, defaultBrowserOptions, browserType}, test) => {
|
|
|
|
let context;
|
|
|
|
async function launchPersistent(options) {
|
|
|
|
if (context)
|
|
|
|
throw new Error('can only launch one persitent context');
|
|
|
|
context = await browserType.launchPersistentContext(tmpDir, {...defaultBrowserOptions, ...options});
|
|
|
|
const page = context.pages()[0];
|
|
|
|
return {context, page};
|
|
|
|
}
|
|
|
|
await test(launchPersistent);
|
|
|
|
if (context)
|
|
|
|
await context.close();
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-08-23 02:44:56 +03:00
|
|
|
function valueFromEnv(name, defaultValue) {
|
|
|
|
if (!(name in process.env))
|
|
|
|
return defaultValue;
|
|
|
|
return JSON.parse(process.env[name]);
|
2020-08-20 03:47:24 +03:00
|
|
|
}
|