2021-04-02 02:35:26 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-10-26 23:45:53 +03:00
|
|
|
import type { Fixtures, PlaywrightTestOptions, PlaywrightWorkerOptions } from '@playwright/test';
|
2021-10-11 17:52:17 +03:00
|
|
|
import type { Browser, BrowserContext, BrowserContextOptions, BrowserType, LaunchOptions, Page } from 'playwright-core';
|
2021-10-22 22:59:52 +03:00
|
|
|
import { removeFolders } from 'playwright-core/lib/utils/utils';
|
2021-10-27 01:41:18 +03:00
|
|
|
import { browserOptionsWorkerFixture, browserTypeWorkerFixture, browserWorkerFixture, ReuseBrowserContextStorage } from '../../packages/playwright-test/lib/index';
|
2021-04-29 21:11:32 +03:00
|
|
|
import * as path from 'path';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as os from 'os';
|
|
|
|
import { RemoteServer, RemoteServerOptions } from './remoteServer';
|
2021-10-27 18:28:53 +03:00
|
|
|
import { baseTest } from './baseTest';
|
2021-09-22 02:24:48 +03:00
|
|
|
import { CommonFixtures } from './commonFixtures';
|
2021-10-22 22:59:52 +03:00
|
|
|
import type { ParsedStackTrace } from 'playwright-core/lib/utils/stackTrace';
|
2021-10-27 18:28:53 +03:00
|
|
|
import { DefaultTestMode, DriverTestMode, ServiceTestMode } from './testMode';
|
|
|
|
import { TestModeWorkerFixtures } from './testModeFixtures';
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-05-17 05:58:26 +03:00
|
|
|
export type PlaywrightWorkerFixtures = {
|
2021-10-27 18:28:53 +03:00
|
|
|
playwright: typeof import('playwright-core');
|
2021-10-26 23:45:53 +03:00
|
|
|
_browserType: BrowserType;
|
|
|
|
_browserOptions: LaunchOptions;
|
2021-04-29 21:11:32 +03:00
|
|
|
browserType: BrowserType;
|
|
|
|
browserOptions: LaunchOptions;
|
2021-05-17 05:58:26 +03:00
|
|
|
browser: Browser;
|
|
|
|
browserVersion: string;
|
2021-10-26 23:45:53 +03:00
|
|
|
_reuseBrowserContext: ReuseBrowserContextStorage;
|
2021-10-27 18:28:53 +03:00
|
|
|
toImpl: (rpcObject: any) => any;
|
2021-04-29 21:11:32 +03:00
|
|
|
};
|
2021-10-26 23:45:53 +03:00
|
|
|
|
2021-05-17 05:58:26 +03:00
|
|
|
type PlaywrightTestFixtures = {
|
|
|
|
createUserDataDir: () => Promise<string>;
|
|
|
|
launchPersistent: (options?: Parameters<BrowserType['launchPersistentContext']>[1]) => Promise<{ context: BrowserContext, page: Page }>;
|
|
|
|
startRemoteServer: (options?: RemoteServerOptions) => Promise<RemoteServer>;
|
|
|
|
contextOptions: BrowserContextOptions;
|
|
|
|
contextFactory: (options?: BrowserContextOptions) => Promise<BrowserContext>;
|
|
|
|
context: BrowserContext;
|
|
|
|
page: Page;
|
|
|
|
};
|
|
|
|
|
2021-10-27 18:28:53 +03:00
|
|
|
export const playwrightFixtures: Fixtures<PlaywrightTestOptions & PlaywrightTestFixtures, PlaywrightWorkerOptions & PlaywrightWorkerFixtures, CommonFixtures, TestModeWorkerFixtures> = {
|
2021-05-17 05:58:26 +03:00
|
|
|
hasTouch: undefined,
|
|
|
|
|
2021-10-27 18:28:53 +03:00
|
|
|
playwright: [ async ({ mode }, run) => {
|
|
|
|
const testMode = {
|
|
|
|
default: new DefaultTestMode(),
|
|
|
|
service: new ServiceTestMode(),
|
|
|
|
driver: new DriverTestMode(),
|
|
|
|
}[mode];
|
|
|
|
require('playwright-core/lib/utils/utils').setUnderTest();
|
|
|
|
const playwright = await testMode.setup();
|
|
|
|
await run(playwright);
|
|
|
|
await testMode.teardown();
|
|
|
|
}, { scope: 'worker' } ],
|
|
|
|
|
|
|
|
toImpl: [ async ({ playwright }, run) => run((playwright as any)._toImpl), { scope: 'worker' } ],
|
|
|
|
|
2021-10-26 23:45:53 +03:00
|
|
|
_browserType: [browserTypeWorkerFixture, { scope: 'worker' } ],
|
|
|
|
_browserOptions: [browserOptionsWorkerFixture, { scope: 'worker' } ],
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-10-26 23:45:53 +03:00
|
|
|
launchOptions: [ {}, { scope: 'worker' } ],
|
|
|
|
browserType: [async ({ _browserType }, use) => use(_browserType), { scope: 'worker' } ],
|
|
|
|
browserOptions: [async ({ _browserOptions }, use) => use(_browserOptions), { scope: 'worker' } ],
|
|
|
|
browser: [browserWorkerFixture, { scope: 'worker' } ],
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-05-17 05:58:26 +03:00
|
|
|
browserVersion: [async ({ browser }, run) => {
|
|
|
|
await run(browser.version());
|
|
|
|
}, { scope: 'worker' } ],
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-10-08 00:01:08 +03:00
|
|
|
_reuseBrowserContext: [new ReuseBrowserContextStorage(), { scope: 'worker' }],
|
|
|
|
|
2021-05-17 05:58:26 +03:00
|
|
|
createUserDataDir: async ({}, run) => {
|
|
|
|
const dirs: string[] = [];
|
2021-04-29 21:11:32 +03:00
|
|
|
// We do not put user data dir in testOutputPath,
|
|
|
|
// because we do not want to upload them as test result artifacts.
|
|
|
|
//
|
|
|
|
// Additionally, it is impossible to upload user data dir after test run:
|
|
|
|
// - Firefox removes lock file later, presumably from another watchdog process?
|
|
|
|
// - WebKit has circular symlinks that makes CI go crazy.
|
2021-05-17 05:58:26 +03:00
|
|
|
await run(async () => {
|
2021-06-03 19:55:33 +03:00
|
|
|
const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'playwright-test-'));
|
2021-05-17 05:58:26 +03:00
|
|
|
dirs.push(dir);
|
|
|
|
return dir;
|
|
|
|
});
|
|
|
|
await removeFolders(dirs);
|
|
|
|
},
|
|
|
|
|
|
|
|
launchPersistent: async ({ createUserDataDir, browserType, browserOptions }, run) => {
|
|
|
|
let persistentContext: BrowserContext | undefined;
|
|
|
|
await run(async options => {
|
|
|
|
if (persistentContext)
|
|
|
|
throw new Error('can only launch one persitent context');
|
|
|
|
const userDataDir = await createUserDataDir();
|
|
|
|
persistentContext = await browserType.launchPersistentContext(userDataDir, { ...browserOptions, ...options });
|
|
|
|
const page = persistentContext.pages()[0];
|
|
|
|
return { context: persistentContext, page };
|
|
|
|
});
|
|
|
|
if (persistentContext)
|
|
|
|
await persistentContext.close();
|
|
|
|
},
|
|
|
|
|
2021-09-22 02:24:48 +03:00
|
|
|
startRemoteServer: async ({ childProcess, browserType, browserOptions }, run) => {
|
2021-05-17 05:58:26 +03:00
|
|
|
let remoteServer: RemoteServer | undefined;
|
|
|
|
await run(async options => {
|
|
|
|
if (remoteServer)
|
|
|
|
throw new Error('can only start one remote server');
|
|
|
|
remoteServer = new RemoteServer();
|
2021-09-22 02:24:48 +03:00
|
|
|
await remoteServer._start(childProcess, browserType, browserOptions, options);
|
2021-05-17 05:58:26 +03:00
|
|
|
return remoteServer;
|
|
|
|
});
|
|
|
|
if (remoteServer)
|
|
|
|
await remoteServer.close();
|
|
|
|
},
|
|
|
|
|
2021-08-20 05:09:19 +03:00
|
|
|
contextOptions: async ({ video, hasTouch }, run, testInfo) => {
|
2021-05-08 01:25:55 +03:00
|
|
|
const debugName = path.relative(testInfo.project.outputDir, testInfo.outputDir).replace(/[\/\\]/g, '-');
|
2021-04-29 21:11:32 +03:00
|
|
|
const contextOptions = {
|
2021-10-26 23:45:53 +03:00
|
|
|
recordVideo: video === 'on' ? { dir: testInfo.outputPath('') } : undefined,
|
2021-04-29 21:11:32 +03:00
|
|
|
_debugName: debugName,
|
2021-05-17 05:58:26 +03:00
|
|
|
hasTouch,
|
2021-04-29 21:11:32 +03:00
|
|
|
} as BrowserContextOptions;
|
2021-05-17 05:58:26 +03:00
|
|
|
await run(contextOptions);
|
|
|
|
},
|
|
|
|
|
2021-08-20 05:09:19 +03:00
|
|
|
contextFactory: async ({ browser, contextOptions, trace }, run, testInfo) => {
|
2021-09-14 07:12:36 +03:00
|
|
|
const contexts = new Map<BrowserContext, { closed: boolean }>();
|
2021-05-17 05:58:26 +03:00
|
|
|
await run(async options => {
|
|
|
|
const context = await browser.newContext({ ...contextOptions, ...options });
|
2021-09-14 07:12:36 +03:00
|
|
|
contexts.set(context, { closed: false });
|
|
|
|
context.on('close', () => contexts.get(context).closed = true);
|
2021-10-26 23:45:53 +03:00
|
|
|
if (trace === 'on')
|
2021-10-26 04:56:57 +03:00
|
|
|
await context.tracing.start({ screenshots: true, snapshots: true, sources: true } as any);
|
2021-10-26 21:13:35 +03:00
|
|
|
(context as any)._instrumentation.addListener({
|
|
|
|
onApiCallBegin: (apiCall: string, stackTrace: ParsedStackTrace | null, userData: any) => {
|
2021-09-27 19:19:59 +03:00
|
|
|
if (apiCall.startsWith('expect.'))
|
|
|
|
return { userObject: null };
|
2021-09-10 03:35:31 +03:00
|
|
|
const testInfoImpl = testInfo as any;
|
2021-09-17 01:51:27 +03:00
|
|
|
const step = testInfoImpl._addStep({
|
2021-10-19 07:06:18 +03:00
|
|
|
location: stackTrace?.frames[0],
|
2021-09-17 01:51:27 +03:00
|
|
|
category: 'pw:api',
|
|
|
|
title: apiCall,
|
|
|
|
canHaveChildren: false,
|
|
|
|
forceNoParent: false
|
|
|
|
});
|
2021-10-26 21:13:35 +03:00
|
|
|
userData.userObject = step;
|
2021-09-15 21:34:23 +03:00
|
|
|
},
|
2021-10-26 21:13:35 +03:00
|
|
|
onApiCallEnd: (userData: any, error?: Error) => {
|
|
|
|
const step = userData.userObject;
|
2021-09-15 21:34:23 +03:00
|
|
|
step?.complete(error);
|
2021-08-05 23:36:47 +03:00
|
|
|
},
|
2021-10-26 21:13:35 +03:00
|
|
|
});
|
2021-05-17 05:58:26 +03:00
|
|
|
return context;
|
|
|
|
});
|
2021-09-14 07:12:36 +03:00
|
|
|
await Promise.all([...contexts.keys()].map(async context => {
|
2021-08-08 01:47:03 +03:00
|
|
|
const videos = context.pages().map(p => p.video()).filter(Boolean);
|
2021-10-27 18:28:53 +03:00
|
|
|
if (trace === 'on' && !contexts.get(context)!.closed) {
|
2021-09-14 04:07:40 +03:00
|
|
|
const tracePath = testInfo.outputPath('trace.zip');
|
|
|
|
await context.tracing.stop({ path: tracePath });
|
|
|
|
testInfo.attachments.push({ name: 'trace', path: tracePath, contentType: 'application/zip' });
|
|
|
|
}
|
2021-08-08 01:47:03 +03:00
|
|
|
await context.close();
|
|
|
|
for (const v of videos) {
|
2021-08-10 11:24:14 +03:00
|
|
|
const videoPath = await v.path().catch(() => null);
|
|
|
|
if (!videoPath)
|
|
|
|
continue;
|
2021-08-08 01:47:03 +03:00
|
|
|
const savedPath = testInfo.outputPath(path.basename(videoPath));
|
|
|
|
await v.saveAs(savedPath);
|
|
|
|
testInfo.attachments.push({ name: 'video', path: savedPath, contentType: 'video/webm' });
|
|
|
|
}
|
|
|
|
}));
|
2021-05-17 05:58:26 +03:00
|
|
|
},
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-10-08 00:01:08 +03:00
|
|
|
context: async ({ contextFactory, browser, _reuseBrowserContext, contextOptions }, run) => {
|
|
|
|
if (_reuseBrowserContext.isEnabled()) {
|
|
|
|
const context = await _reuseBrowserContext.obtainContext(browser, contextOptions);
|
|
|
|
await run(context);
|
|
|
|
return;
|
|
|
|
}
|
2021-05-17 05:58:26 +03:00
|
|
|
await run(await contextFactory());
|
|
|
|
},
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-10-08 00:01:08 +03:00
|
|
|
page: async ({ context, _reuseBrowserContext }, run) => {
|
|
|
|
if (_reuseBrowserContext.isEnabled()) {
|
|
|
|
await run(await _reuseBrowserContext.obtainPage());
|
|
|
|
return;
|
|
|
|
}
|
2021-05-17 05:58:26 +03:00
|
|
|
await run(await context.newPage());
|
|
|
|
},
|
2021-10-27 18:28:53 +03:00
|
|
|
|
|
|
|
browserName: [ 'chromium' , { scope: 'worker' } ],
|
|
|
|
headless: [ undefined, { scope: 'worker' } ],
|
|
|
|
channel: [ undefined, { scope: 'worker' } ],
|
|
|
|
video: [ 'off', { scope: 'worker' } ],
|
|
|
|
trace: [ 'off', { scope: 'worker' } ],
|
2021-05-17 05:58:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const test = baseTest.extend<PlaywrightTestOptions & PlaywrightTestFixtures, PlaywrightWorkerOptions & PlaywrightWorkerFixtures>(playwrightFixtures);
|
|
|
|
export const playwrightTest = test;
|
|
|
|
export const browserTest = test;
|
|
|
|
export const contextTest = test;
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-10-11 17:52:17 +03:00
|
|
|
export { expect } from '@playwright/test';
|