From 85a7cf0be58fb8945046e11d4cadeb1c60e49648 Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Tue, 17 Oct 2023 14:45:21 +0200 Subject: [PATCH] fix: move to waitFor --- lib/helper/Playwright.js | 61 +++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js index 2b84f1214..8f50a5336 100644 --- a/lib/helper/Playwright.js +++ b/lib/helper/Playwright.js @@ -2365,10 +2365,11 @@ class Playwright extends Helper { locator = new Locator(locator, 'css'); const context = await this._getContext(); - const waiter = context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'attached' }); - return waiter.catch((err) => { - throw new Error(`element (${locator.toString()}) still not present on page after ${waitTimeout / 1000} sec\n${err.message}`); - }); + try { + await context.locator(buildLocatorString(locator)).first().waitFor({ timeout: waitTimeout, state: 'attached' }); + } catch (e) { + throw new Error(`element (${locator.toString()}) still not present on page after ${waitTimeout / 1000} sec\n${e.message}`); + } } /** @@ -2380,10 +2381,11 @@ class Playwright extends Helper { const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout; locator = new Locator(locator, 'css'); const context = await this._getContext(); - const waiter = context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'visible' }); - return waiter.catch((err) => { - throw new Error(`element (${locator.toString()}) still not visible after ${waitTimeout / 1000} sec\n${err.message}`); - }); + try { + await context.locator(buildLocatorString(locator)).first().waitFor({ timeout: waitTimeout, state: 'visible' }); + } catch (e) { + throw new Error(`element (${locator.toString()}) still not visible after ${waitTimeout / 1000} sec\n${e.message}`); + } } /** @@ -2393,10 +2395,11 @@ class Playwright extends Helper { const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout; locator = new Locator(locator, 'css'); const context = await this._getContext(); - const waiter = context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'hidden' }); - return waiter.catch((err) => { - throw new Error(`element (${locator.toString()}) still visible after ${waitTimeout / 1000} sec\n${err.message}`); - }); + try { + await context.locator(buildLocatorString(locator)).first().waitFor({ timeout: waitTimeout, state: 'hidden' }); + } catch (e) { + throw new Error(`element (${locator.toString()}) still visible after ${waitTimeout / 1000} sec\n${e.message}`); + } } /** @@ -2406,7 +2409,7 @@ class Playwright extends Helper { const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout; locator = new Locator(locator, 'css'); const context = await this._getContext(); - return context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'hidden' }).catch((err) => { + return context.locator(buildLocatorString(locator)).first().waitFor({ timeout: waitTimeout, state: 'hidden' }).catch((err) => { throw new Error(`element (${locator.toString()}) still not hidden after ${waitTimeout / 1000} sec\n${err.message}`); }); } @@ -2473,7 +2476,12 @@ class Playwright extends Helper { if (context) { const locator = new Locator(context, 'css'); if (!locator.isXPath()) { - waiter = contextObject.waitForSelector(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()} >> text=${text}`, { timeout: waitTimeout, state: 'visible' }); + try { + await contextObject.locator(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()} >> text=${text}`).first().waitFor({ timeout: waitTimeout, state: 'visible' }); + } catch (e) { + console.log(e); + throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec\n${e.message}`); + } } if (locator.isXPath()) { @@ -2493,11 +2501,16 @@ class Playwright extends Helper { if (!waiter) throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec`); return; } - waiter = contextObject.waitForFunction(text => document.body && document.body.innerText.indexOf(text) > -1, text, { timeout: waitTimeout }); + + let count = 0; + do { + waiter = await contextObject.locator(`:has-text('${text}')`).first().isVisible(); + await this.wait(1); + count += 1000; + } while (count <= waitTimeout); + + if (!waiter) throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec`); } - return waiter.catch((err) => { - throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec\n${err.message}`); - }); } /** @@ -2656,17 +2669,21 @@ class Playwright extends Helper { let waiter; const context = await this._getContext(); if (!locator.isXPath()) { - waiter = context.waitForSelector(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()}`, { timeout: waitTimeout, state: 'detached' }); + try { + await context.locator(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()}`).first().waitFor({ timeout: waitTimeout, state: 'detached' }); + } catch (e) { + throw new Error(`element (${locator.toString()}) still on page after ${waitTimeout / 1000} sec\n${e.message}`); + } } else { const visibleFn = function ([locator, $XPath]) { eval($XPath); // eslint-disable-line no-eval return $XPath(null, locator).length === 0; }; waiter = context.waitForFunction(visibleFn, [locator.value, $XPath.toString()], { timeout: waitTimeout }); + return waiter.catch((err) => { + throw new Error(`element (${locator.toString()}) still on page after ${waitTimeout / 1000} sec\n${err.message}`); + }); } - return waiter.catch((err) => { - throw new Error(`element (${locator.toString()}) still on page after ${waitTimeout / 1000} sec\n${err.message}`); - }); } async _waitForAction() {