Skip to content

Commit d2e499e

Browse files
committed
feat: add E2ED_EXPLORE mode for exploration
fix: e2ed process exit
1 parent 0339ad7 commit d2e499e

17 files changed

Lines changed: 175 additions & 15 deletions

‎README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,13 @@ You can pass the following optional environment variables to the `e2ed` process
444444
`E2ED_DEBUG`: run `e2ed` in debug mode if this variable is not empty. If this variable has the form `inspect-brk:9229`,
445445
then `nodejs` debugging is started on port `9229` (via `--inspect-brk=9229`).
446446

447+
`E2ED_EXPLORE`: run `e2ed` in explore mode if this variable is not empty (for local run only).
448+
In this mode, timeouts are disabled (as in debug mode, but without debug mode itself), and after each test
449+
its browser remains open and is bound under the name `<E2ED_EXPLORE>-<runId>` (via `browser.bind`),
450+
so you can connect to it, for example, with `playwright-cli attach <name>`.
451+
The test ends when its page is closed (for example, by `playwright-cli tab-close`),
452+
or `e2ed` is stopped by a signal (`SIGINT`, `SIGTERM`, etc.).
453+
447454
`E2ED_TERMINATION_SIGNAL`: the termination signal received by the `e2ed` process (if any).
448455
Typically this value is `'SIGUSR1'`.
449456

‎autotests/tests/exploreMode.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {test} from 'autotests';
2+
import {Search} from 'autotests/pageObjects/pages';
3+
import {CreateProduct as CreateProductRoute} from 'autotests/routes/apiRoutes';
4+
import {expect} from 'e2ed';
5+
import {mockApiRoute, navigateToPage} from 'e2ed/actions';
6+
7+
import type {DeviceId} from 'autotests/types';
8+
9+
test(
10+
'Test with API mock and opened page (in explore mode its browser remains open)',
11+
{meta: {testId: '38'}},
12+
async () => {
13+
await mockApiRoute(CreateProductRoute, (routeParams, {method, query, requestBody, url}) => {
14+
const responseBody = {
15+
id: routeParams.id,
16+
method,
17+
output: 'mocked in explore mode',
18+
payload: {id: String(routeParams.id) as DeviceId, ...requestBody},
19+
query,
20+
url,
21+
};
22+
23+
return {responseBody};
24+
});
25+
26+
const searchPage = await navigateToPage(Search, {searchQuery: 'explore'});
27+
28+
await expect(
29+
searchPage.searchInput.input,
30+
'search input is in viewport after page load',
31+
).toBeInViewport();
32+
},
33+
);

‎src/bin/localEntrypoint.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ const execArgv = DEBUG_PORT !== undefined ? [`--inspect-brk=${DEBUG_PORT}`] : []
1313

1414
const e2edProcess = fork(entrypoitFilePath, process.argv.slice(2), {execArgv, stdio: 'inherit'});
1515

