Skip to content

Commit 995e435

Browse files
committed
E2E-275 fix: renew last log event time on start of each test
1 parent 08f3af5 commit 995e435

12 files changed

Lines changed: 43 additions & 17 deletions

autotests/tests/switchingPagesForRequests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ test(
5757
await click(reportPage.header);
5858
});
5959

60-
switchToTab(npmPageTab);
60+
await switchToTab(npmPageTab);
6161

6262
await waitForTimeout(maxNumberOfRequests * 333 + 1_000);
6363

autotests/tests/switchingPagesForResponses.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ test(
5757
await click(reportPage.header);
5858
});
5959

60-
switchToTab(npmPageTab);
60+
await switchToTab(npmPageTab);
6161

6262
await waitForTimeout(maxNumberOfRequests * 333 + 1_000);
6363

autotests/tests/waitForAllRequestsComplete.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test(
1616

1717
let waitedInMs = Date.now() - startRequestInMs;
1818

19-
if (waitedInMs < 300 || waitedInMs > 400) {
19+
if (waitedInMs < 250 || waitedInMs > 450) {
2020
throw new E2edError(
2121
'waitForAllRequestsComplete did not wait for maxIntervalBetweenRequestsInMs in the beginning',
2222
{waitedInMs},

src/actions/switchToMainTab.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import {switchPlaywrightPage} from '../utils/playwrightPage';
77
/**
88
* Switches page context to the specified tab.
99
*/
10-
export const switchToMainTab = (): void => {
10+
export const switchToMainTab = async (): Promise<void> => {
1111
clearTab();
1212

1313
const page = getPlaywrightPage();
1414
const url = page.url();
1515

1616
log(`Switch page context to the main tab at ${url}`, LogEventType.InternalAction);
1717

18-
switchPlaywrightPage(page);
18+
await switchPlaywrightPage(page);
1919
};

src/actions/switchToTab.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import type {InternalTab, Tab} from '../types/internal';
88
/**
99
* Switches page context to the specified tab.
1010
*/
11-
export const switchToTab = (tab: Tab): void => {
11+
export const switchToTab = async (tab: Tab): Promise<void> => {
1212
const {page} = tab as InternalTab;
1313
const url = page.url();
1414

1515
log(`Switch page context to the specified tab at ${url}`, LogEventType.InternalAction);
1616

1717
setTab(tab);
1818

19-
switchPlaywrightPage(page);
19+
await switchPlaywrightPage(page);
2020
};

src/context/clearPage.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {useContext} from '../useContext';
2+
3+
/**
4+
* Get and set internal (maybe `undefined`) `clearPage` function.
5+
* @internal
6+
*/
7+
export const [getClearPage, setClearPage] = useContext<() => Promise<void>>();

src/utils/playwrightPage/switchPlaywrightPage.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {getClearPage} from '../../context/clearPage';
2+
3+
import {preparePage} from '../test';
4+
15
import {pageWaitForRequest, waitForRequestCalls} from './waitForRequest';
26
import {pageWaitForResponse, waitForResponseCalls} from './waitForResponse';
37

@@ -8,7 +12,13 @@ import type {Page} from '@playwright/test';
812
* Removes all event handlers from the old page and moves them to the new one.
913
* @internal
1014
*/
11-
export const switchPlaywrightPage = (newPage: Page): void => {
15+
export const switchPlaywrightPage = async (newPage: Page): Promise<void> => {
16+
const clearPage = getClearPage();
17+
18+
await clearPage?.();
19+
20+
await preparePage(newPage);
21+
1222
const oldRequestCalls = [...waitForRequestCalls];
1323

1424
waitForRequestCalls.length = 0;

src/utils/test/afterTest.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
import {getClearPage} from '../../context/clearPage';
2+
13
import {registerEndTestRunEvent} from '../events';
24
import {generalLog, writeLogsToFile} from '../generalLog';
35

46
import type {EndTestRunEvent, UtcTimeInMs} from '../../types/internal';
57

6-
type Options = Readonly<{clearPage: (() => Promise<void>) | undefined}> &
7-
Omit<EndTestRunEvent, 'utcTimeInMs'>;
8+
type Options = Omit<EndTestRunEvent, 'utcTimeInMs'>;
89

910
/**
1011
* Internal after test hook.
1112
* @internal
1213
*/
1314
export const afterTest = async (options: Options): Promise<void> => {
14-
const {clearPage, ...optionsWithoutClearPage} = options;
1515
const utcTimeInMs = Date.now() as UtcTimeInMs;
16-
const endTestRunEvent: EndTestRunEvent = {...optionsWithoutClearPage, utcTimeInMs};
16+
const endTestRunEvent: EndTestRunEvent = {...options, utcTimeInMs};
1717

1818
try {
19+
const clearPage = getClearPage();
20+
1921
await clearPage?.();
2022

2123
await registerEndTestRunEvent(endTestRunEvent);

src/utils/test/beforeTest.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {addResponseToApiStatistics} from '../apiStatistics';
1212
import {getFullPackConfig} from '../config';
1313
import {getRunLabel} from '../environment';
1414
import {registerStartTestRunEvent} from '../events';
15+
import {writeLogEventTime} from '../fs';
1516
import {mapBackendResponseForLogs} from '../log';
1617
import {isUiMode} from '../uiMode';
1718
import {getUserlandHooks} from '../userland';
@@ -97,4 +98,6 @@ export const beforeTest = ({
9798
};
9899

99100
registerStartTestRunEvent(testRunEvent);
101+
102+
void writeLogEventTime().catch(() => {});
100103
};

src/utils/test/getRunTest.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export const getRunTest =
2727
const retryIndex = testInfo.retry + 1;
2828
const runId = createRunId(test, retryIndex);
2929

30-
let clearPage: (() => Promise<void>) | undefined;
3130
let hasRunError = false;
3231
let shouldRunTest = false;
3332
let testStaticOptions: TestStaticOptions | undefined;
@@ -52,7 +51,7 @@ export const getRunTest =
5251
testStaticOptions,
5352
};
5453

55-
clearPage = await preparePage(page);
54+
await preparePage(page);
5655

5756
beforeTest(testUnit);
5857

@@ -68,7 +67,7 @@ export const getRunTest =
6867
throw error;
6968
} finally {
7069
if (shouldRunTest) {
71-
await afterTest({clearPage, hasRunError, runId, unknownRunError});
70+
await afterTest({hasRunError, runId, unknownRunError});
7271
}
7372
}
7473
};

0 commit comments

Comments
 (0)