Skip to content

Commit

Permalink
FI-1344 fix: add action pause instead of debug
Browse files Browse the repository at this point in the history
fix: `getBrowserConsoleMessages` action
fix: `getBrowserJsErrors` action
fix: clear page callbacks in the end ot the test
chore: update devDependencies (`@types/node`, `husky`)
  • Loading branch information
uid11 committed Aug 25, 2024
1 parent c56c2fe commit 2a0b558
Show file tree
Hide file tree
Showing 30 changed files with 289 additions and 278 deletions.
27 changes: 18 additions & 9 deletions autotests/tests/e2edReportExample/browserData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('correctly read data from browser', {meta: {testId: '14'}}, async () => {
console.error('error');
console.info('info');
console.log('log');
console.warn('warn');
console.warn('warning');

setTimeout(() => {
throw new Error('foo');
Expand All @@ -29,14 +29,23 @@ test('correctly read data from browser', {meta: {testId: '14'}}, async () => {
}, 32);
})();

const {error, info, log, warn} = await getBrowserConsoleMessages();

await expect(
error.length === 0 && info.length === 0 && log.length === 0 && warn.length === 0,
'getBrowserConsoleMessages read all of messages',
).eql(true);

const jsErrors = await getBrowserJsErrors();
const consoleMessages = getBrowserConsoleMessages();
const columnNumber = 12;
const url = '';

await expect(consoleMessages, 'getBrowserConsoleMessages read all of messages').eql([
{args: ['error'], location: {columnNumber, lineNumber: 3, url}, text: 'error', type: 'error'},
{args: ['info'], location: {columnNumber, lineNumber: 4, url}, text: 'info', type: 'info'},
{args: ['log'], location: {columnNumber, lineNumber: 5, url}, text: 'log', type: 'log'},
{
args: ['warning'],
location: {columnNumber, lineNumber: 6, url},
text: 'warning',
type: 'warning',
},
]);

const jsErrors = getBrowserJsErrors();

await expect(jsErrors.length === 0, 'getBrowserJsErrors read JS errors').eql(true);
});
2 changes: 1 addition & 1 deletion autotests/tests/e2edReportExample/selectorCustomMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('selector custom methods', {meta: {testId: '15'}}, async () => {
'[data-testid="app-navigation-retries"].findByLocatorId(app-navigation-retries-button).filterByLocatorParameter(selected, true)',
);

await click(reportPage.navigationRetriesButton);
await click(reportPage.navigationRetriesButton.nth(0));

await expect(
reportPage.testRunButton.nth(2).getLocatorParameter('status'),
Expand Down
35 changes: 19 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"devDependencies": {
"@playwright/browser-chromium": "1.46.1",
"@types/node": "22.4.1",
"@types/node": "22.5.0",
"@typescript-eslint/eslint-plugin": "7.18.0",
"@typescript-eslint/parser": "7.18.0",
"assert-modules-support-case-insensitive-fs": "1.0.1",
Expand All @@ -42,7 +42,7 @@
"eslint-plugin-import": "2.29.1",
"eslint-plugin-simple-import-sort": "12.1.1",
"eslint-plugin-typescript-sort-keys": "3.2.0",
"husky": "9.1.4",
"husky": "9.1.5",
"prettier": "3.3.3",
"typescript": "5.5.4"
},
Expand Down
12 changes: 0 additions & 12 deletions src/actions/debug.ts

This file was deleted.

31 changes: 13 additions & 18 deletions src/actions/getBrowserConsoleMessages.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
import {LogEventStatus, LogEventType} from '../constants/internal';
import {getConsoleMessagesFromContext} from '../context/consoleMessages';
import {log} from '../utils/log';

type ConsoleMessages = Readonly<{
error: readonly string[];
info: readonly string[];
log: readonly string[];
warn: readonly string[];
}>;
import type {ConsoleMessage} from '../types/internal';

type Options = Readonly<{
showMessagesInLog?: boolean;
}>;

const logMessage = 'Get browser console messages';

/**
* Returns an object that contains messages output to the browser console.
*/
export const getBrowserConsoleMessages = (options: Options = {}): Promise<ConsoleMessages> => {
export const getBrowserConsoleMessages = (options: Options = {}): readonly ConsoleMessage[] => {
const {showMessagesInLog = false} = options;
const consoleMessages = getConsoleMessagesFromContext();

if (showMessagesInLog === false) {
log('Get browser console messages', LogEventType.InternalAction);
log(logMessage, LogEventType.InternalAction);
} else {
const logEventStatus = consoleMessages.some(({type}) => type === 'error')
? LogEventStatus.Failed
: LogEventStatus.Passed;

// TODO
return Promise.resolve({error: [], info: [], log: [], warn: []});
log(logMessage, {consoleMessages, logEventStatus}, LogEventType.InternalAction);
}

return Promise.resolve({error: [], info: [], log: [], warn: []}).then((messages) => {
const logEventStatus =
messages.error.length > 0 ? LogEventStatus.Failed : LogEventStatus.Passed;

log('Got browser console messages', {logEventStatus, messages}, LogEventType.InternalAction);

return messages;
});
return consoleMessages;
};
17 changes: 6 additions & 11 deletions src/actions/getBrowserJsErrors.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import {LogEventStatus, LogEventType} from '../constants/internal';
import {getJsErrorsFromContext} from '../context/jsError';
import {log} from '../utils/log';

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

type Options = Readonly<{
showErrorsInLog?: boolean;
}>;

/**
* Get browser JS errors.
*/
export const getBrowserJsErrors = (options: Options = {}): Promise<readonly BrowserJsError[]> => {
export const getBrowserJsErrors = (options: Options = {}): readonly Error[] => {
const {showErrorsInLog = false} = options;
const jsErrors = getJsErrorsFromContext();

if (showErrorsInLog === false) {
log('Get browser JS errors', LogEventType.InternalAction);

return Promise.resolve([]);
}

// TODO
return Promise.resolve([]).then((jsErrors = []) => {
} else {
const logEventStatus = jsErrors.length > 0 ? LogEventStatus.Failed : LogEventStatus.Passed;

log('Got browser JS errors', {jsErrors, logEventStatus}, LogEventType.InternalAction);
}

return jsErrors;
});
return jsErrors;
};
2 changes: 1 addition & 1 deletion src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export {
} from './asserts';
export {clearUpload} from './clearUpload';
export {click} from './click';
export {debug} from './debug';
export {deleteCookies} from './deleteCookies';
export {dispatchEvent} from './dispatchEvent';
export {doubleClick} from './doubleClick';
Expand All @@ -28,6 +27,7 @@ export {
navigateToPage,
reloadPage,
} from './pages';
export {pause} from './pause';
export {pressKey} from './pressKey';
export {resizeWindow} from './resizeWindow';
export {scroll} from './scroll';
Expand Down
12 changes: 6 additions & 6 deletions src/actions/navigateToUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ 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;
}>;
type Options = Readonly<{skipLogs?: boolean} & Parameters<Page['goto']>[1]>;

/**
* Navigate to the `url` (without waiting of interface stabilization).
Expand All @@ -15,14 +15,14 @@ export const navigateToUrl = async (url: Url, options: Options = {}): Promise<vo
const {skipLogs = false} = options;

if (skipLogs !== true) {
log(`Will navigate to the url ${url}`, LogEventType.InternalAction);
log(`Will navigate to the url ${url}`, options, LogEventType.InternalAction);
}

const page = getPlaywrightPage();

await page.goto(url);
await page.goto(url, options);

if (skipLogs !== true) {
log(`Navigation to the url ${url} completed`, LogEventType.InternalAction);
log(`Navigation to the url ${url} completed`, options, LogEventType.InternalAction);
}
};
14 changes: 14 additions & 0 deletions src/actions/pause.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {LogEventType} from '../constants/internal';
import {getPlaywrightPage} from '../useContext';
import {log} from '../utils/log';

/**
* Pauses the test and switches to the step-by-step execution mode.
*/
export const pause = (): Promise<void> => {
log('Pause', LogEventType.InternalAction);

const page = getPlaywrightPage();

return page.pause();
};
23 changes: 21 additions & 2 deletions src/actions/waitFor/waitForRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {getDurationWithUnits} from '../../utils/getDurationWithUnits';
import {log} from '../../utils/log';
import {getRequestFromPlaywrightRequest} from '../../utils/requestHooks';

import type {Request, RequestPredicate, RequestWithUtcTimeInMs} from '../../types/internal';
import type {
Request,
RequestPredicate,
RequestWithUtcTimeInMs,
UtcTimeInMs,
} from '../../types/internal';

/**
* Waits for some request (from browser) filtered by the request predicate.
Expand All @@ -17,6 +22,8 @@ export const waitForRequest = <SomeRequest extends Request>(
predicate: RequestPredicate<SomeRequest>,
{skipLogs = false, timeout}: {skipLogs?: boolean; timeout?: number} = {},
): Promise<RequestWithUtcTimeInMs<SomeRequest>> => {
const startTimeInMs = Date.now() as UtcTimeInMs;

setCustomInspectOnFunction(predicate);

const {waitForRequestTimeout} = getFullPackConfig();
Expand Down Expand Up @@ -57,5 +64,17 @@ export const waitForRequest = <SomeRequest extends Request>(
);
}

return promise;
return skipLogs !== true
? promise.then((request) => {
const waitWithUnits = getDurationWithUnits(Date.now() - startTimeInMs);

log(
`Have waited for request for ${waitWithUnits}`,
{predicate, request, timeoutWithUnits},
LogEventType.InternalCore,
);

return request;
})
: promise;
};
24 changes: 22 additions & 2 deletions src/actions/waitFor/waitForResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import {log} from '../../utils/log';
import {getResponseFromPlaywrightResponse} from '../../utils/requestHooks';
import {getWaitForResponsePredicate} from '../../utils/waitForEvents';

import type {Request, Response, ResponsePredicate, ResponseWithRequest} from '../../types/internal';
import type {
Request,
Response,
ResponsePredicate,
ResponseWithRequest,
UtcTimeInMs,
} from '../../types/internal';

type Options = Readonly<{includeNavigationRequest?: boolean; skipLogs?: boolean; timeout?: number}>;

Expand All @@ -24,6 +30,8 @@ export const waitForResponse = <
predicate: ResponsePredicate<SomeRequest, SomeResponse>,
{includeNavigationRequest = false, skipLogs = false, timeout}: Options = {},
): Promise<ResponseWithRequest<SomeResponse, SomeRequest>> => {
const startTimeInMs = Date.now() as UtcTimeInMs;

setCustomInspectOnFunction(predicate);

const {waitForResponseTimeout} = getFullPackConfig();
Expand Down Expand Up @@ -59,5 +67,17 @@ export const waitForResponse = <
);
}

return promise;
return skipLogs !== true
? promise.then((response) => {
const waitWithUnits = getDurationWithUnits(Date.now() - startTimeInMs);

log(
`Have waited for response for ${waitWithUnits}`,
{predicate, response, timeoutWithUnits},
LogEventType.InternalCore,
);

return response;
})
: promise;
};
Loading

0 comments on commit 2a0b558

Please sign in to comment.