|
| 1 | +import { Locator } from "playwright"; |
| 2 | + |
| 3 | +export const conditionValidations = { |
| 4 | + PRESENT: 'present', |
| 5 | + // CLICKABLE: 'clickable', |
| 6 | + VISIBLE: 'visible', |
| 7 | + INVISIBLE: 'invisible', |
| 8 | + // ENABLED: 'enabled', |
| 9 | + // DISABLED: 'disabled' |
| 10 | +} |
| 11 | + |
| 12 | +const notClause = '(not )?'; |
| 13 | +const toBeClause = 'to (?:be )?'; |
| 14 | +const validationClause = `(${Object.values(conditionValidations).join('|')})`; |
| 15 | + |
| 16 | +export const conditionWaitExtractRegexp = new RegExp(`^${notClause}${toBeClause}${validationClause}$`); |
| 17 | +export const conditionWaitRegexp = new RegExp(`(${notClause}${toBeClause}${validationClause})`); |
| 18 | + |
| 19 | +const waits = { |
| 20 | + [conditionValidations.PRESENT]: ( |
| 21 | + element: Locator, |
| 22 | + reverse: boolean, |
| 23 | + timeout: number, |
| 24 | + timeoutMsg: string |
| 25 | + ) => element.waitFor({state: reverse ? 'detached' : 'attached', timeout}), |
| 26 | + [conditionValidations.VISIBLE]: ( |
| 27 | + element: Locator, |
| 28 | + reverse: boolean, |
| 29 | + timeout: number, |
| 30 | + timeoutMsg: string |
| 31 | + ) => element.waitFor({state: reverse ? 'hidden' : 'visible', timeout}), |
| 32 | + [conditionValidations.INVISIBLE]: ( |
| 33 | + element: Locator, |
| 34 | + reverse: boolean, |
| 35 | + timeout: number, |
| 36 | + timeoutMsg: string |
| 37 | + ) => element.waitFor({state: reverse ? 'visible' : 'hidden', timeout}) |
| 38 | +} |
| 39 | +/** |
| 40 | + * Wait for condition |
| 41 | + * @param {Locator} element - element |
| 42 | + * @param {string} validationType - validation to perform |
| 43 | + * @param {number} [timeout] - timeout to wait |
| 44 | + * @param {boolean} [reverse] - negate flag |
| 45 | + * @return {Promise<void>} |
| 46 | + */ |
| 47 | +export async function conditionWait( |
| 48 | + element: Locator, |
| 49 | + validationType: string, |
| 50 | + timeout: number = 10000, |
| 51 | + reverse: boolean = false |
| 52 | +) { |
| 53 | + const timeoutMsg: string = `Element is${reverse ? '' : ' not'} ${validationType}`; |
| 54 | + const waitFn = waits[validationType]; |
| 55 | + await waitFn(element, reverse, timeout, timeoutMsg); |
| 56 | +} |
0 commit comments