80 lines
2.6 KiB
TypeScript
Raw Normal View History

/**
* 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.
*/
import * as path from 'path';
import { test as baseTest, Locator } from '@playwright/test';
2022-02-16 15:45:35 -08:00
type Component = {
type: string;
props: Object;
children: Object[];
};
declare global {
interface Window {
2022-02-16 15:45:35 -08:00
__playwright_render: (component: Component) => void;
}
}
type TestFixtures = {
2022-02-16 15:45:35 -08:00
mount: (component: any) => Promise<Locator>;
webpack: string;
};
export const test = baseTest.extend<TestFixtures>({
webpack: '',
2022-02-16 15:45:35 -08:00
mount: async ({ page, webpack }, use) => {
const webpackConfig = require(webpack);
const outputPath = webpackConfig.output.path;
const filename = webpackConfig.output.filename.replace('[name]', 'playwright');
2022-02-16 15:45:35 -08:00
await use(async (component: Component) => {
await page.route('http://component/index.html', route => {
route.fulfill({
body: `<html>
2022-02-16 15:45:35 -08:00
<meta name='color-scheme' content='dark light'>
<style>html, body { padding: 0; margin: 0; background: #aaa; }</style>
<div id='root' style='width: 100%; height: 100%;'></div>
</html>`,
contentType: 'text/html'
});
});
await page.goto('http://component/index.html');
await page.addScriptTag({ path: path.resolve(__dirname, outputPath, filename) });
2021-12-14 19:25:07 -08:00
const props = { ...component.props };
for (const [key, value] of Object.entries(props)) {
if (typeof value === 'function') {
const functionName = '__pw_func_' + key;
await page.exposeFunction(functionName, value);
2021-12-14 19:25:07 -08:00
(props as any)[key] = functionName;
}
}
await page.evaluate(v => {
2021-12-14 19:25:07 -08:00
const props = v.props;
for (const [key, value] of Object.entries(props)) {
if (typeof value === 'string' && (value as string).startsWith('__pw_func_'))
2021-12-14 19:25:07 -08:00
(props as any)[key] = (window as any)[value];
}
2022-02-16 15:45:35 -08:00
window.__playwright_render({ ...v, props });
}, { ...component, props });
return page.locator('#pw-root');
});
},
});
export { expect } from '@playwright/test';