16+
const forwardSignal = (signal: NodeJS.Signals): void => {
17+
e2edProcess.kill(signal);
18+
};
19+
20+
process.on('SIGHUP', forwardSignal);
21+
process.on('SIGINT', forwardSignal);
22+
process.on('SIGTERM', forwardSignal);
23+
process.on('SIGUSR1', forwardSignal);
24+
1625
e2edProcess.on('exit', (code) => {
1726
process.on('exit', () => {
1827
process.exit(code);

‎src/config.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import {
99
ABSOLUTE_PATH_TO_INSTALLED_E2ED_DIRECTORY,
1010
ABSOLUTE_PATH_TO_PROJECT_ROOT_DIRECTORY,
1111
AFTER_ERROR_IN_TEST_TIMEOUT_IN_MS,
12+
ARE_TIMEOUTS_DISABLED,
1213
COMPILED_USERLAND_CONFIG_DIRECTORY,
1314
e2edEnvironment,
1415
EXPECTED_SCREENSHOTS_DIRECTORY_PATH,
1516
INTERNAL_REPORTS_DIRECTORY_PATH,
16-
IS_DEBUG,
1717
MAX_TIMEOUT_IN_MS,
1818
PATH_TO_TEST_FILE_VARIABLE_NAME,
1919
TESTS_DIRECTORY_PATH,
@@ -91,7 +91,7 @@ setCustomInspectOnFunction(mapLogPayloadInConsole);
9191
setCustomInspectOnFunction(mapLogPayloadInLogFile);
9292
setCustomInspectOnFunction(mapLogPayloadInReport);
9393

94-
if (IS_DEBUG || isUiMode) {
94+
if (ARE_TIMEOUTS_DISABLED || isUiMode) {
9595
setReadonlyProperty(userlandPack, 'packTimeout', MAX_TIMEOUT_IN_MS);
9696
setReadonlyProperty(userlandPack, 'testIdleTimeout', MAX_TIMEOUT_IN_MS);
9797
setReadonlyProperty(userlandPack, 'testTimeout', MAX_TIMEOUT_IN_MS);

‎src/constants/environment.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ export const DEBUG_PORT: number | undefined = e2edEnvironment.E2ED_DEBUG?.starts
1818
*/
1919
export const IS_DEBUG: boolean = Boolean(e2edEnvironment.E2ED_DEBUG);
2020

21+
/**
22+
* `true` if e2ed run in explore mode (`E2ED_EXPLORE` is not empty), and `false` otherwise.
23+
* In this mode, timeouts are disabled, and after each test its browser is bound
24+
* (for connecting to it by name) and remains open.
25+
*/
26+
export const IS_EXPLORE: boolean = Boolean(e2edEnvironment.E2ED_EXPLORE);
27+
28+
/**
29+
* `true` if timeouts are disabled (in debug mode or in explore mode), and `false` otherwise.
30+
*/
31+
export const ARE_TIMEOUTS_DISABLED: boolean = IS_DEBUG || IS_EXPLORE;
32+
2133
/**
2234
* Name of e2ed environment variable with path to pack.
2335
* @internal

‎src/constants/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export {EndE2edReason, ExitCode} from './end';
2-
export {DEBUG_PORT, IS_DEBUG} from './environment';
2+
export {ARE_TIMEOUTS_DISABLED, DEBUG_PORT, IS_DEBUG, IS_EXPLORE} from './environment';
33
export {READ_FILE_OPTIONS} from './fs';
44
export {
55
BAD_REQUEST_STATUS_CODE,

‎src/constants/internal.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ export {ConsoleBackgroundColor} from './color';
77
/** @internal */
88
export {MAX_TIMEOUT_IN_MS} from './debug';
99
export {EndE2edReason, ExitCode} from './end';
10-
export {DEBUG_PORT, IS_DEBUG, RunEnvironment} from './environment';
10+
export {
11+
ARE_TIMEOUTS_DISABLED,
12+
DEBUG_PORT,
13+
IS_DEBUG,
14+
IS_EXPLORE,
15+
RunEnvironment,
16+
} from './environment';
1117
/** @internal */
1218
export {
1319
e2edEnvironment,

‎src/types/environment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {RunLabel} from './runLabel';
1717
export type E2edEnvironment = {
1818
[key: string]: string | undefined;
1919
['E2ED_DEBUG']?: string;
20+
['E2ED_EXPLORE']?: string;
2021
['E2ED_ORIGIN']?: string;
2122
['E2ED_TERMINATION_SIGNAL']?: NodeJS.Signals;
2223
[PATH_TO_PACK_VARIABLE_NAME]?: string;

‎src/utils/end/endE2ed.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* eslint-disable no-console */
22

3+
import {type EndE2edReason, IS_EXPLORE} from '../../constants/internal';
4+
35
import {generalLog} from '../generalLog';
4-
import {testsSubprocess} from '../tests';
6+
import {playwrightSubprocess, testsSubprocess} from '../tests';
57

68
import {endE2edReason, setEndE2edReason} from './endReason';
79

8-
import type {EndE2edReason} from '../../constants/internal';
9-
1010
/**
1111
* Ends e2ed run by end e2ed reason (kill tests subprocess and exit).
1212
* @internal
@@ -39,4 +39,15 @@ export const endE2ed = (definedEndE2edReason: EndE2edReason): void => {
3939

4040
testsSubprocess.kill();
4141
}
42+
43+
if (
44+
playwrightSubprocess !== undefined &&
45+
playwrightSubprocess.exitCode === null &&
46+
playwrightSubprocess.signalCode === null
47+
) {
48+
console.log(`Kill Playwright subprocess (reason: ${definedEndE2edReason})`);
49+
50+
// In explore mode tests wait for their bound browsers, so only `SIGINT` interrupts them.
51+
playwrightSubprocess.kill(IS_EXPLORE ? 'SIGINT' : 'SIGTERM');
52+
}
4253
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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

Comments
 (0)