diff --git a/src/actions/check.ts b/src/actions/check.ts new file mode 100644 index 00000000..53566a54 --- /dev/null +++ b/src/actions/check.ts @@ -0,0 +1,20 @@ +import {LogEventType} from '../constants/internal'; +import {step} from '../step'; + +import type {Locator} from '@playwright/test'; + +import type {Selector} from '../types/internal'; + +type Options = Parameters[0]; + +/** + * Checks checkbox or radio element, if it is not checked yet. + */ +export const check = (selector: Selector, options: Options = {}): Promise => + step( + `Checks checked or radio element ${selector.description}`, + async () => { + await selector.getPlaywrightLocator().check(options); + }, + {payload: {...options, selector}, type: LogEventType.InternalAction}, + ); diff --git a/src/actions/index.ts b/src/actions/index.ts index f1aef9ef..ea0d0091 100644 --- a/src/actions/index.ts +++ b/src/actions/index.ts @@ -5,6 +5,7 @@ export { assertUrlMatchRoute, } from './asserts'; export {blur} from './blur'; +export {check} from './check'; export {clearCookies} from './clearCookies'; export {clearInput} from './clearInput'; export {click} from './click'; diff --git a/src/config.ts b/src/config.ts index 7c6133cc..3e275ec3 100644 --- a/src/config.ts +++ b/src/config.ts @@ -97,7 +97,7 @@ if (IS_DEBUG || isUiMode) { } const useOptions: PlaywrightTestConfig['use'] = { - actionTimeout: Math.round(userlandPack.testIdleTimeout / 2), + actionTimeout: userlandPack.selectorTimeout, browserName: userlandPack.browserName, // eslint-disable-next-line @typescript-eslint/naming-convention bypassCSP: !userlandPack.enableCsp, diff --git a/src/utils/expect/getAssertionPromise.ts b/src/utils/expect/getAssertionPromise.ts index ede24439..ef7f2fa4 100644 --- a/src/utils/expect/getAssertionPromise.ts +++ b/src/utils/expect/getAssertionPromise.ts @@ -76,11 +76,17 @@ export const getAssertionPromise = ({ const assertionPromise: Promise = RESOLVED_PROMISE.then(() => { if (isThenable(context.actualValue)) { - return addTimeoutToPromise( - context.actualValue as Promise, - timeout, - timeoutError, - ).then(runAssertion); + return addTimeoutToPromise(context.actualValue as Promise, timeout, timeoutError) + .then(runAssertion) + .catch((error: Error) => { + const ctx: Expect = { + actualValue: '', + description: context.description, + error, + }; + + return ctx; + }); } return runAssertion(context.actualValue); diff --git a/src/utils/index.ts b/src/utils/index.ts index a1ef4d45..86a7dac8 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -38,6 +38,7 @@ export {getHash} from './getHash'; export {getKeysCounter} from './getKeysCounter'; export {getEquivalentHeadersNames, getHeadersFromHeaderEntries, getHeaderValue} from './headers'; export {getContentJsonHeaders} from './http'; +export {getCheckedInputValue, getInputByValue} from './input'; export {log} from './log'; export {deepMerge, getEntries, getKeys, setReadonlyProperty} from './object'; export {createPageObjectsFromMultiLocator} from './pageObjects'; diff --git a/src/utils/input/getCheckedInputValue.ts b/src/utils/input/getCheckedInputValue.ts new file mode 100644 index 00000000..0a5ba183 --- /dev/null +++ b/src/utils/input/getCheckedInputValue.ts @@ -0,0 +1,18 @@ +import type {Selector} from '../../types/internal'; + +/** + * Get checked input value from multiselector (usually from ``). + */ +export const getCheckedInputValue = async (input: Selector): Promise => { + const count = await input.count; + + for (let index = 0; index < count; index += 1) { + const currentInput = input.nth(index); + + if (await currentInput.checked) { + return currentInput.value; + } + } + + return null; +}; diff --git a/src/utils/input/getInputByValue.ts b/src/utils/input/getInputByValue.ts new file mode 100644 index 00000000..6f2e4e99 --- /dev/null +++ b/src/utils/input/getInputByValue.ts @@ -0,0 +1,18 @@ +import type {Selector} from '../../types/internal'; + +/** + * Get input from multiselector (usually from ``) by input value. + */ +export const getInputByValue = async (input: Selector, value: string): Promise => { + const count = await input.count; + + for (let index = 0; index < count; index += 1) { + const currentInput = input.nth(index); + + if ((await currentInput.value) === value) { + return currentInput; + } + } + + return null; +}; diff --git a/src/utils/input/index.ts b/src/utils/input/index.ts new file mode 100644 index 00000000..7c5e3148 --- /dev/null +++ b/src/utils/input/index.ts @@ -0,0 +1,2 @@ +export {getCheckedInputValue} from './getCheckedInputValue'; +export {getInputByValue} from './getInputByValue';