2022-03-11 08:00:46 -08: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.
|
|
|
|
*/
|
|
|
|
|
2022-05-31 15:59:36 -07:00
|
|
|
import { normalizeTraceMode, normalizeVideoMode, shouldCaptureTrace, shouldCaptureVideo } from './index';
|
|
|
|
import type { Fixtures, Locator, Page, BrowserContextOptions, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions, BrowserContext } from './types';
|
2022-06-04 14:07:06 -07:00
|
|
|
import type { Component, JsxComponent, ObjectComponentOptions } from '../types/component';
|
2022-05-09 06:44:20 -08:00
|
|
|
|
|
|
|
let boundCallbacksForMount: Function[] = [];
|
2022-03-11 08:00:46 -08:00
|
|
|
|
2022-05-31 15:59:36 -07:00
|
|
|
export const fixtures: Fixtures<
|
|
|
|
PlaywrightTestArgs & PlaywrightTestOptions & { mount: (component: any, options: any) => Promise<Locator> },
|
2022-06-02 12:16:07 -07:00
|
|
|
PlaywrightWorkerArgs & PlaywrightWorkerOptions & { _ctWorker: { context: BrowserContext | undefined, hash: string } },
|
2022-05-31 15:59:36 -07:00
|
|
|
{ _contextFactory: (options?: BrowserContextOptions) => Promise<BrowserContext> }> = {
|
|
|
|
|
2022-06-02 12:16:07 -07:00
|
|
|
_ctWorker: [{ context: undefined, hash: '' }, { scope: 'worker' }],
|
2022-05-31 15:59:36 -07:00
|
|
|
|
2022-06-02 12:16:07 -07:00
|
|
|
context: async ({ playwright, browser, _ctWorker, _contextFactory, video, trace }, use, testInfo) => {
|
|
|
|
const isolateTests = shouldCaptureVideo(normalizeVideoMode(video), testInfo) || shouldCaptureTrace(normalizeTraceMode(trace), testInfo);
|
2022-05-31 15:59:36 -07:00
|
|
|
const defaultContextOptions = (playwright.chromium as any)._defaultContextOptions as BrowserContextOptions;
|
|
|
|
const hash = contextHash(defaultContextOptions);
|
|
|
|
|
2022-06-02 12:16:07 -07:00
|
|
|
if (!_ctWorker.context || _ctWorker.hash !== hash || isolateTests) {
|
2022-05-31 15:59:36 -07:00
|
|
|
if (_ctWorker.context)
|
|
|
|
await _ctWorker.context.close();
|
2022-06-02 12:16:07 -07:00
|
|
|
// Context factory sets up video so we want to use that for isolated contexts.
|
|
|
|
// However, it closes the context after the test, so we don't want to use it
|
|
|
|
// for shared contexts.
|
|
|
|
_ctWorker.context = isolateTests ? await _contextFactory() : await browser.newContext();
|
2022-05-31 15:59:36 -07:00
|
|
|
_ctWorker.hash = hash;
|
2022-06-02 12:16:07 -07:00
|
|
|
await _ctWorker.context.addInitScript('navigator.serviceWorker.register = () => {}');
|
|
|
|
await _ctWorker.context.exposeFunction('__pw_dispatch', (ordinal: number, args: any[]) => {
|
|
|
|
boundCallbacksForMount[ordinal](...args);
|
|
|
|
});
|
2022-05-31 15:59:36 -07:00
|
|
|
} else {
|
2022-06-02 12:16:07 -07:00
|
|
|
await (_ctWorker.context as any)._resetForReuse();
|
2022-05-31 15:59:36 -07:00
|
|
|
}
|
2022-06-02 12:16:07 -07:00
|
|
|
await use(_ctWorker.context);
|
2022-05-31 15:59:36 -07:00
|
|
|
},
|
|
|
|
|
2022-06-02 12:16:07 -07:00
|
|
|
page: async ({ context, viewport }, use) => {
|
|
|
|
let page = context.pages()[0];
|
|
|
|
await (context as any)._wrapApiCall(async () => {
|
|
|
|
if (!page) {
|
|
|
|
page = await context.newPage();
|
|
|
|
} else {
|
|
|
|
await (page as any)._resetForReuse();
|
|
|
|
await page.goto('about:blank');
|
|
|
|
await page.setViewportSize(viewport || { width: 1280, height: 800 });
|
|
|
|
}
|
|
|
|
await page.goto(process.env.PLAYWRIGHT_VITE_COMPONENTS_BASE_URL!);
|
|
|
|
}, true);
|
|
|
|
await use(page);
|
2022-05-31 15:59:36 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
mount: async ({ page }, use) => {
|
2022-06-04 14:07:06 -07:00
|
|
|
await use(async (component: JsxComponent | string, options?: ObjectComponentOptions) => {
|
2022-05-31 15:59:36 -07:00
|
|
|
const selector = await (page as any)._wrapApiCall(async () => {
|
|
|
|
return await innerMount(page, component, options);
|
|
|
|
}, true);
|
|
|
|
return page.locator(selector);
|
|
|
|
});
|
|
|
|
boundCallbacksForMount = [];
|
|
|
|
},
|
|
|
|
};
|
2022-05-06 13:53:38 -08:00
|
|
|
|
2022-06-04 14:07:06 -07:00
|
|
|
async function innerMount(page: Page, jsxOrType: JsxComponent | string, options?: ObjectComponentOptions): Promise<string> {
|
|
|
|
let component: Component;
|
2022-03-11 08:00:46 -08:00
|
|
|
if (typeof jsxOrType === 'string')
|
|
|
|
component = { kind: 'object', type: jsxOrType, options };
|
|
|
|
else
|
|
|
|
component = jsxOrType;
|
|
|
|
|
2022-05-09 06:44:20 -08:00
|
|
|
wrapFunctions(component, page, boundCallbacksForMount);
|
2022-03-11 08:00:46 -08:00
|
|
|
|
2022-03-11 15:46:11 -08:00
|
|
|
// WebKit does not wait for deferred scripts.
|
2022-06-04 14:07:06 -07:00
|
|
|
await page.waitForFunction(() => !!window.playwrightMount);
|
2022-03-11 15:46:11 -08:00
|
|
|
|
2022-05-09 06:44:20 -08:00
|
|
|
const selector = await page.evaluate(async ({ component }) => {
|
2022-03-11 08:00:46 -08:00
|
|
|
const unwrapFunctions = (object: any) => {
|
|
|
|
for (const [key, value] of Object.entries(object)) {
|
|
|
|
if (typeof value === 'string' && (value as string).startsWith('__pw_func_')) {
|
|
|
|
const ordinal = +value.substring('__pw_func_'.length);
|
|
|
|
object[key] = (...args: any[]) => {
|
2022-05-09 06:44:20 -08:00
|
|
|
(window as any)['__pw_dispatch'](ordinal, args);
|
2022-03-11 08:00:46 -08:00
|
|
|
};
|
|
|
|
} else if (typeof value === 'object' && value) {
|
|
|
|
unwrapFunctions(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
unwrapFunctions(component);
|
2022-06-04 14:07:06 -07:00
|
|
|
let rootElement = document.getElementById('root');
|
|
|
|
if (!rootElement) {
|
|
|
|
rootElement = document.createElement('div');
|
|
|
|
rootElement.id = 'root';
|
|
|
|
document.body.appendChild(rootElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
window.playwrightMount(component, rootElement);
|
|
|
|
|
|
|
|
// When mounting fragments, return selector pointing to the root element.
|
|
|
|
return rootElement.childNodes.length > 1 ? '#root' : '#root > *';
|
2022-05-09 06:44:20 -08:00
|
|
|
}, { component });
|
2022-03-11 08:00:46 -08:00
|
|
|
return selector;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapFunctions(object: any, page: Page, callbacks: Function[]) {
|
|
|
|
for (const [key, value] of Object.entries(object)) {
|
|
|
|
const type = typeof value;
|
|
|
|
if (type === 'function') {
|
|
|
|
const functionName = '__pw_func_' + callbacks.length;
|
|
|
|
callbacks.push(value as Function);
|
|
|
|
object[key] = functionName;
|
|
|
|
} else if (type === 'object' && value) {
|
|
|
|
wrapFunctions(value, page, callbacks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-18 09:57:05 -07:00
|
|
|
|
|
|
|
function contextHash(context: BrowserContextOptions): string {
|
|
|
|
const hash = {
|
|
|
|
acceptDownloads: context.acceptDownloads,
|
|
|
|
bypassCSP: context.bypassCSP,
|
|
|
|
colorScheme: context.colorScheme,
|
|
|
|
extraHTTPHeaders: context.extraHTTPHeaders,
|
|
|
|
forcedColors: context.forcedColors,
|
|
|
|
geolocation: context.geolocation,
|
|
|
|
hasTouch: context.hasTouch,
|
|
|
|
httpCredentials: context.httpCredentials,
|
|
|
|
ignoreHTTPSErrors: context.ignoreHTTPSErrors,
|
|
|
|
isMobile: context.isMobile,
|
|
|
|
javaScriptEnabled: context.javaScriptEnabled,
|
|
|
|
locale: context.locale,
|
|
|
|
offline: context.offline,
|
|
|
|
permissions: context.permissions,
|
|
|
|
proxy: context.proxy,
|
|
|
|
storageState: context.storageState,
|
|
|
|
timezoneId: context.timezoneId,
|
|
|
|
userAgent: context.userAgent,
|
|
|
|
deviceScaleFactor: context.deviceScaleFactor,
|
|
|
|
};
|
|
|
|
return JSON.stringify(hash);
|
|
|
|
}
|