chore: dedupe ct fixtures (#14002)

This commit is contained in:
Pavel Feldman 2022-05-06 13:53:38 -08:00 committed by GitHub
parent 1bbefce9af
commit dc36b0158a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 87 deletions

View File

@ -15,7 +15,7 @@
*/
const { test: baseTest, expect, devices, _addRunnerPlugin } = require('@playwright/test');
const { mount } = require('@playwright/test/lib/mount');
const { fixtures } = require('@playwright/test/lib/mount');
const path = require('path');
_addRunnerPlugin(() => {
@ -26,30 +26,6 @@ _addRunnerPlugin(() => {
() => require('@vitejs/plugin-react')());
});
const test = baseTest.extend({
_workerPage: [async ({ browser }, use) => {
const page = await browser._wrapApiCall(async () => {
const page = await browser.newPage();
await page.addInitScript('navigator.serviceWorker.register = () => {}');
return page;
});
await use(page);
}, { scope: 'worker' }],
context: async ({ page }, use) => {
await use(page.context());
},
page: async ({ _workerPage }, use) => {
await use(_workerPage);
},
mount: async ({ page, baseURL, viewport }, use) => {
await use(async (component, options) => {
const selector = await mount(page, component, options, baseURL, viewport);
return page.locator(selector);
});
},
});
const test = baseTest.extend(fixtures);
module.exports = { test, expect, devices };

View File

@ -15,7 +15,7 @@
*/
const { test: baseTest, expect, devices, _addRunnerPlugin } = require('@playwright/test');
const { mount } = require('@playwright/test/lib/mount');
const { fixtures } = require('@playwright/test/lib/mount');
const path = require('path');
_addRunnerPlugin(() => {
@ -26,30 +26,6 @@ _addRunnerPlugin(() => {
() => require('@sveltejs/vite-plugin-svelte').svelte());
});
const test = baseTest.extend({
_workerPage: [async ({ browser }, use) => {
const page = await browser._wrapApiCall(async () => {
const page = await browser.newPage();
await page.addInitScript('navigator.serviceWorker.register = () => {}');
return page;
});
await use(page);
}, { scope: 'worker' }],
context: async ({ page }, use) => {
await use(page.context());
},
page: async ({ _workerPage }, use) => {
await use(_workerPage);
},
mount: async ({ page, baseURL, viewport }, use) => {
await use(async (component, options) => {
const selector = await mount(page, component, options, baseURL, viewport);
return page.locator(selector);
});
},
});
const test = baseTest.extend(fixtures);
module.exports = { test, expect, devices };

View File

@ -15,7 +15,7 @@
*/
const { test: baseTest, expect, devices, _addRunnerPlugin } = require('@playwright/test');
const { mount } = require('@playwright/test/lib/mount');
const { fixtures } = require('@playwright/test/lib/mount');
const path = require('path');
_addRunnerPlugin(() => {
@ -26,30 +26,6 @@ _addRunnerPlugin(() => {
() => require('@vitejs/plugin-vue')());
});
const test = baseTest.extend({
_workerPage: [async ({ browser }, use) => {
const page = await browser._wrapApiCall(async () => {
const page = await browser.newPage();
await page.addInitScript('navigator.serviceWorker.register = () => {}');
return page;
});
await use(page);
}, { scope: 'worker' }],
context: async ({ page }, use) => {
await use(page.context());
},
page: async ({ _workerPage }, use) => {
await use(_workerPage);
},
mount: async ({ page, baseURL, viewport }, use) => {
await use(async (component, options) => {
const selector = await mount(page, component, options, baseURL, viewport);
return page.locator(selector);
});
},
});
const test = baseTest.extend(fixtures);
module.exports = { test, expect, devices };

View File

@ -14,21 +14,43 @@
* limitations under the License.
*/
import type { Page, ViewportSize } from '@playwright/test';
import type { Fixtures, Locator, Page, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, ViewportSize } from './types';
import { createGuid } from 'playwright-core/lib/utils';
export async function mount(page: Page, jsxOrType: any, options: any, baseURL: string, viewport: ViewportSize): Promise<string> {
return await (page as any)._wrapApiCall(async () => {
return await innerMount(page, jsxOrType, options, baseURL, viewport);
}, true);
}
export const fixtures: Fixtures<PlaywrightTestArgs & PlaywrightTestOptions & { mount: (component: any, options: any) => Promise<Locator> }, PlaywrightWorkerArgs & { _workerPage: Page }> = {
_workerPage: [async ({ browser }, use) => {
const page = await (browser as any)._wrapApiCall(async () => {
const page = await browser.newPage();
await page.addInitScript('navigator.serviceWorker.register = () => {}');
return page;
});
await use(page);
}, { scope: 'worker' }],
async function innerMount(page: Page, jsxOrType: any, options: any, baseURL: string, viewport: ViewportSize): Promise<string> {
context: async ({ page }, use) => {
await use(page.context());
},
page: async ({ _workerPage }, use) => {
await use(_workerPage);
},
mount: async ({ page, viewport }, use) => {
await use(async (component, options) => {
const selector = await (page as any)._wrapApiCall(async () => {
return await innerMount(page, component, options, viewport || { width: 1280, height: 800 });
}, true);
return page.locator(selector);
});
},
};
async function innerMount(page: Page, jsxOrType: any, options: any, viewport: ViewportSize): Promise<string> {
await page.goto('about:blank');
await (page as any)._resetForReuse();
await (page.context() as any)._resetForReuse();
await page.setViewportSize(viewport);
await page.goto(baseURL);
await page.goto(process.env.PLAYWRIGHT_VITE_COMPONENTS_BASE_URL!);
let component;
if (typeof jsxOrType === 'string')

View File

@ -40,7 +40,7 @@ export function createPlugin(
configDir = configDirectory;
process.env.PLAYWRIGHT_TEST_BASE_URL = `http://localhost:${port}/playwright/index.html`;
process.env.PLAYWRIGHT_VITE_COMPONENTS_BASE_URL = `http://localhost:${port}/playwright/index.html`;
viteConfig.root = viteConfig.root || configDir;
viteConfig.plugins = viteConfig.plugins || [