Skip to content

Commit

Permalink
FI-1512 fix: add pack option and page property navigationTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
uid11 committed Nov 21, 2024
1 parent 07719bb commit c41b348
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 65 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,15 @@ If the mapping returns `undefined`, the log entry is not skipped, but is printed
`your-project/autotests/bin/runDocker.sh` (until the test passes).
For example, if it is equal to three, the test will be run no more than three times.

`navigationTimeout: number`: default timeout for navigation to url
(`navigateToPage`, `navigateToUrl` actions) in milliseconds.

`overriddenConfigFields: PlaywrightTestConfig | null`: if not `null`, then this value will override
fields of internal Playwright config.

`packTimeout: number`: timeout (in millisecond) for the entire pack of tests (tasks).
If the test pack takes longer than this timeout, the pack will fail with the appropriate error.

`pageStabilizationInterval: number`: after navigating to the page, `e2ed` will wait until
the page is stable for the specified time in millisecond, and only after that it will consider the page loaded.
This parameter can be overridden on a specific page instance.

`pathToScreenshotsDirectoryForReport: string | null`: path to the directory where screenshots
will be stored for displaying them in the HTML report.
This path must be either relative (from the HTML report file) or absolute (i.e. with http/https protocol).
Expand Down
3 changes: 1 addition & 2 deletions autotests/packs/allTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ export const pack: Pack = {
mapLogPayloadInLogFile,
mapLogPayloadInReport,
maxRetriesCountInDocker: 3,
navigationTimeout: 6_000,
overriddenConfigFields: null,
packTimeout: packTimeoutInMinutes * msInMinute,
pageRequestTimeout: 7_000,
pageStabilizationInterval: 500,
pathToScreenshotsDirectoryForReport: './screenshots',
port1: 1337,
port2: 1338,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export class E2edReportExample extends Page<CustomPageParams> {
readonly navigationRetriesButtonSelected: Selector =
this.navigationRetriesButton.filterByLocatorParameter('selected', 'true');

override readonly navigationTimeout = 5_000;

/**
* Cookies that we set (additionally) on a page before navigating to it.
*/
Expand All @@ -47,8 +49,6 @@ export class E2edReportExample extends Page<CustomPageParams> {
*/
readonly pageRequestHeaders: StringHeaders | undefined;

override readonly pageStabilizationInterval = 600;

/**
* Test run button.
*/
Expand Down
22 changes: 10 additions & 12 deletions src/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {reloadDocument} from './utils/document';
import {getPlaywrightPage} from './useContext';

import type {PageRoute} from './PageRoute';
import type {AsyncVoid, PageClassTypeArgs, Url} from './types/internal';
import type {AsyncVoid, NavigateToUrlOptions, PageClassTypeArgs, Url} from './types/internal';

/**
* Abstract page with base methods.
Expand All @@ -33,17 +33,15 @@ export abstract class Page<PageParams = undefined> {
readonly maxIntervalBetweenRequestsInMs: number;

/**
* Immutable page parameters.
* Default timeout for navigation to url (`navigateToPage`, `navigateToUrl` actions) in milliseconds.
* The default value is taken from the corresponding field of the pack config.
*/
readonly pageParams: PageParams;
readonly navigationTimeout: number;

/**
* After navigating to the page, `e2ed` will wait until
* the page is stable for the specified time in millisecond,
* and only after that it will consider the page loaded.
* The default value is taken from the corresponding field of the pack config.
* Immutable page parameters.
*/
readonly pageStabilizationInterval: number;
readonly pageParams: PageParams;

constructor(...args: PageClassTypeArgs<PageParams>) {
const [createPageToken, pageParams] = args;
Expand All @@ -56,12 +54,12 @@ export abstract class Page<PageParams = undefined> {
this.pageParams = pageParams as PageParams;

const {
pageStabilizationInterval,
navigationTimeout,
waitForAllRequestsComplete: {maxIntervalBetweenRequestsInMs},
} = getFullPackConfig();

this.maxIntervalBetweenRequestsInMs = maxIntervalBetweenRequestsInMs;
this.pageStabilizationInterval = pageStabilizationInterval;
this.navigationTimeout = navigationTimeout;
}

/**
Expand Down Expand Up @@ -114,8 +112,8 @@ export abstract class Page<PageParams = undefined> {
/**
* Navigates to the page by url.
*/
navigateToPage(url: Url): Promise<void> {
return navigateToUrl(url, {skipLogs: true});
navigateToPage(url: Url, options?: NavigateToUrlOptions): Promise<void> {
return navigateToUrl(url, {skipLogs: true, timeout: this.navigationTimeout, ...options});
}

/**
Expand Down
11 changes: 5 additions & 6 deletions src/actions/navigateToUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import {LogEventType} from '../constants/internal';
import {getPlaywrightPage} from '../useContext';
import {log} from '../utils/log';

import type {Page} from '@playwright/test';

import type {Url} from '../types/internal';

type Options = Readonly<{skipLogs?: boolean} & Parameters<Page['goto']>[1]>;
import type {NavigateToUrlOptions, Url} from '../types/internal';

/**
* Navigate to the `url` (without waiting of interface stabilization).
*/
export const navigateToUrl = async (url: Url, options: Options = {}): Promise<void> => {
export const navigateToUrl = async (
url: Url,
options: NavigateToUrlOptions = {},
): Promise<void> => {
const {skipLogs = false} = options;

if (skipLogs !== true) {
Expand Down
4 changes: 1 addition & 3 deletions src/actions/pages/history/backPageHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {LogEventType} from '../../../constants/internal';
import {createClientFunction} from '../../../createClientFunction';
import {log} from '../../../utils/log';

import {waitForInterfaceStabilization} from '../../waitFor';

import type {AnyPageClassType} from '../../../types/internal';

const backPageHistoryClient = createClientFunction(() => window.history.back(), {
Expand All @@ -22,5 +20,5 @@ export const backPageHistory = async (page: InstanceType<AnyPageClassType>): Pro

await backPageHistoryClient();

await waitForInterfaceStabilization(page.pageStabilizationInterval);
await page.waitForPageLoaded();
};
4 changes: 1 addition & 3 deletions src/actions/pages/history/forwardPageHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {LogEventType} from '../../../constants/internal';
import {createClientFunction} from '../../../createClientFunction';
import {log} from '../../../utils/log';

import {waitForInterfaceStabilization} from '../../waitFor';

import type {AnyPageClassType} from '../../../types/internal';

const forwardPageHistoryClient = createClientFunction(() => window.history.forward(), {
Expand All @@ -22,5 +20,5 @@ export const forwardPageHistory = async (page: InstanceType<AnyPageClassType>):

await forwardPageHistoryClient();

await waitForInterfaceStabilization(page.pageStabilizationInterval);
await page.waitForPageLoaded();
};
4 changes: 1 addition & 3 deletions src/actions/pages/history/goPageHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {LogEventType} from '../../../constants/internal';
import {createClientFunction} from '../../../createClientFunction';
import {log} from '../../../utils/log';

import {waitForInterfaceStabilization} from '../../waitFor';

import type {AnyPageClassType} from '../../../types/internal';

const goPageHistoryClient = createClientFunction((delta: number) => window.history.go(delta), {
Expand All @@ -25,5 +23,5 @@ export const goPageHistory = async (

await goPageHistoryClient(delta);

await waitForInterfaceStabilization(page.pageStabilizationInterval);
await page.waitForPageLoaded();
};
10 changes: 7 additions & 3 deletions src/actions/setHeadersAndNavigateToUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import {applyHeadersMapper} from '../utils/requestHooks';

import {navigateToUrl} from './navigateToUrl';

import type {MapOptions, Url} from '../types/internal';
import type {MapOptions, NavigateToUrlOptions, Url} from '../types/internal';

/**
* Navigate to the url and map custom response and request headers.
*/
export const setHeadersAndNavigateToUrl = async (url: Url, options: MapOptions): Promise<void> => {
export const setHeadersAndNavigateToUrl = async (
url: Url,
options: MapOptions,
navigateToUrlOptions?: NavigateToUrlOptions,
): Promise<void> => {
const {mapRequestHeaders, mapResponseHeaders} = options;

const page = getPlaywrightPage();
Expand Down Expand Up @@ -52,5 +56,5 @@ export const setHeadersAndNavigateToUrl = async (url: Url, options: MapOptions):
);
}

await navigateToUrl(url, {skipLogs: true});
await navigateToUrl(url, {skipLogs: true, ...navigateToUrlOptions});
};
2 changes: 1 addition & 1 deletion src/actions/waitFor/waitForNewTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const waitForNewTab = async (options?: Options): Promise<Tab> => {
const startTimeInMs = Date.now() as UtcTimeInMs;

const context = getPlaywrightPage().context();
const timeout = options?.timeout ?? getFullPackConfig().pageRequestTimeout;
const timeout = options?.timeout ?? getFullPackConfig().navigationTimeout;

const page = await context.waitForEvent('page', {timeout});

Expand Down
2 changes: 1 addition & 1 deletion src/actions/waitFor/waitForStartOfPageLoad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const waitForStartOfPageLoad = async (options?: Options): Promise<URL> =>
const startTimeInMs = Date.now() as UtcTimeInMs;

const page = getPlaywrightPage();
const timeout = options?.timeout ?? getFullPackConfig().pageRequestTimeout;
const timeout = options?.timeout ?? getFullPackConfig().navigationTimeout;

let urlObject: URL | undefined;
let wasCalled = false;
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const useOptions: PlaywrightTestConfig['use'] = {
headless: isLocalRun ? userlandPack.enableHeadlessMode : true,
isMobile: userlandPack.enableMobileDeviceMode,
launchOptions: {args: [...userlandPack.browserFlags]},
navigationTimeout: userlandPack.pageRequestTimeout,
navigationTimeout: userlandPack.navigationTimeout,
trace: 'retain-on-failure',
userAgent: userlandPack.userAgent,
viewport: {height: userlandPack.viewportHeight, width: userlandPack.viewportWidth},
Expand Down
1 change: 0 additions & 1 deletion src/types/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export type UserlandPackWithoutDoBeforePack<
> = Readonly<{
assertionTimeout: number;
concurrency: number;
pageRequestTimeout: number;
port1: number;
port2: number;
selectorTimeout: number;
Expand Down
12 changes: 5 additions & 7 deletions src/types/config/ownE2edConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ export type OwnE2edConfig<
*/
maxRetriesCountInDocker: number;

/**
* Default timeout for navigation to url (`navigateToPage`, `navigateToUrl` actions) in milliseconds.
*/
navigationTimeout: number;

/**
* If not `null`, then this value will override fields of internal Playwright config.
*/
Expand All @@ -169,13 +174,6 @@ export type OwnE2edConfig<
*/
packTimeout: number;

/**
* After navigating to the page, `e2ed` will wait until the page is stable
* for the specified time in millisecond, and only after that it will consider the page loaded.
* This parameter can be overridden on a specific page instance.
*/
pageStabilizationInterval: number;

/**
* Path to the directory where screenshots will be stored for displaying them in the HTML report.
* This path must be either relative (from the HTML report file) or absolute (i.e. with http/https protocol).
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type {
} from './http';
export type {KeyboardPressKey} from './keyboard';
export type {ApiMockFunction} from './mockApiRoute';
export type {NavigateToUrlOptions} from './navigation';
export type {
AnyPageClassType,
NavigateToOrAssertPageArgs,
Expand Down
1 change: 1 addition & 0 deletions src/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export type {
export type {ApiMockFunction} from './mockApiRoute';
/** @internal */
export type {ApiMockState} from './mockApiRoute';
export type {NavigateToUrlOptions} from './navigation';
/** @internal */
export type {NavigationDelay} from './navigation';
export type {
Expand Down
7 changes: 7 additions & 0 deletions src/types/navigation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import type {Page} from '@playwright/test';

/**
* Options for `navigateToUrl` action.
*/
export type NavigateToUrlOptions = Readonly<{skipLogs?: boolean} & Parameters<Page['goto']>[1]>;

/**
* Object with information for navigation delay.
* @internal
Expand Down
24 changes: 8 additions & 16 deletions src/utils/config/assertUserlandPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,19 @@ export const assertUserlandPack = (userlandPack: UserlandPack): void => {
logParams,
);

assertNumberIsPositiveInteger(
userlandPack.packTimeout,
'packTimeout is positive integer',
logParams,
);

if (userlandPack.pageRequestTimeout !== 0) {
if (userlandPack.navigationTimeout !== 0) {
assertNumberIsPositiveInteger(
userlandPack.pageRequestTimeout,
'pageRequestTimeout is positive integer',
userlandPack.navigationTimeout,
'navigationTimeout is positive integer',
logParams,
);
}

if (userlandPack.pageStabilizationInterval !== 0) {
assertNumberIsPositiveInteger(
userlandPack.pageStabilizationInterval,
'pageStabilizationInterval is positive integer',
logParams,
);
}
assertNumberIsPositiveInteger(
userlandPack.packTimeout,
'packTimeout is positive integer',
logParams,
);

assertNumberIsPositiveInteger(userlandPack.port1, 'port1 is positive integer', logParams);
assertNumberIsPositiveInteger(userlandPack.port2, 'port2 is positive integer', logParams);
Expand Down

0 comments on commit c41b348

Please sign in to comment.