diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js index 3f7285f53..9fb962006 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,10 @@ class Playwright extends Helper { const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout; locator = new Locator(locator, 'css'); const context = await this._getContext(); - let waiter; let count = 0; // we have this as https://github.com/microsoft/playwright/issues/26829 is not yet implemented + let waiter; if (this.frame) { do { waiter = await this.frame.locator(buildLocatorString(locator)).first().isVisible(); @@ -2393,13 +2394,13 @@ class Playwright extends Helper { } while (count <= waitTimeout); if (!waiter) throw new Error(`element (${locator.toString()}) still not visible after ${waitTimeout / 1000} sec.`); - return; } - 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}`); + } } /** @@ -2425,10 +2426,11 @@ class Playwright extends Helper { return; } - 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}`); + } } /** @@ -2438,7 +2440,6 @@ class Playwright extends Helper { const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout; locator = new Locator(locator, 'css'); const context = await this._getContext(); - let waiter; let count = 0; @@ -2455,7 +2456,7 @@ class Playwright extends Helper { return; } - 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}`); }); } @@ -2522,7 +2523,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()) { @@ -2535,23 +2541,17 @@ class Playwright extends Helper { } } else { // we have this as https://github.com/microsoft/playwright/issues/26829 is not yet implemented - if (this.frame) { - let count = 0; - do { - waiter = await this.frame.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; - } + // eslint-disable-next-line no-lonely-if + const _contextObject = this.frame ? this.frame : contextObject; + let count = 0; + do { + waiter = await _contextObject.locator(`:has-text('${text}')`).first().isVisible(); + await this.wait(1); + count += 1000; + } while (count <= waitTimeout); - waiter = contextObject.waitForFunction(text => document.body && document.body.innerText.indexOf(text) > -1, text, { timeout: 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}`); - }); } /** @@ -2710,17 +2710,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() {