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.
|
|
|
|
*/
|
|
|
|
|
2020-09-26 20:59:27 +03:00
|
|
|
import { config, fixtures as baseFixtures } from '@playwright/test-runner';
|
2020-08-11 02:48:34 +03:00
|
|
|
import fs from 'fs';
|
2020-09-26 20:59:27 +03:00
|
|
|
import os from 'os';
|
2020-09-26 05:27:09 +03:00
|
|
|
import path from 'path';
|
2020-09-19 01:52:14 +03:00
|
|
|
import util from 'util';
|
2020-09-26 20:59:27 +03:00
|
|
|
import type { Browser, BrowserContext, BrowserContextOptions, BrowserType, LaunchOptions, Page } from '../index';
|
2020-09-11 07:31:46 +03:00
|
|
|
|
2020-09-26 20:59:27 +03:00
|
|
|
const mkdtempAsync = util.promisify(fs.mkdtemp);
|
2020-09-19 01:52:14 +03:00
|
|
|
const removeFolderAsync = util.promisify(require('rimraf'));
|
|
|
|
|
2020-09-26 20:59:27 +03:00
|
|
|
type PlaywrightParameters = {
|
2020-09-11 20:02:07 +03:00
|
|
|
browserName: string;
|
|
|
|
};
|
|
|
|
|
2020-09-26 20:59:27 +03:00
|
|
|
type PlaywrightWorkerFixtures = {
|
|
|
|
defaultBrowserOptions: LaunchOptions;
|
|
|
|
playwright: typeof import('../index');
|
|
|
|
browserType: BrowserType<Browser>;
|
|
|
|
browser: Browser;
|
|
|
|
isChromium: boolean;
|
|
|
|
isFirefox: boolean;
|
|
|
|
isWebKit: boolean;
|
2020-09-11 07:31:46 +03:00
|
|
|
};
|
|
|
|
|
2020-09-26 20:59:27 +03:00
|
|
|
type PlaywrightTestFixtures = {
|
|
|
|
context: BrowserContext;
|
|
|
|
page: Page;
|
|
|
|
testOutputDir: string;
|
|
|
|
tmpDir: string;
|
2020-09-11 07:31:46 +03:00
|
|
|
};
|
|
|
|
|
2020-09-26 20:59:27 +03:00
|
|
|
export const fixtures = baseFixtures
|
|
|
|
.declareParameters<PlaywrightParameters>()
|
|
|
|
.declareWorkerFixtures<PlaywrightWorkerFixtures>()
|
|
|
|
.declareTestFixtures<PlaywrightTestFixtures>();
|
|
|
|
|
|
|
|
const { defineTestFixture, defineWorkerFixture, defineParameter, generateParametrizedTests } = fixtures;
|
2020-08-11 02:48:34 +03:00
|
|
|
|
2020-08-23 02:44:56 +03:00
|
|
|
export const options = {
|
2020-09-17 07:36:36 +03:00
|
|
|
CHROMIUM: (parameters: PlaywrightParameters) => parameters.browserName === 'chromium',
|
|
|
|
FIREFOX: (parameters: PlaywrightParameters) => parameters.browserName === 'firefox',
|
|
|
|
WEBKIT: (parameters: PlaywrightParameters) => parameters.browserName === 'webkit',
|
2020-09-26 20:59:27 +03:00
|
|
|
HEADLESS: !!valueFromEnv('HEADLESS', true),
|
|
|
|
SLOW_MO: valueFromEnv('SLOW_MO', 0),
|
|
|
|
TRACING: valueFromEnv('TRACING', false),
|
2020-08-28 14:20:29 +03:00
|
|
|
};
|
2020-08-15 05:34:42 +03:00
|
|
|
|
2020-09-26 20:59:27 +03:00
|
|
|
defineWorkerFixture('defaultBrowserOptions', async ({}, runTest) => {
|
2020-09-18 21:54:00 +03:00
|
|
|
await runTest({
|
2020-08-11 02:48:34 +03:00
|
|
|
handleSIGINT: false,
|
2020-08-23 02:44:56 +03:00
|
|
|
slowMo: options.SLOW_MO,
|
|
|
|
headless: options.HEADLESS,
|
2020-09-25 22:52:06 +03:00
|
|
|
artifactsPath: config.outputDir,
|
2020-08-11 02:48:34 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-09-26 20:59:27 +03:00
|
|
|
defineWorkerFixture('playwright', async ({}, test) => {
|
|
|
|
const playwright = require('../index');
|
|
|
|
await test(playwright);
|
|
|
|
});
|
2020-08-11 02:48:34 +03:00
|
|
|
|
2020-09-26 20:59:27 +03:00
|
|
|
defineWorkerFixture('browserType', async ({playwright, browserName}, test) => {
|
|
|
|
const browserType = playwright[browserName];
|
|
|
|
await test(browserType);
|
|
|
|
});
|
|
|
|
|
|
|
|
defineParameter('browserName', 'Browser type name', '');
|
|
|
|
|
|
|
|
generateParametrizedTests(
|
|
|
|
'browserName',
|
|
|
|
process.env.BROWSER ? [process.env.BROWSER] : ['chromium', 'webkit', 'firefox']);
|
|
|
|
|
|
|
|
defineWorkerFixture('isChromium', async ({browserName}, test) => {
|
|
|
|
await test(browserName === 'chromium');
|
2020-08-11 02:48:34 +03:00
|
|
|
});
|
|
|
|
|
2020-09-26 20:59:27 +03:00
|
|
|
defineWorkerFixture('isFirefox', async ({browserName}, test) => {
|
|
|
|
await test(browserName === 'firefox');
|
2020-08-21 03:45:10 +03:00
|
|
|
});
|
|
|
|
|
2020-09-26 20:59:27 +03:00
|
|
|
defineWorkerFixture('isWebKit', async ({browserName}, test) => {
|
|
|
|
await test(browserName === 'webkit');
|
|
|
|
});
|
|
|
|
|
|
|
|
defineWorkerFixture('browser', async ({browserType, defaultBrowserOptions}, test) => {
|
|
|
|
const browser = await browserType.launch(defaultBrowserOptions);
|
|
|
|
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-21 03:45:10 +03:00
|
|
|
}
|
2020-09-26 20:59:27 +03:00
|
|
|
await browser.close();
|
2020-08-11 02:48:34 +03:00
|
|
|
});
|
|
|
|
|
2020-09-27 00:16:22 +03:00
|
|
|
defineTestFixture('testOutputDir', async ({ testInfo, browserName }, runTest) => {
|
|
|
|
const relativePath = path.relative(config.testDir, testInfo.file)
|
|
|
|
.replace(/\.spec\.[jt]s/, '')
|
|
|
|
.replace(/\.test\.[jt]s/, '');
|
2020-09-26 20:59:27 +03:00
|
|
|
const sanitizedTitle = testInfo.title.replace(/[^\w\d]+/g, '_');
|
2020-09-27 00:16:22 +03:00
|
|
|
const testOutputDir = path.join(config.outputDir, relativePath, sanitizedTitle, browserName);
|
2020-09-26 20:59:27 +03:00
|
|
|
await runTest(testOutputDir);
|
2020-08-25 06:26:06 +03:00
|
|
|
});
|
2020-09-26 20:59:27 +03:00
|
|
|
|
|
|
|
defineTestFixture('context', async ({ browser, testOutputDir }, runTest) => {
|
|
|
|
const contextOptions: BrowserContextOptions = {
|
|
|
|
relativeArtifactsPath: path.relative(config.outputDir, testOutputDir),
|
|
|
|
recordTrace: !!options.TRACING,
|
|
|
|
recordVideos: !!options.TRACING,
|
|
|
|
};
|
|
|
|
const context = await browser.newContext(contextOptions);
|
|
|
|
await runTest(context);
|
|
|
|
await context.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
defineTestFixture('page', async ({ context, testOutputDir, testInfo }, runTest) => {
|
|
|
|
const page = await context.newPage();
|
|
|
|
await runTest(page);
|
|
|
|
if (testInfo.status === 'failed' || testInfo.status === 'timedOut')
|
|
|
|
await page.screenshot({ timeout: 5000, path: path.join(testOutputDir, 'test-failed.png') });
|
|
|
|
});
|
|
|
|
|
|
|
|
defineTestFixture('tmpDir', async ({}, test) => {
|
|
|
|
const tmpDir = await mkdtempAsync(path.join(os.tmpdir(), 'playwright-test-'));
|
|
|
|
await test(tmpDir);
|
|
|
|
await removeFolderAsync(tmpDir).catch(e => {});
|
|
|
|
});
|
|
|
|
|
|
|
|
function valueFromEnv(name, defaultValue) {
|
|
|
|
if (!(name in process.env))
|
|
|
|
return defaultValue;
|
|
|
|
return JSON.parse(process.env[name]);
|
|
|
|
}
|