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-04-29 21:11:32 +03:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as os from 'os';
|
2021-10-28 18:31:30 +03:00
|
|
|
import { PageTestFixtures, PageWorkerFixtures } from '../page/pageTestApi';
|
|
|
|
import * as path from 'path';
|
|
|
|
import type { BrowserContext, BrowserContextOptions, BrowserType, Page } from 'playwright-core';
|
2022-01-27 05:28:42 +03:00
|
|
|
import { removeFolders } from '../../packages/playwright-core/lib/utils/utils';
|
2021-10-27 18:28:53 +03:00
|
|
|
import { baseTest } from './baseTest';
|
2021-10-28 18:31:30 +03:00
|
|
|
import { RemoteServer, RemoteServerOptions } from './remoteServer';
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-10-28 18:31:30 +03:00
|
|
|
export type BrowserTestWorkerFixtures = PageWorkerFixtures & {
|
2021-05-17 05:58:26 +03:00
|
|
|
browserVersion: string;
|
2022-03-01 12:32:40 +03:00
|
|
|
defaultSameSiteCookieValue: string;
|
2021-10-28 18:31:30 +03:00
|
|
|
browserMajorVersion: number;
|
|
|
|
browserType: BrowserType;
|
|
|
|
isAndroid: boolean;
|
|
|
|
isElectron: boolean;
|
2021-04-29 21:11:32 +03:00
|
|
|
};
|
2021-10-26 23:45:53 +03:00
|
|
|
|
2021-10-28 18:31:30 +03:00
|
|
|
type BrowserTestTestFixtures = PageTestFixtures & {
|
2021-05-17 05:58:26 +03:00
|
|
|
createUserDataDir: () => Promise<string>;
|
|
|
|
launchPersistent: (options?: Parameters<BrowserType['launchPersistentContext']>[1]) => Promise<{ context: BrowserContext, page: Page }>;
|
|
|
|
startRemoteServer: (options?: RemoteServerOptions) => Promise<RemoteServer>;
|
|
|
|
contextFactory: (options?: BrowserContextOptions) => Promise<BrowserContext>;
|
|
|
|
};
|
|
|
|
|
2021-10-28 18:31:30 +03:00
|
|
|
const test = baseTest.extend<BrowserTestTestFixtures, BrowserTestWorkerFixtures>({
|
|
|
|
browserVersion: [async ({ browser }, run) => {
|
|
|
|
await run(browser.version());
|
2021-10-27 18:28:53 +03:00
|
|
|
}, { scope: 'worker' } ],
|
|
|
|
|
2022-01-28 21:51:55 +03:00
|
|
|
browserType: [async ({ playwright, browserName }, run) => {
|
|
|
|
await run(playwright[browserName]);
|
2021-10-28 18:31:30 +03:00
|
|
|
}, { scope: 'worker' } ],
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2022-03-01 11:17:33 +03:00
|
|
|
defaultSameSiteCookieValue: [async ({ browserName, browserMajorVersion }, run) => {
|
2022-03-08 03:16:42 +03:00
|
|
|
await run(browserName === 'chromium' || (browserName === 'firefox' && browserMajorVersion >= 96 && browserMajorVersion < 97) ? 'Lax' : 'None');
|
2022-03-01 11:17:33 +03:00
|
|
|
}, { scope: 'worker' } ],
|
|
|
|
|
2021-10-28 18:31:30 +03:00
|
|
|
browserMajorVersion: [async ({ browserVersion }, run) => {
|
|
|
|
await run(Number(browserVersion.split('.')[0]));
|
2021-05-17 05:58:26 +03:00
|
|
|
}, { scope: 'worker' } ],
|
2021-04-29 21:11:32 +03:00
|
|
|
|
2021-10-28 18:31:30 +03:00
|
|
|
isAndroid: [false, { scope: 'worker' } ],
|
|
|
|
isElectron: [false, { scope: 'worker' } ],
|
|
|
|
|
|
|
|
contextFactory: async ({ _contextFactory }: any, run) => {
|
|
|
|
await run(_contextFactory);
|
|
|
|
},
|
2021-10-08 00:01:08 +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);
|
|
|
|
},
|
|
|
|
|
2021-10-28 05:00:06 +03:00
|
|
|
launchPersistent: async ({ createUserDataDir, browserType }, run) => {
|
2021-05-17 05:58:26 +03:00
|
|
|
let persistentContext: BrowserContext | undefined;
|
|
|
|
await run(async options => {
|
|
|
|
if (persistentContext)
|
2021-10-28 18:31:30 +03:00
|
|
|
throw new Error('can only launch one persistent context');
|
2021-05-17 05:58:26 +03:00
|
|
|
const userDataDir = await createUserDataDir();
|
2021-10-28 05:00:06 +03:00
|
|
|
persistentContext = await browserType.launchPersistentContext(userDataDir, { ...options });
|
2021-05-17 05:58:26 +03:00
|
|
|
const page = persistentContext.pages()[0];
|
|
|
|
return { context: persistentContext, page };
|
|
|
|
});
|
|
|
|
if (persistentContext)
|
|
|
|
await persistentContext.close();
|
|
|
|
},
|
|
|
|
|
2021-10-28 05:00:06 +03:00
|
|
|
startRemoteServer: async ({ childProcess, browserType }, 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-10-28 05:00:06 +03:00
|
|
|
await remoteServer._start(childProcess, browserType, options);
|
2021-05-17 05:58:26 +03:00
|
|
|
return remoteServer;
|
|
|
|
});
|
2022-02-10 04:00:33 +03:00
|
|
|
if (remoteServer) {
|
2021-05-17 05:58:26 +03:00
|
|
|
await remoteServer.close();
|
2022-02-10 04:00:33 +03:00
|
|
|
// Give any connected browsers a chance to disconnect to avoid
|
|
|
|
// poisoning next test with quasy-alive browsers.
|
|
|
|
await new Promise(f => setTimeout(f, 1000));
|
|
|
|
}
|
2021-05-17 05:58:26 +03:00
|
|
|
},
|
2021-10-28 18:31:30 +03:00
|
|
|
});
|
2021-05-17 05:58:26 +03:00
|
|
|
|
|
|
|
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';
|