|
| 1 | +import {ABSOLUTE_PATH_TO_PROJECT_ROOT_DIRECTORY, e2edEnvironment} from '../../constants/internal'; |
| 2 | +import {getPlaywrightPage} from '../../useContext'; |
| 3 | + |
| 4 | +import {generalLog} from '../generalLog'; |
| 5 | + |
| 6 | +import type {RunId} from '../../types/internal'; |
| 7 | + |
| 8 | +type Options = Readonly<{name: string; runId: RunId}>; |
| 9 | + |
| 10 | +/** |
| 11 | + * Binds browser of completed test for exploration (in explore mode), |
| 12 | + * and waits until the test page is closed or the browser is disconnected |
| 13 | + * (or e2ed is stopped by a signal). |
| 14 | + * @internal |
| 15 | + */ |
| 16 | +export const bindBrowserForExploration = async ({name, runId}: Options): Promise<void> => { |
| 17 | + const page = getPlaywrightPage(); |
| 18 | + const browser = page.context().browser(); |
| 19 | + |
| 20 | + if (browser === null) { |
| 21 | + generalLog(`Cannot bind browser of test "${name}" for exploration: there is no browser`, { |
| 22 | + runId, |
| 23 | + }); |
| 24 | + |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + const bindName = `${e2edEnvironment.E2ED_EXPLORE}-${runId}`; |
| 29 | + const {endpoint} = await browser.bind(bindName, { |
| 30 | + workspaceDir: ABSOLUTE_PATH_TO_PROJECT_ROOT_DIRECTORY, |
| 31 | + }); |
| 32 | + |
| 33 | + generalLog( |
| 34 | + `Browser of test "${name}" is bound as "${bindName}" for exploration (attach to it with "playwright-cli attach ${bindName}"); the test will end when its page is closed (for example, by "playwright-cli tab-close") or e2ed is stopped`, |
| 35 | + {endpoint, runId}, |
| 36 | + ); |
| 37 | + |
| 38 | + await new Promise<void>((resolve) => { |
| 39 | + browser.once('disconnected', () => { |
| 40 | + resolve(); |
| 41 | + }); |
| 42 | + page.once('close', () => { |
| 43 | + resolve(); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + generalLog(`Exploration of test "${name}" in browser bound as "${bindName}" is completed`, { |
| 48 | + runId, |
| 49 | + }); |
| 50 | +}; |
0 commit comments