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-10-12 19:16:02 +03:00
|
|
|
import { config, folio as base } from 'folio';
|
2020-09-26 20:59:27 +03:00
|
|
|
import type { Browser, BrowserContext, BrowserContextOptions, BrowserType, LaunchOptions, Page } from '../index';
|
2020-10-01 21:06:19 +03:00
|
|
|
import * as path from 'path';
|
2020-09-11 07:31:46 +03:00
|
|
|
|
2020-10-12 19:16:02 +03:00
|
|
|
// Parameters ------------------------------------------------------------------
|
|
|
|
// ... these can be used to run tests in different modes.
|
|
|
|
|
|
|
|
type PlaywrightParameters = {
|
|
|
|
// Browser type name.
|
|
|
|
browserName: 'chromium' | 'firefox' | 'webkit';
|
|
|
|
// Whether to run tests headless or headful.
|
|
|
|
headful: boolean;
|
|
|
|
// Operating system.
|
|
|
|
platform: 'win32' | 'linux' | 'darwin';
|
|
|
|
// Generate screenshot on failure.
|
|
|
|
screenshotOnFailure: boolean;
|
|
|
|
// Slows down Playwright operations by the specified amount of milliseconds.
|
|
|
|
slowMo: number;
|
|
|
|
// Whether to record the execution trace.
|
|
|
|
trace: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
// 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-10-12 19:16:02 +03:00
|
|
|
const fixtures = base.extend<PlaywrightWorkerFixtures, PlaywrightTestFixtures, PlaywrightParameters>();
|
|
|
|
fixtures.browserName.initParameter('Browser type name', (process.env.BROWSER || 'chromium') as 'chromium' | 'firefox' | 'webkit');
|
|
|
|
fixtures.headful.initParameter('Whether to run tests headless or headful', process.env.HEADFUL ? true : false);
|
|
|
|
fixtures.platform.initParameter('Operating system', process.platform as ('win32' | 'linux' | 'darwin'));
|
|
|
|
fixtures.screenshotOnFailure.initParameter('Generate screenshot on failure', false);
|
|
|
|
fixtures.slowMo.initParameter('Slows down Playwright operations by the specified amount of milliseconds', 0);
|
|
|
|
fixtures.trace.initParameter('Whether to record the execution trace', !!process.env.TRACING);
|
|
|
|
|
|
|
|
fixtures.defaultBrowserOptions.initWorker(async ({ headful, slowMo }, run) => {
|
|
|
|
await run({
|
|
|
|
handleSIGINT: false,
|
|
|
|
slowMo,
|
|
|
|
headless: !headful,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.playwright.initWorker(async ({ }, run) => {
|
|
|
|
const playwright = require('../index');
|
|
|
|
await run(playwright);
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.browserType.initWorker(async ({ playwright, browserName }, run) => {
|
|
|
|
const browserType = (playwright as any)[browserName];
|
|
|
|
await run(browserType);
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.browser.initWorker(async ({ browserType, defaultBrowserOptions }, run) => {
|
|
|
|
const browser = await browserType.launch(defaultBrowserOptions);
|
|
|
|
await run(browser);
|
|
|
|
await browser.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.isChromium.initWorker(async ({ browserName }, run) => {
|
|
|
|
await run(browserName === 'chromium');
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.isFirefox.initWorker(async ({ browserName }, run) => {
|
|
|
|
await run(browserName === 'firefox');
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.isWebKit.initWorker(async ({ browserName }, run) => {
|
|
|
|
await run(browserName === 'webkit');
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.isWindows.initWorker(async ({ platform }, run) => {
|
|
|
|
await run(platform === 'win32');
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.isMac.initWorker(async ({ platform }, run) => {
|
|
|
|
await run(platform === 'darwin');
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.isLinux.initWorker(async ({ platform }, run) => {
|
|
|
|
await run(platform === 'linux');
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.defaultContextOptions.initTest(async ({ trace, testInfo }, run) => {
|
|
|
|
if (trace || testInfo.retry) {
|
|
|
|
await run({
|
|
|
|
_traceResourcesPath: path.join(config.outputDir, 'trace-resources'),
|
|
|
|
_tracePath: testInfo.outputPath('playwright.trace'),
|
|
|
|
videosPath: testInfo.outputPath(''),
|
|
|
|
} as any);
|
|
|
|
} else {
|
|
|
|
await run({});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.contextFactory.initTest(async ({ browser, defaultContextOptions, testInfo, screenshotOnFailure }, run) => {
|
|
|
|
const contexts: BrowserContext[] = [];
|
|
|
|
async function contextFactory(options: BrowserContextOptions = {}) {
|
|
|
|
const context = await browser.newContext({ ...defaultContextOptions, ...options });
|
|
|
|
contexts.push(context);
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
await run(contextFactory);
|
|
|
|
|
|
|
|
if (screenshotOnFailure && (testInfo.status !== testInfo.expectedStatus)) {
|
|
|
|
let ordinal = 0;
|
|
|
|
for (const context of contexts) {
|
|
|
|
for (const page of context.pages())
|
|
|
|
await page.screenshot({ timeout: 5000, path: testInfo.outputPath + `-test-failed-${++ordinal}.png` });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const context of contexts)
|
|
|
|
await context.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.context.initTest(async ({ contextFactory }, run) => {
|
|
|
|
const context = await contextFactory();
|
|
|
|
await run(context);
|
|
|
|
// Context factory is taking care of closing the context,
|
|
|
|
// so that it could capture a screenshot on failure.
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.page.initTest(async ({ context }, run) => {
|
|
|
|
// Always create page off context so that they matched.
|
|
|
|
await run(await context.newPage());
|
|
|
|
// Context fixture is taking care of closing the page.
|
|
|
|
});
|
|
|
|
|
|
|
|
fixtures.testParametersPathSegment.overrideTest(async ({ browserName, platform }, run) => {
|
|
|
|
await run(browserName + '-' + platform);
|
|
|
|
});
|
|
|
|
|
|
|
|
export const folio = fixtures.build();
|
2020-09-27 02:05:58 +03:00
|
|
|
|
|
|
|
// If browser is not specified, we are running tests against all three browsers.
|
2020-10-12 19:16:02 +03:00
|
|
|
folio.generateParametrizedTests(
|
2020-09-27 02:05:58 +03:00
|
|
|
'browserName',
|
|
|
|
process.env.BROWSER ? [process.env.BROWSER] as any : ['chromium', 'webkit', 'firefox']);
|