import PARTY from '../../src/enum/party'; import { sleep } from './sleep'; /** * Retry function when hit error for maxTries times * * @export * @param {number} maxTries * @param {Function} fn * @param {[]} params */ export function retry(maxTries: number, fn: Function, params: any[]): void { let count = 0; while (true) { try { fn(...params); break; } catch (error) { if (++count == maxTries) { throw error; } } } } /** * Retry a function based on a condition for times. * Example: Retry a HTTP request until the response object returns a true condition * @export * @param {number} maxTries * @param {Function} fn * @param {any[]} params * @param {((result:any)=> Promise | boolean)} checkFn * @return {*} {Promise} */ export async function retryWithCondition( maxTries: number, fn: Function, params: any[], checkFn: (result: any) => Promise | boolean ): Promise { let count = maxTries; while (count > 0) { const result = await fn(...params); if (await checkFn(result)) { return result; } await sleep(1500); count--; } throw new Error('Condition is not met, max amount of tries exceeded.'); } /** * Ensure element is visible, else refresh the page * * @export * @param {*} element */ export async function ensureElementIsVisible(element: any) { try { element.waitForVisible(); } catch (error) { logger.info('**********Failed to find element**********'); await browser.refresh(); throw error; } } /** * Check if party is an export party * * @export * @param {string} party * @return {*} {boolean} */ export function isExportParty(party: string): boolean { const exportParties = [ PARTY.BENEFICIARY, PARTY.CONFIRMING_BANK, PARTY.NOMINATED_BANK, PARTY.ADVISING_BANK, PARTY.NEW_ADVISING_BANK, PARTY.NEW_CONFIRMING_BANK ]; const partyEnum: any = party; return Object.values(exportParties).includes(partyEnum); }