diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js index 84b3eecc3..b792b8655 100644 --- a/lib/helper/Playwright.js +++ b/lib/helper/Playwright.js @@ -3327,21 +3327,9 @@ class Playwright extends Helper { const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout locator = new Locator(locator, 'css') - let waiter const context = await this._getContext() - if (!locator.isXPath()) { - const valueFn = function ([locator]) { - return Array.from(document.querySelectorAll(locator)).filter(el => !el.disabled).length > 0 - } - waiter = context.waitForFunction(valueFn, [locator.value], { timeout: waitTimeout }) - } else { - const enabledFn = function ([locator, $XPath]) { - eval($XPath) - return $XPath(null, locator).filter(el => !el.disabled).length > 0 - } - waiter = context.waitForFunction(enabledFn, [locator.value, $XPath.toString()], { timeout: waitTimeout }) - } - return waiter.catch(err => { + const enabledFn = els => els.filter(el => !el.disabled).length > 0 + return waitForElements(context, locator, enabledFn, null, waitTimeout).catch(err => { throw new Error(`element (${locator.toString()}) still not enabled after ${waitTimeout / 1000} sec\n${err.message}`) }) } @@ -3353,21 +3341,9 @@ class Playwright extends Helper { const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout locator = new Locator(locator, 'css') - let waiter const context = await this._getContext() - if (!locator.isXPath()) { - const valueFn = function ([locator]) { - return Array.from(document.querySelectorAll(locator)).filter(el => el.disabled).length > 0 - } - waiter = context.waitForFunction(valueFn, [locator.value], { timeout: waitTimeout }) - } else { - const disabledFn = function ([locator, $XPath]) { - eval($XPath) - return $XPath(null, locator).filter(el => el.disabled).length > 0 - } - waiter = context.waitForFunction(disabledFn, [locator.value, $XPath.toString()], { timeout: waitTimeout }) - } - return waiter.catch(err => { + const disabledFn = els => els.filter(el => el.disabled).length > 0 + return waitForElements(context, locator, disabledFn, null, waitTimeout).catch(err => { throw new Error(`element (${locator.toString()}) is still enabled after ${waitTimeout / 1000} sec\n${err.message}`) }) } @@ -3378,24 +3354,10 @@ class Playwright extends Helper { async waitForValue(field, value, sec) { const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout const locator = new Locator(field, 'css') - const matcher = await this.context - let waiter + const context = await this._getContext() - if (!locator.isXPath()) { - const valueFn = function ([locator, value]) { - return Array.from(document.querySelectorAll(locator)).filter(el => (el.value || '').indexOf(value) !== -1).length > 0 - } - waiter = context.waitForFunction(valueFn, [locator.value, value], { timeout: waitTimeout }) - } else { - const valueFn = function ([locator, $XPath, value]) { - eval($XPath) - return $XPath(null, locator).filter(el => (el.value || '').indexOf(value) !== -1).length > 0 - } - waiter = context.waitForFunction(valueFn, [locator.value, $XPath.toString(), value], { - timeout: waitTimeout, - }) - } - return waiter.catch(err => { + const valueFn = (els, value) => els.filter(el => (el.value || '').indexOf(value) !== -1).length > 0 + return waitForElements(context, locator, valueFn, value, waitTimeout).catch(err => { const loc = locator.toString() throw new Error(`element (${loc}) is not in DOM or there is no element(${loc}) with value "${value}" after ${waitTimeout / 1000} sec\n${err.message}`) }) @@ -3409,27 +3371,14 @@ class Playwright extends Helper { const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout locator = new Locator(locator, 'css') - let waiter const context = await this._getContext() + let visibleFn if (locator.isCSS()) { - const visibleFn = function ([locator, num]) { - const els = document.querySelectorAll(locator) - if (!els || els.length === 0) { - return false - } - return Array.prototype.filter.call(els, el => el.offsetParent !== null).length === num - } - waiter = context.waitForFunction(visibleFn, [locator.value, num], { timeout: waitTimeout }) + visibleFn = (els, num) => els.length > 0 && els.filter(el => el.offsetParent !== null).length === num } else { - const visibleFn = function ([locator, $XPath, num]) { - eval($XPath) - return $XPath(null, locator).filter(el => el.offsetParent !== null).length === num - } - waiter = context.waitForFunction(visibleFn, [locator.value, $XPath.toString(), num], { - timeout: waitTimeout, - }) + visibleFn = (els, num) => els.filter(el => el.offsetParent !== null).length === num } - return waiter.catch(err => { + return waitForElements(context, locator, visibleFn, num, waitTimeout).catch(err => { throw new Error(`The number of elements (${locator.toString()}) is not ${num} after ${waitTimeout / 1000} sec\n${err.message}`) }) } @@ -4412,6 +4361,20 @@ async function findElements(matcher, locator) { return withVisibleLocator(matcher.locator(locatorString)).all() } +async function waitForElements(context, locator, fn, arg, timeout) { + const elements = context.locator(buildLocatorString(locator)) + const deadline = Date.now() + timeout + for (;;) { + try { + if (await elements.evaluateAll(fn, arg)) return + } catch (err) { + if (!err.message.includes('Execution context was destroyed')) throw err + } + if (Date.now() >= deadline) throw new Error(`Timeout ${timeout}ms exceeded.`) + await new Promise(resolve => setTimeout(resolve, 100)) + } +} + async function findElement(matcher, locator) { if (locator.pw) return findByPlaywrightLocator.call(this, matcher, locator).first() diff --git a/test/acceptance/within_test.js b/test/acceptance/within_test.js index c0734cd13..11ab8f340 100644 --- a/test/acceptance/within_test.js +++ b/test/acceptance/within_test.js @@ -18,6 +18,19 @@ Scenario('within on form @WebDriverIO @Puppeteer @Playwright', async ({ I }) => I.dontSeeCheckboxIsChecked({ css: 'form[name=form1] input[name=first_test_radio]' }) }) +Scenario('wait functions inside within @Playwright', async ({ I }) => { + I.amOnPage('/form/wait_enabled') + await within('body', async () => { + I.waitForEnabled('#text', 2) + I.waitForEnabled("//*[@name = 'test']", 2) + }) + I.amOnPage('/info') + await within('#grab-multiple', async () => { + I.waitNumberOfVisibleElements('a', 3) + I.waitNumberOfVisibleElements('//a', 3) + }) +}) + Scenario('switch iframe manually @WebDriverIO @Puppeteer @Playwright', ({ I }) => { I.amOnPage('/iframe') diff --git a/test/helper/Playwright_test.js b/test/helper/Playwright_test.js index 4d99ba6a5..0b8a6e206 100644 --- a/test/helper/Playwright_test.js +++ b/test/helper/Playwright_test.js @@ -1029,6 +1029,12 @@ describe('Playwright', function () { .then(() => I.waitForEnabled('#text', 2)) .then(() => I.click('#button')) .then(() => I.see('button was clicked', '#message'))) + + it('should keep waiting when the page navigates', async () => { + await I.amOnPage('/info') + await I.executeScript(() => setTimeout(() => (location.href = '/form/wait_enabled'), 300)) + await I.waitForEnabled('#text', 5) + }) }) describe('#waitForDisabled', () => { @@ -1076,6 +1082,59 @@ describe('Playwright', function () { .then(() => I.seeInField('#text2', 'London'))) }) + describe('#waitForEnabled #waitForDisabled #waitForValue #waitNumberOfVisibleElements - within block', () => { + afterEach(() => I._withinEnd()) + + it('should wait for an element to be enabled', async () => { + await I.amOnPage('/form/wait_enabled') + await I._withinBegin('body') + await I.waitForEnabled('#text', 2) + await I.waitForEnabled("//*[@name = 'test']", 2) + }) + + it('should wait for an element to be disabled', async () => { + await I.amOnPage('/form/wait_disabled') + await I._withinBegin('body') + await I.waitForDisabled('#text', 1) + await I.waitForDisabled("//*[@name = 'test']", 1) + }) + + it('should wait for a value', async () => { + await I.amOnPage('/form/wait_value') + await I._withinBegin('body') + await I.waitForValue('#text', 'Brisbane', 2.5) + await I.waitForValue('//input[@name = "test"]', 'Brisbane', 2.5) + }) + + it('should wait for a number of visible elements inside the within element', async () => { + await I.amOnPage('/info') + await I._withinBegin('#grab-multiple') + await I.waitNumberOfVisibleElements('a', 3) + await I.waitNumberOfVisibleElements('//a', 3) + }) + + it('should not find elements outside the within element', async () => { + await I.amOnPage('/info') + await I._withinBegin('#grab-multiple') + let err + try { + await I.waitForValue('input[name="rus"]', 'Верно', 0.1) + } catch (e) { + err = e + } + expect(err).to.exist + expect(err.message).to.include('element (input[name="rus"]) is not in DOM or there is no element(input[name="rus"]) with value "Верно" after 0.1 sec') + }) + + it('should wait inside a frame', async () => { + await I.amOnPage('/iframe') + await I._withinBegin({ frame: '#number-frame-1234' }) + await I.waitForEnabled('input[name="rus"]') + await I.waitForValue('//input[@name = "rus"]', 'Верно') + await I.waitNumberOfVisibleElements('#grab-multiple > a', 3) + }) + }) + describe('#waitForText timeout fix', () => { it('should wait for the full timeout duration when text is not found', async function () { this.timeout(10000) // Allow up to 10 seconds for this test