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';
|
|
|
|
import type { Browser, BrowserContext, BrowserContextOptions, BrowserType, LaunchOptions, Page } from '../index';
|
2020-09-11 07:31:46 +03:00
|
|
|
|
2020-09-27 02:05:58 +03:00
|
|
|
// Parameter declarations ------------------------------------------------------
|
|
|
|
|
2020-09-26 20:59:27 +03:00
|
|
|
type PlaywrightParameters = {
|
2020-09-27 02:05:58 +03:00
|
|
|
// Browser name, one of 'chromium', 'webkit' and 'firefox', can be specified via
|
|
|
|
// environment BROWSER=webkit or via command line, --browser-name=webkit
|
|
|
|
browserName: 'chromium' | 'firefox' | 'webkit';
|
|
|
|
// Run tests in a headful mode, can be specified via environment HEADFUL=1 or via
|
|
|
|
// command line, --headful. Defaults to false.
|
|
|
|
headful: boolean;
|
|
|
|
// Slows down Playwright operations by the specified amount of milliseconds.
|
|
|
|
// Useful so that you can see what is going on. Defaults to 0.
|
|
|
|
slowMo: number;
|
|
|
|
// Whether to take screenshots on failure, --screenshot-on-failure. Defaults to false.
|
|
|
|
screenshotOnFailure: boolean;
|
|
|
|
// Whether to record the execution trace
|
|
|
|
trace: boolean;
|
2020-09-29 21:51:00 +03:00
|
|
|
// Host platform
|
|
|
|
platform: 'win32' | 'linux' | 'darwin';
|
2020-09-11 20:02:07 +03:00
|
|
|
};
|
|
|
|
|
2020-09-27 02:05:58 +03:00
|
|
|
|
|
|
|
// Worker fixture declarations -------------------------------------------------
|
|
|
|
// ... these live as long as the worker process.
|
|
|
|
|
2020-09-26 20:59:27 +03:00
|
|
|
type PlaywrightWorkerFixtures = {
|
2020-09-27 02:05:58 +03:00
|
|
|
// Playwright library.
|
2020-09-26 20:59:27 +03:00
|
|
|
playwright: typeof import('../index');
|
2020-09-27 02:05:58 +03:00
|
|
|
// Browser type (Chromium / WebKit / Firefox)
|
2020-09-26 20:59:27 +03:00
|
|
|
browserType: BrowserType<Browser>;
|
2020-09-27 02:05:58 +03:00
|
|
|
// Default browserType.launch() options.
|
|
|
|
defaultBrowserOptions: LaunchOptions;
|
|
|
|
// Factory for creating a browser with given additional options.
|
|
|
|
browserFactory: (options?: LaunchOptions) => Promise<Browser>;
|
|
|
|
// Browser instance, shared for the worker.
|
2020-09-26 20:59:27 +03:00
|
|
|
browser: Browser;
|
2020-09-27 02:05:58 +03:00
|
|
|
// True iff browserName is Chromium
|
2020-09-26 20:59:27 +03:00
|
|
|
isChromium: boolean;
|
2020-09-27 02:05:58 +03:00
|
|
|
// True iff browserName is Firefox
|
2020-09-26 20:59:27 +03:00
|
|
|
isFirefox: boolean;
|
2020-09-27 02:05:58 +03:00
|
|
|
// True iff browserName is WebKit
|
2020-09-26 20:59:27 +03:00
|
|
|
isWebKit: boolean;
|
2020-09-29 21:51:00 +03:00
|
|
|
// True iff running on Windows.
|
|
|
|
isWindows: boolean;
|
|
|
|
// True iff running on Mac.
|
|
|
|
isMac: boolean;
|
|
|
|
// True iff running on Linux.
|
|
|
|
isLinux: boolean;
|
2020-09-11 07:31:46 +03:00
|
|
|
};
|
|
|
|
|
2020-09-27 02:05:58 +03:00
|
|
|
|
|
|
|
// Test fixture definitions, those are created for each test ------------------
|
|
|
|
|
2020-09-26 20:59:27 +03:00
|
|
|
type PlaywrightTestFixtures = {
|
2020-09-27 02:05:58 +03:00
|
|
|
// Default browser.newContext() options.
|
|
|
|
defaultContextOptions: BrowserContextOptions;
|
|
|
|
// Factory for creating a context with given additional options.
|
|
|
|
contextFactory: (options?: BrowserContextOptions) => Promise<BrowserContext>;
|
|
|
|
// Context instance for test.
|
2020-09-26 20:59:27 +03:00
|
|
|
context: BrowserContext;
|
2020-09-27 02:05:58 +03:00
|
|
|
// Page instance for test.
|
2020-09-26 20:59:27 +03:00
|
|
|
page: Page;
|
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>();
|
|
|
|
|
2020-09-27 02:05:58 +03:00
|
|
|
// Parameter and matrix definitions --------------------------------------------
|
2020-08-11 02:48:34 +03:00
|
|
|
|
2020-09-27 02:05:58 +03:00
|
|
|
fixtures.defineParameter('browserName', 'Browser type name', process.env.BROWSER || 'chromium' as any);
|
|
|
|
fixtures.defineParameter('headful', 'Whether to run tests headless or headful', process.env.HEADFUL ? true : false);
|
2020-09-29 21:51:00 +03:00
|
|
|
fixtures.defineParameter('platform', 'Operating system', process.platform as ('win32' | 'linux' | 'darwin'));
|
2020-09-27 02:05:58 +03:00
|
|
|
fixtures.defineParameter('screenshotOnFailure', 'Generate screenshot on failure', false);
|
|
|
|
fixtures.defineParameter('slowMo', 'Slows down Playwright operations by the specified amount of milliseconds', 0);
|
|
|
|
fixtures.defineParameter('trace', 'Whether to record the execution trace', !!process.env.TRACING || false);
|
|
|
|
|
|
|
|
// If browser is not specified, we are running tests against all three browsers.
|
|
|
|
fixtures.generateParametrizedTests(
|
|
|
|
'browserName',
|
|
|
|
process.env.BROWSER ? [process.env.BROWSER] as any : ['chromium', 'webkit', 'firefox']);
|
2020-08-15 05:34:42 +03:00
|
|
|
|
2020-09-27 02:05:58 +03:00
|
|
|
// Worker fixtures definitions -------------------------------------------------
|
|
|
|
|
|
|
|
fixtures.defineWorkerFixture('defaultBrowserOptions', async ({ headful, slowMo }, runTest) => {
|
2020-09-18 21:54:00 +03:00
|
|
|
await runTest({
|
2020-08-11 02:48:34 +03:00
|
|
|
handleSIGINT: false,
|
2020-09-27 02:05:58 +03:00
|
|
|
slowMo,
|
|
|
|
headless: !headful,
|
2020-09-25 22:52:06 +03:00
|
|
|
artifactsPath: config.outputDir,
|
2020-08-11 02:48:34 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-09-27 02:05:58 +03:00
|
|
|
fixtures.defineWorkerFixture('playwright', async ({}, runTest) => {
|
2020-09-26 20:59:27 +03:00
|
|
|
const playwright = require('../index');
|
2020-09-27 02:05:58 +03:00
|
|
|
await runTest(playwright);
|
2020-09-26 20:59:27 +03:00
|
|
|
});
|
2020-08-11 02:48:34 +03:00
|
|
|
|
2020-09-27 02:05:58 +03:00
|
|
|
fixtures.defineWorkerFixture('browserType', async ({playwright, browserName}, runTest) => {
|
2020-09-26 20:59:27 +03:00
|
|
|
const browserType = playwright[browserName];
|
2020-09-27 02:05:58 +03:00
|
|
|
await runTest(browserType);
|
2020-09-26 20:59:27 +03:00
|
|
|
});
|
|
|
|
|
2020-09-27 02:05:58 +03:00
|
|
|
fixtures.defineWorkerFixture('browser', async ({browserType, defaultBrowserOptions}, runTest) => {
|
|
|
|
const browser = await browserType.launch(defaultBrowserOptions);
|
|
|
|
await runTest(browser);
|
|
|
|
await browser.close();
|
2020-08-21 03:45:10 +03:00
|
|
|
});
|
|
|
|
|
2020-09-27 02:05:58 +03:00
|
|
|
fixtures.defineWorkerFixture('isChromium', async ({browserName}, runTest) => {
|
|
|
|
await runTest(browserName === 'chromium');
|
2020-09-26 20:59:27 +03:00
|
|
|
});
|
|
|
|
|
2020-09-27 02:05:58 +03:00
|
|
|
fixtures.defineWorkerFixture('isFirefox', async ({browserName}, runTest) => {
|
|
|
|
await runTest(browserName === 'firefox');
|
2020-08-11 02:48:34 +03:00
|
|
|
});
|
|
|
|
|
2020-09-27 02:05:58 +03:00
|
|
|
fixtures.defineWorkerFixture('isWebKit', async ({browserName}, runTest) => {
|
|
|
|
await runTest(browserName === 'webkit');
|
2020-08-25 06:26:06 +03:00
|
|
|
});
|
2020-09-26 20:59:27 +03:00
|
|
|
|
2020-09-29 21:51:00 +03:00
|
|
|
fixtures.defineWorkerFixture('isWindows', async ({platform}, test) => {
|
|
|
|
await test(platform === 'win32');
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.defineWorkerFixture('isMac', async ({platform}, test) => {
|
|
|
|
await test(platform === 'darwin');
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.defineWorkerFixture('isLinux', async ({platform}, test) => {
|
|
|
|
await test(platform === 'linux');
|
|
|
|
});
|
|
|
|
|
2020-09-27 02:05:58 +03:00
|
|
|
// Test fixtures definitions ---------------------------------------------------
|
|
|
|
|
2020-10-01 02:52:21 +03:00
|
|
|
fixtures.defineTestFixture('defaultContextOptions', async ({ testRelativeArtifactsPath, trace, testInfo }, runTest) => {
|
|
|
|
if (trace || testInfo.retry) {
|
|
|
|
await runTest({
|
|
|
|
relativeArtifactsPath: testRelativeArtifactsPath,
|
|
|
|
recordTrace: true,
|
|
|
|
recordVideos: true,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await runTest({});
|
|
|
|
}
|
2020-09-27 02:05:58 +03:00
|
|
|
});
|
|
|
|
|
2020-09-29 21:51:00 +03:00
|
|
|
fixtures.defineTestFixture('contextFactory', async ({ browser, defaultContextOptions, testInfo, screenshotOnFailure, testOutputPath }, runTest) => {
|
2020-09-27 02:05:58 +03:00
|
|
|
const contexts: BrowserContext[] = [];
|
|
|
|
async function contextFactory(options: BrowserContextOptions = {}) {
|
|
|
|
const context = await browser.newContext({ ...defaultContextOptions, ...options });
|
|
|
|
contexts.push(context);
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
await runTest(contextFactory);
|
|
|
|
|
|
|
|
if (screenshotOnFailure && (testInfo.status !== testInfo.expectedStatus)) {
|
|
|
|
let ordinal = 0;
|
|
|
|
for (const context of contexts) {
|
|
|
|
for (const page of context.pages())
|
2020-09-29 21:51:00 +03:00
|
|
|
await page.screenshot({ timeout: 5000, path: testOutputPath + `-test-failed-${++ordinal}.png` });
|
2020-09-27 02:05:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const context of contexts)
|
|
|
|
await context.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.defineTestFixture('context', async ({ contextFactory }, runTest) => {
|
|
|
|
const context = await contextFactory();
|
2020-09-26 20:59:27 +03:00
|
|
|
await runTest(context);
|
2020-09-27 02:05:58 +03:00
|
|
|
// Context factory is taking care of closing the context,
|
|
|
|
// so that it could capture a screenshot on failure.
|
2020-09-26 20:59:27 +03:00
|
|
|
});
|
|
|
|
|
2020-09-27 02:05:58 +03:00
|
|
|
fixtures.defineTestFixture('page', async ({context}, runTest) => {
|
|
|
|
// Always create page off context so that they matched.
|
|
|
|
await runTest(await context.newPage());
|
|
|
|
// Context fixture is taking care of closing the page.
|
2020-09-26 20:59:27 +03:00
|
|
|
});
|
|
|
|
|
2020-09-29 21:51:00 +03:00
|
|
|
fixtures.overrideTestFixture('testParametersArtifactsPath', async ({ browserName, platform }, runTest) => {
|
|
|
|
await runTest(browserName + '-' + platform);
|
2020-09-27 02:05:58 +03:00
|
|
|
});
|