|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { baseTest } from '../config/baseTest'; |
| 18 | +import { chromium } from 'playwright'; |
| 19 | +import { expect, type PageTestFixtures, type PageWorkerFixtures } from '../page/pageTestApi'; |
| 20 | +import type { TraceViewerFixtures } from '../config/traceViewerFixtures'; |
| 21 | +import { traceViewerFixtures } from '../config/traceViewerFixtures'; |
| 22 | +export { expect } from '@playwright/test'; |
| 23 | +import http from 'node:http'; |
| 24 | +import path from 'node:path'; |
| 25 | +import { AddressInfo } from 'node:net'; |
| 26 | + |
| 27 | +export const extensionTest = baseTest.extend<TraceViewerFixtures>(traceViewerFixtures).extend<PageTestFixtures, PageWorkerFixtures>({ |
| 28 | + browserVersion: [({ browser }) => browser.version(), { scope: 'worker' }], |
| 29 | + browserMajorVersion: [({ browserVersion }, use) => use(Number(browserVersion.split('.')[0])), { scope: 'worker' }], |
| 30 | + isAndroid: [false, { scope: 'worker' }], |
| 31 | + isElectron: [false, { scope: 'worker' }], |
| 32 | + electronMajorVersion: [0, { scope: 'worker' }], |
| 33 | + isWebView2: [false, { scope: 'worker' }], |
| 34 | + isHeadlessShell: [false, { scope: 'worker' }], |
| 35 | + |
| 36 | + browser: [async ({ playwright }, use, testInfo) => { |
| 37 | + const httpServer = http.createServer(); |
| 38 | + await new Promise<void>(resolve => httpServer.listen(0, resolve)); |
| 39 | + const pathToExtension = path.join(__dirname, '../../../playwright-mcp/extension'); |
| 40 | + const context = await chromium.launchPersistentContext('', { |
| 41 | + args: [ |
| 42 | + `--disable-extensions-except=${pathToExtension}`, |
| 43 | + `--load-extension=${pathToExtension}`, |
| 44 | + '--enable-features=AllowContentInitiatedDataUrlNavigations', |
| 45 | + ], |
| 46 | + channel: 'chromium', |
| 47 | + }); |
| 48 | + const { CDPBridgeServer } = await import('../../../playwright-mcp/src/cdp-relay.ts'); |
| 49 | + const server = new CDPBridgeServer(httpServer); |
| 50 | + const origin = `ws://localhost:${(httpServer.address() as AddressInfo).port}`; |
| 51 | + await expect.poll(() => context?.serviceWorkers()).toHaveLength(1); |
| 52 | + await context.pages()[0].goto(new URL('/popup.html', context.serviceWorkers()[0].url()).toString()); |
| 53 | + await context.pages()[0].getByRole('textbox', { name: 'Bridge Server URL:' }).clear(); |
| 54 | + await context.pages()[0].getByRole('textbox', { name: 'Bridge Server URL:' }).fill(`${origin}${server.EXTENSION_PATH}`); |
| 55 | + await context.pages()[0].getByRole('button', { name: 'Share This Tab' }).click(); |
| 56 | + await context.pages()[0].goto('about:blank'); |
| 57 | + const browser = await playwright.chromium.connectOverCDP(`${origin}${server.CDP_PATH}`); |
| 58 | + await use(browser); |
| 59 | + httpServer.close(); |
| 60 | + }, { scope: 'worker' }], |
| 61 | + |
| 62 | + context: async ({ browser }, use) => { |
| 63 | + await use(browser.contexts()[0]); |
| 64 | + }, |
| 65 | + |
| 66 | + page: async ({ browser }, use) => { |
| 67 | + await use(browser.contexts()[0].pages()[0]); |
| 68 | + } |
| 69 | +}); |
0 commit comments