const { I } = inject(); /** * Wait Class */ class Wait { static count: number; /** * Waits for an element to be removed or become invisible on the page * * ```js * Wait.waitUntilInvisibilityOfElement("//input[@id='login']",20); * ``` * * @param {any} locatorParam locator located by xpath|CSS * @param {String} timeoutSecs in secs * */ static async waitUntilInvisibilityOfElement(locatorParam: any, timeoutSecs?: number) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } if (timeoutSecs != undefined) { console.log(`Timeout Passed To Paramater is:`, timeoutSecs); } await I.waitForInvisible(locator); } /** * Waits for an element to become visible on the page * * ```js * Wait.waitUntilVisibilityOfElement("//input[@id='login']",20); * ``` * * @param {any} locatorParam located by xpath|CSS * @param {String} timeoutSecs in secs * */ static async waitUntilVisibilityOfElement(locatorParam: any, timeoutSecs?: number) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } try { if (timeoutSecs != undefined) { console.log(`Timeout Passed To Paramater is:`, timeoutSecs); } await I.waitForVisible(locator); } catch (error) { throw error; } } /** * Pauses for an execution for a number of secs * * ```js * Wait.waitForDefaultTimeout(20); * ``` * * @param {String} timeoutSecs in secs * */ static async waitForDefaultTimeout(timeoutSecs: number) { I.wait(timeoutSecs); } /** * Waits for an element to be clickable * * ```js * Wait.waitForElementClickable("//input[@id='login']",20); * ``` * * @param {any} locatorParam located by xpath|CSS * @param {String} timeoutSecs in secs * */ static async waitForElementClickable(locatorParam: any, timeoutSecs?: number) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } if (timeoutSecs != undefined) { console.log(`Timeout Passed To Paramater is:`, timeoutSecs); } I.waitForClickable(locator); } /** * Waits for a text to appear * * ```js * Wait.waitForVisibilityOfText("Apple",20); * ``` * * @param {String} text * @param {String} timeoutSecs in secs * */ static async waitForVisibilityOfText(text: string, timeoutSecs?: number) { if (timeoutSecs != undefined) { console.log(`Timeout Passed To Paramater is:`, timeoutSecs); } I.waitForText(text); } /** * Waits for a specified value of attribute * * ```js * Wait.waitForVisibilityOfValue("//input[@id='name']","John",20); * ``` * @param {any} locatorParam * @param {String} value "value" * @param {String} timeoutSecs in secs * */ static async waitForVisibilityOfValue(locatorParam: any, value: string, timeoutSecs?: number) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } if (timeoutSecs != undefined) { console.log(`Timeout Passed To Paramater is:`, timeoutSecs); } I.waitForValue(locator, value); } /** * Waits for an element to become enabled * * ```js * Wait.waitForElementToBeEnabled("//input[@id='login']",20); * ``` * * @param {any} locatorParam specified by CSS | Xpath * @param {String} timeoutSecs in secs * */ static async waitForElementToBeEnabled(locatorParam: any, timeoutSecs?: number) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } if (timeoutSecs != undefined) { console.log(`Timeout Passed To Paramater is:`, timeoutSecs); } I.waitForEnabled(locator); } /** * Waits for an URL to match the expected * * ```js * Wait.waitForURLPresent("eInvoice#\auth\login",20); * ``` * * @param {String} URLPart * @param {String} timeoutSecs in secs * */ static async waitForURLPresent(URLPart: string, timeoutSecs?: number) { if (timeoutSecs != undefined) { console.log(`Timeout Passed To Paramater is:`, timeoutSecs); } I.waitInUrl(URLPart); } /** * Waits for an element * * ```js * await Wait.waitForElement("//input[@id='login']"",20); * ``` * * @param {any} locatorParam specified by CSS | Xpath * @param {String} timeoutSecs in secs * */ static async waitForElement(locatorParam: any, timeoutSecs?: number) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } if (timeoutSecs != undefined) { console.log(`Timeout Passed To Paramater is:`, timeoutSecs); } I.waitForElement(locator); } /** * Waits for an element to not attached to DOM * * ```js * Wait.waitForElementToDetach("//input[@id='login']"",20); * ``` * * @param {any} locatorParam specified by CSS | Xpath * @param {String} timeoutSecs in secs * */ static async waitForElementToDetach(locatorParam: any, timeoutSecs?: number) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } if (timeoutSecs != undefined) { console.log(`Timeout Passed To Paramater is:`, timeoutSecs); } I.waitForDetached(locator); } /** * Waits for a specified number of elements * * ```js * Wait.waitForNumberOfElementsToBeVisible("//input",10,20); * ``` * * @param {any} locatorParam specified by CSS | Xpath * @param {number} numberOfElements of elements * @param {String} timeoutSecs in secs * */ static async waitForNumberOfElementsToBeVisible(locatorParam: any, numberOfElements: number, timeoutSecs?: number) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } if (timeoutSecs != undefined) { console.log(`Timeout Passed To Paramater is:`, timeoutSecs); } I.waitNumberOfVisibleElements(locator, numberOfElements); } /** * Waits for an element to hide * * ```js * Wait.waitForElementToHide(locator,20); * ``` * * @param {any} locatorParam specified as CSS | Xpath * @param {String} timeoutSecs in secs * */ static async waitForElementToHide(locatorParam: any, timeoutSecs?: number) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } if (timeoutSecs != undefined) { console.log(`Timeout Passed To Paramater is:`, timeoutSecs); } I.waitToHide(locator); } /** * Waits for the specified amount of time * for the element to load in the DOM of a page * and then returns a boolean value. * @param {string} xpath * @param {number} timeouts -default timeout is value mentioned against key "DEFAULT_HIGH_WAIT" * in config.json * @return {boolean} boolean * Wait.waitforElementPresentInDom("//button[@class='au--wc--btn btn acti']",120); */ static async waitforElementPresentInDom(xpath: string, timeouts?: number) { let locator: string; if (Array.isArray(xpath)) { locator = xpath[0]; } else { locator = xpath; this.count = 0; } return await new Promise(async (resolve) => { const timeout = await setTimeout(async () => { if (this.count < timeouts) { this.count++; } const element = await I.grabNumberOfVisibleElements((locator)) > 0; console.log(`element present in DOM = ` + element); if (element == true) { clearTimeout(timeouts); resolve(true); } else { await this.waitForDefaultTimeout(timeouts); clearTimeout(timeout); resolve(false); } }, 1000); }).catch(() => { console.log(`Error occurred in promise function`); return false; }); } } export default new Wait(); const _Wait = Wait; export { _Wait as Wait }; // for inheritance