Skip to content

Commit bb7ffa6

Browse files
authored
Merge pull request #121 from joomcode/feat/add-status-code-to-page
feat: add `statusCode` property on pages
2 parents 090596a + 9c1c50c commit bb7ffa6

12 files changed

Lines changed: 101 additions & 60 deletions

File tree

autotests/actions/setPageCookiesAndNavigateToUrl.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import {setHeadersAndNavigateToUrl} from 'e2ed/actions';
22
import {LogEventType} from 'e2ed/constants';
33
import {getHeaderValue, log, replaceSetCookie} from 'e2ed/utils';
44

5-
import type {Cookie, SetCookieHeaderString, StringHeaders, Url} from 'e2ed/types';
5+
import type {Cookie, NavigationReturn, SetCookieHeaderString, StringHeaders, Url} from 'e2ed/types';
66

77
/**
88
* Navigate to the url and set custom page cookies.
99
*/
10-
export const setPageCookiesAndNavigateToUrl = async (
10+
export const setPageCookiesAndNavigateToUrl = (
1111
url: Url,
1212
pageCookies: readonly Cookie[],
13-
): Promise<void> => {
13+
): Promise<NavigationReturn> => {
1414
const mapResponseHeaders = (headers: StringHeaders): StringHeaders => {
1515
const setCookies = getHeaderValue(headers, 'set-cookie');
1616

@@ -28,5 +28,5 @@ export const setPageCookiesAndNavigateToUrl = async (
2828

2929
log(`Navigate to ${url} and set page cookie`, {pageCookies, url}, LogEventType.Action);
3030

31-
await setHeadersAndNavigateToUrl(url, {mapResponseHeaders});
31+
return setHeadersAndNavigateToUrl(url, {mapResponseHeaders});
3232
};

autotests/actions/setPageRequestHeadersAndNavigateToUrl.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import {setHeadersAndNavigateToUrl} from 'e2ed/actions';
22
import {LogEventType} from 'e2ed/constants';
33
import {log} from 'e2ed/utils';
44

5-
import type {StringHeaders, Url} from 'e2ed/types';
5+
import type {NavigationReturn, StringHeaders, Url} from 'e2ed/types';
66

77
/**
88
* Navigate to the url and set additional page request headers.
99
*/
10-
export const setPageRequestHeadersAndNavigateToUrl = async (
10+
export const setPageRequestHeadersAndNavigateToUrl = (
1111
url: Url,
1212
pageRequestHeaders: StringHeaders,
13-
): Promise<void> => {
13+
): Promise<NavigationReturn> => {
1414
const mapRequestHeaders = (): StringHeaders => pageRequestHeaders;
1515

1616
log(
@@ -19,5 +19,5 @@ export const setPageRequestHeadersAndNavigateToUrl = async (
1919
LogEventType.Action,
2020
);
2121

22-
await setHeadersAndNavigateToUrl(url, {mapRequestHeaders});
22+
return setHeadersAndNavigateToUrl(url, {mapRequestHeaders});
2323
};

autotests/pageObjects/pages/E2edReportExample/E2edReportExample.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {setReadonlyProperty} from 'e2ed/utils';
1010

1111
import {TestRunButton} from './TestRunButton';
1212

13-
import type {Cookie, Selector, StringHeaders, Url} from 'e2ed/types';
13+
import type {Cookie, NavigationReturn, Selector, StringHeaders, Url} from 'e2ed/types';
1414

1515
type CustomPageParams =
1616
| {pageCookies?: readonly Cookie[]; pageRequestHeaders?: StringHeaders}
@@ -110,7 +110,7 @@ export class E2edReportExample extends Page<CustomPageParams> {
110110
setReadonlyProperty(this, 'pageRequestHeaders', pageRequestHeaders);
111111
}
112112

113-
override navigateToPage(url: Url): Promise<void> {
113+
override navigateToPage(url: Url): Promise<NavigationReturn> {
114114
if (this.pageRequestHeaders) {
115115
return setPageRequestHeadersAndNavigateToUrl(url, this.pageRequestHeaders);
116116
}

package-lock.json

Lines changed: 33 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,27 @@
2525
"url": "git+https://github.com/joomcode/e2ed.git"
2626
},
2727
"dependencies": {
28-
"@playwright/test": "1.53.2",
28+
"@playwright/test": "1.55.0",
2929
"create-locator": "0.0.27",
3030
"get-modules-graph": "0.0.11",
3131
"sort-json-keys": "1.0.3"
3232
},
3333
"devDependencies": {
34-
"@playwright/browser-chromium": "1.53.2",
35-
"@types/node": "24.0.8",
34+
"@playwright/browser-chromium": "1.55.0",
35+
"@types/node": "24.3.0",
3636
"@typescript-eslint/eslint-plugin": "7.18.0",
3737
"@typescript-eslint/parser": "7.18.0",
3838
"assert-modules-support-case-insensitive-fs": "1.0.1",
3939
"assert-package-lock-is-consistent": "1.0.0",
4040
"eslint": "8.57.1",
4141
"eslint-config-airbnb-base": "15.0.0",
42-
"eslint-config-prettier": "10.1.5",
42+
"eslint-config-prettier": "10.1.8",
4343
"eslint-plugin-import": "2.32.0",
4444
"eslint-plugin-simple-import-sort": "12.1.1",
4545
"eslint-plugin-typescript-sort-keys": "3.3.0",
4646
"husky": "9.1.7",
4747
"prettier": "3.6.2",
48-
"typescript": "5.8.3"
48+
"typescript": "5.9.2"
4949
},
5050
"peerDependencies": {
5151
"@types/node": ">=20",

src/Page.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@ import {CREATE_PAGE_TOKEN} from './constants/internal';
77
import {assertValueIsTrue} from './utils/asserts';
88
import {getFullPackConfig} from './utils/config';
99
import {reloadDocument} from './utils/document';
10+
import {setReadonlyProperty} from './utils/object';
1011
import {getPlaywrightPage} from './useContext';
1112

1213
import type {PageRoute} from './PageRoute';
13-
import type {AsyncVoid, NavigateToUrlOptions, PageClassTypeArgs, Url} from './types/internal';
14+
import type {
15+
AsyncVoid,
16+
NavigateToUrlOptions,
17+
NavigationReturn,
18+
PageClassTypeArgs,
19+
StatusCode,
20+
Url,
21+
} from './types/internal';
1422

1523
/**
1624
* Abstract page with base methods.
@@ -43,6 +51,11 @@ export abstract class Page<PageParams = undefined> {
4351
*/
4452
readonly pageParams: PageParams;
4553

54+
/**
55+
* Status code of page, if any.
56+
*/
57+
readonly statusCode: StatusCode | undefined;
58+
4659
constructor(...args: PageClassTypeArgs<PageParams>) {
4760
const [createPageToken, pageParams] = args;
4861

@@ -112,8 +125,21 @@ export abstract class Page<PageParams = undefined> {
112125
/**
113126
* Navigates to the page by url.
114127
*/
115-
navigateToPage(url: Url, options?: NavigateToUrlOptions): Promise<void> {
116-
return navigateToUrl(url, {skipLogs: true, timeout: this.navigationTimeout, ...options});
128+
async navigateToPage(
129+
this: Page,
130+
url: Url,
131+
options?: NavigateToUrlOptions,
132+
): Promise<NavigationReturn> {
133+
const navigationReturn = await navigateToUrl(url, {
134+
skipLogs: true,
135+
timeout: this.navigationTimeout,
136+
...options,
137+
});
138+
const {statusCode} = navigationReturn;
139+
140+
setReadonlyProperty(this, 'statusCode', statusCode);
141+
142+
return navigationReturn;
117143
}
118144

119145
/**

src/actions/navigateToUrl.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import {LogEventType} from '../constants/internal';
22
import {getPlaywrightPage} from '../useContext';
33
import {log} from '../utils/log';
44

5-
import type {NavigateToUrlOptions, Url} from '../types/internal';
5+
import type {NavigateToUrlOptions, NavigationReturn, StatusCode, Url} from '../types/internal';
66

77
/**
88
* Navigate to the `url` (without waiting of interface stabilization).
99
*/
1010
export const navigateToUrl = async (
1111
url: Url,
1212
options: NavigateToUrlOptions = {},
13-
): Promise<void> => {
13+
): Promise<NavigationReturn> => {
1414
const {skipLogs = false} = options;
1515

1616
if (skipLogs !== true) {
@@ -19,9 +19,16 @@ export const navigateToUrl = async (
1919

2020
const page = getPlaywrightPage();
2121

22-
await page.goto(url, options);
22+
const maybeResponse = await page.goto(url, options);
23+
let statusCode: StatusCode | undefined;
24+
25+
if (maybeResponse !== null) {
26+
statusCode = maybeResponse.status() as StatusCode;
27+
}
2328

2429
if (skipLogs !== true) {
2530
log(`Navigation to the url ${url} completed`, options, LogEventType.InternalAction);
2631
}
32+
33+
return {statusCode};
2734
};

src/actions/setHeadersAndNavigateToUrl.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {log} from '../utils/log';
88

99
import {navigateToUrl} from './navigateToUrl';
1010

11-
import type {MapOptions, NavigateToUrlOptions, Url} from '../types/internal';
11+
import type {MapOptions, NavigateToUrlOptions, NavigationReturn, Url} from '../types/internal';
1212

1313
/**
1414
* Navigate to the url and map custom response and request headers.
@@ -17,7 +17,7 @@ export const setHeadersAndNavigateToUrl = async (
1717
url: Url,
1818
options: MapOptions,
1919
navigateToUrlOptions?: NavigateToUrlOptions,
20-
): Promise<void> => {
20+
): Promise<NavigationReturn> => {
2121
const {mapRequestHeaders, mapResponseHeaders} = options;
2222

2323
const page = getPlaywrightPage();
@@ -59,5 +59,5 @@ export const setHeadersAndNavigateToUrl = async (
5959
);
6060
}
6161

62-
await navigateToUrl(url, {skipLogs: true, ...navigateToUrlOptions});
62+
return navigateToUrl(url, {skipLogs: true, ...navigateToUrlOptions});
6363
};

src/types/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export type {
5353
} from './matchScreenshot';
5454
export type {ApiMockFunction} from './mockApiRoute';
5555
export type {WebSocketMockFunction} from './mockWebSocketRoute';
56-
export type {NavigateToUrlOptions} from './navigation';
56+
export type {NavigateToUrlOptions, NavigationReturn} from './navigation';
5757
export type {
5858
AnyPageClassType,
5959
NavigateToOrAssertPageArgs,
@@ -105,5 +105,6 @@ export type {
105105
UnionToIntersection,
106106
UnwrapSet,
107107
Values,
108+
ZeroOrOneArg,
108109
} from './utils';
109110
export type {RequestPredicate, ResponsePredicate} from './waitForEvents';

0 commit comments

Comments
 (0)