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-06-07 03:09:53 +03:00
|
|
|
import type { Fixtures } from './test-runner';
|
2021-04-29 21:11:32 +03:00
|
|
|
import type { Browser, BrowserContext, BrowserContextOptions, BrowserType, LaunchOptions, Page } from '../../index';
|
|
|
|
import { removeFolders } from '../../lib/utils/utils';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as os from 'os';
|
|
|
|
import { RemoteServer, RemoteServerOptions } from './remoteServer';
|
2021-05-17 05:58:26 +03:00
|
|
|
import { baseTest, CommonWorkerFixtures } from './baseTest';
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-05-17 05:58:26 +03:00
|
|
|
type PlaywrightWorkerOptions = {
|
2021-06-03 08:00:34 +03:00
|
|
|
tracesDir: LaunchOptions['tracesDir'];
|
2021-05-17 05:58:26 +03:00
|
|
|
executablePath: LaunchOptions['executablePath'];
|
|
|
|
proxy: LaunchOptions['proxy'];
|
|
|
|
args: LaunchOptions['args'];
|
2021-04-29 21:11:32 +03:00
|
|
|
};
|
2021-05-17 05:58:26 +03:00
|
|
|
export type PlaywrightWorkerFixtures = {
|
2021-04-29 21:11:32 +03:00
|
|
|
browserType: BrowserType;
|
|
|
|
browserOptions: LaunchOptions;
|
2021-05-17 05:58:26 +03:00
|
|
|
browser: Browser;
|
|
|
|
browserVersion: string;
|
|
|
|
};
|
|
|
|
type PlaywrightTestOptions = {
|
|
|
|
hasTouch: BrowserContextOptions['hasTouch'];
|
2021-04-29 21:11:32 +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;
|
|
|
|
};
|
|
|
|
export type PlaywrightOptions = PlaywrightWorkerOptions & PlaywrightTestOptions;
|
|
|
|
|
2021-06-07 03:09:53 +03:00
|
|
|
export const playwrightFixtures: Fixtures<PlaywrightTestOptions & PlaywrightTestFixtures, PlaywrightWorkerOptions & PlaywrightWorkerFixtures, {}, CommonWorkerFixtures> = {
|
2021-06-03 08:00:34 +03:00
|
|
|
tracesDir: [ undefined, { scope: 'worker' } ],
|
2021-05-17 05:58:26 +03:00
|
|
|
executablePath: [ undefined, { scope: 'worker' } ],
|
|
|
|
proxy: [ undefined, { scope: 'worker' } ],
|
|
|
|
args: [ undefined, { scope: 'worker' } ],
|
|
|
|
hasTouch: undefined,
|
|
|
|
|
|
|
|
browserType: [async ({ playwright, browserName }, run) => {
|
|
|
|
await run(playwright[browserName]);
|
|
|
|
}, { scope: 'worker' } ],
|
|
|
|
|
2021-06-03 08:00:34 +03:00
|
|
|
browserOptions: [async ({ headless, channel, executablePath, tracesDir, proxy, args }, run) => {
|
2021-05-17 05:58:26 +03:00
|
|
|
await run({
|
|
|
|
headless,
|
|
|
|
channel,
|
|
|
|
executablePath,
|
2021-06-03 08:00:34 +03:00
|
|
|
tracesDir,
|
2021-05-17 05:58:26 +03:00
|
|
|
proxy,
|
|
|
|
args,
|
|
|
|
handleSIGINT: false,
|
|
|
|
});
|
|
|
|
}, { scope: 'worker' } ],
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-05-17 05:58:26 +03:00
|
|
|
browser: [async ({ browserType, browserOptions }, run) => {
|
|
|
|
const browser = await browserType.launch(browserOptions);
|
|
|
|
await run(browser);
|
|
|
|
await browser.close();
|
|
|
|
}, { 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-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();
|
|
|
|
},
|
|
|
|
|
|
|
|
startRemoteServer: async ({ browserType, browserOptions }, run) => {
|
|
|
|
let remoteServer: RemoteServer | undefined;
|
|
|
|
await run(async options => {
|
|
|
|
if (remoteServer)
|
|
|
|
throw new Error('can only start one remote server');
|
|
|
|
remoteServer = new RemoteServer();
|
|
|
|
await remoteServer._start(browserType, browserOptions, options);
|
|
|
|
return remoteServer;
|
|
|
|
});
|
|
|
|
if (remoteServer)
|
|
|
|
await remoteServer.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
contextOptions: async ({ video, hasTouch, browserVersion }, 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-05-17 05:58:26 +03:00
|
|
|
recordVideo: video ? { 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-03 03:17:20 +03:00
|
|
|
contextFactory: async ({ browser, contextOptions }, run, testInfo) => {
|
2021-05-17 05:58:26 +03:00
|
|
|
const contexts: BrowserContext[] = [];
|
|
|
|
await run(async options => {
|
|
|
|
const context = await browser.newContext({ ...contextOptions, ...options });
|
|
|
|
contexts.push(context);
|
|
|
|
return context;
|
|
|
|
});
|
|
|
|
await Promise.all(contexts.map(context => context.close()));
|
|
|
|
},
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-05-17 05:58:26 +03:00
|
|
|
context: async ({ contextFactory }, run) => {
|
|
|
|
await run(await contextFactory());
|
|
|
|
},
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-05-17 05:58:26 +03:00
|
|
|
page: async ({ context }, run) => {
|
|
|
|
await run(await context.newPage());
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
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-06-07 03:09:53 +03:00
|
|
|
export { expect } from './test-runner';
|