Skip to content

Commit a5a0fd4

Browse files
committed
PRO-10170 fix: add switchPlaywrightPage internal switcher
1 parent f015362 commit a5a0fd4

20 files changed

Lines changed: 354 additions & 72 deletions

autotests/packs/allTests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const pack: Pack = {
8080
skipTests,
8181
takeFullPageScreenshotOnError: false,
8282
takeViewportScreenshotOnError: true,
83-
testFileGlobs: ['**/autotests/tests/**/switchingPages.ts'],
83+
testFileGlobs: ['**/autotests/tests/**/switchingPages*.ts'],
8484
testIdleTimeout: 8_000,
8585
testTimeout: 15_000,
8686
userAgent,

autotests/pageObjects/pages/E2edReportExample/E2edReportExample.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
} from 'autotests/actions';
55
import {setPageCookies, setPageRequestHeaders} from 'autotests/context';
66
import {E2edReportExample as E2edReportExampleRoute} from 'autotests/routes/pageRoutes';
7-
import {createSelector, locator} from 'autotests/selectors';
7+
import {locator} from 'autotests/selectors';
88
import {Page} from 'e2ed';
99
import {setReadonlyProperty} from 'e2ed/utils';
1010

@@ -23,7 +23,7 @@ export class E2edReportExample extends Page<CustomPageParams> {
2323
/**
2424
* Page header.
2525
*/
26-
readonly header: Selector = createSelector('.header');
26+
readonly header: Selector = locator('header');
2727

2828
/**
2929
* Navigation bar with test retries.

autotests/tests/switchingPages.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* eslint-disable @typescript-eslint/no-magic-numbers */
2+
3+
import {test} from 'autotests';
4+
import {getUsers} from 'autotests/entities';
5+
import {E2edReportExample} from 'autotests/pageObjects/pages';
6+
import {GetUsers} from 'autotests/routes/apiRoutes';
7+
import {expect} from 'e2ed';
8+
import {
9+
click,
10+
navigateToPage,
11+
switchToTab,
12+
waitForNewTab,
13+
waitForRequestToRoute,
14+
waitForTimeout,
15+
} from 'e2ed/actions';
16+
import {log} from 'e2ed/utils';
17+
18+
const maxNumberOfRequests = 15;
19+
20+
const timeout = (maxNumberOfRequests + 10) * 1_000;
21+
22+
test(
23+
'support switching of tabs for waitForRequest',
24+
{meta: {testId: '21'}, testTimeout: timeout + 1_000},
25+
async () => {
26+
let numberOfCaughtRequests = 0;
27+
let numberOfSentRequests = 0;
28+
29+
setInterval(() => {
30+
if (numberOfSentRequests < maxNumberOfRequests) {
31+
numberOfSentRequests += 1;
32+
33+
void getUsers({retries: 1});
34+
}
35+
}, 1_000);
36+
37+
void waitForRequestToRoute(GetUsers, {
38+
predicate: (routeParams, request) => {
39+
numberOfCaughtRequests += 1;
40+
41+
log(`Caught request number ${numberOfCaughtRequests}`, {request, routeParams});
42+
43+
return false;
44+
},
45+
timeout,
46+
});
47+
48+
await waitForTimeout(maxNumberOfRequests * 333);
49+
50+
const reportPage = await navigateToPage(E2edReportExample);
51+
52+
await waitForTimeout(maxNumberOfRequests * 333);
53+
54+
const npmPageTab = await waitForNewTab(async () => {
55+
await click(reportPage.header);
56+
});
57+
58+
switchToTab(npmPageTab);
59+
60+
await waitForTimeout(maxNumberOfRequests * 333 + 1_000);
61+
62+
await expect(numberOfSentRequests, 'all requests were caught').eql(numberOfCaughtRequests);
63+
},
64+
);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* eslint-disable @typescript-eslint/no-magic-numbers */
2+
3+
import {test} from 'autotests';
4+
import {getUsers} from 'autotests/entities';
5+
import {E2edReportExample} from 'autotests/pageObjects/pages';
6+
import {GetUsers} from 'autotests/routes/apiRoutes';
7+
import {expect} from 'e2ed';
8+
import {
9+
click,
10+
navigateToPage,
11+
switchToTab,
12+
waitForNewTab,
13+
waitForResponseToRoute,
14+
waitForTimeout,
15+
} from 'e2ed/actions';
16+
import {log} from 'e2ed/utils';
17+
18+
const maxNumberOfResponses = 15;
19+
20+
const timeout = (maxNumberOfResponses + 10) * 1_000;
21+
22+
test(
23+
'support switching of tabs for waitForResponse',
24+
{meta: {testId: '22'}, testTimeout: timeout + 1_000},
25+
async () => {
26+
let numberOfCaughtResponses = 0;
27+
let numberOfSentResponses = 0;
28+
29+
setInterval(() => {
30+
if (numberOfSentResponses < maxNumberOfResponses) {
31+
numberOfSentResponses += 1;
32+
33+
void getUsers({retries: 1});
34+
}
35+
}, 1_000);
36+
37+
void waitForResponseToRoute(GetUsers, {
38+
predicate: (routeParams, response) => {
39+
numberOfCaughtResponses += 1;
40+
41+
log(`Caught response number ${numberOfCaughtResponses}`, {response, routeParams});
42+
43+
return false;
44+
},
45+
timeout,
46+
});
47+
48+
await waitForTimeout(maxNumberOfResponses * 333);
49+
50+
const reportPage = await navigateToPage(E2edReportExample);
51+
52+
await waitForTimeout(maxNumberOfResponses * 333);
53+
54+
const npmPageTab = await waitForNewTab(async () => {
55+
await click(reportPage.header);
56+
});
57+
58+
switchToTab(npmPageTab);
59+
60+
await waitForTimeout(maxNumberOfResponses * 333 + 1_000);
61+
62+
await expect(numberOfSentResponses, 'all responses were caught').eql(numberOfCaughtResponses);
63+
},
64+
);

src/actions/switchToMainTab.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ import {LogEventType} from '../constants/internal';
22
import {clearTab} from '../context/tab';
33
import {getPlaywrightPage} from '../useContext';
44
import {log} from '../utils/log';
5+
import {switchPlaywrightPage} from '../utils/playwrightPage';
56

67
/**
78
* Switches page context to the specified tab.
89
*/
910
export const switchToMainTab = (): void => {
11+
clearTab();
12+
1013
const page = getPlaywrightPage();
1114
const url = page.url();
1215

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

15-
clearTab();
18+
switchPlaywrightPage(page);
1619
};

src/actions/switchToTab.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {LogEventType} from '../constants/internal';
22
import {setTab} from '../context/tab';
33
import {log} from '../utils/log';
4+
import {switchPlaywrightPage} from '../utils/playwrightPage';
45

56
import type {InternalTab, Tab} from '../types/internal';
67

@@ -14,4 +15,6 @@ export const switchToTab = (tab: Tab): void => {
1415
log(`Switch page context to the specified tab at ${url}`, LogEventType.InternalAction);
1516

1617
setTab(tab);
18+
19+
switchPlaywrightPage(page);
1720
};

src/actions/waitFor/waitForRequest.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {E2edError} from '../../utils/error';
88
import {setCustomInspectOnFunction} from '../../utils/fn';
99
import {getDurationWithUnits} from '../../utils/getDurationWithUnits';
1010
import {log} from '../../utils/log';
11+
import {pageWaitForRequest} from '../../utils/playwrightPage';
1112
import {addTimeoutToPromise} from '../../utils/promise';
1213
import {getRequestFromPlaywrightRequest} from '../../utils/requestHooks';
1314

@@ -68,7 +69,8 @@ export const waitForRequest = (async <SomeRequest extends Request>(
6869
const timeoutWithUnits = getDurationWithUnits(timeout);
6970

7071
const promise = addTimeoutToPromise(
71-
page.waitForRequest(
72+
pageWaitForRequest(
73+
page,
7274
AsyncLocalStorage.bind(async (playwrightRequest: PlaywrightRequest) => {
7375
try {
7476
const request = getRequestFromPlaywrightRequest(playwrightRequest);

src/actions/waitFor/waitForResponse.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {E2edError} from '../../utils/error';
88
import {setCustomInspectOnFunction} from '../../utils/fn';
99
import {getDurationWithUnits} from '../../utils/getDurationWithUnits';
1010
import {log} from '../../utils/log';
11+
import {pageWaitForResponse} from '../../utils/playwrightPage';
1112
import {addTimeoutToPromise} from '../../utils/promise';
1213
import {getResponseFromPlaywrightResponse} from '../../utils/requestHooks';
1314
import {getWaitForResponsePredicate} from '../../utils/waitForEvents';
@@ -71,7 +72,8 @@ export const waitForResponse = (async <
7172
const timeoutWithUnits = getDurationWithUnits(timeout);
7273

7374
const promise = addTimeoutToPromise(
74-
page.waitForResponse(
75+
pageWaitForResponse(
76+
page,
7577
AsyncLocalStorage.bind(
7678
getWaitForResponsePredicate(
7779
predicate as ResponsePredicate,

src/types/report.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export type ReportClientData = Readonly<{
9393
export type ReportClientState = {
9494
clickListeners?: Record<string, (event: HTMLElement) => void>;
9595
readonly createLocatorOptions: CreateLocatorOptions;
96-
readonly e2edRightColumnContainer: HTMLElement;
96+
readonly e2edRightColumnContainer: HTMLElement | undefined;
9797
readonly fullTestRuns: readonly FullTestRun[];
9898
readonly internalDirectoryName: string;
9999
lengthOfReadedJsonReportDataParts: number;

0 commit comments

Comments
 (0)