Skip to content

Commit efe3c10

Browse files
authored
fix(playwright): make waitForEnabled and similar waits work inside within() (#5822)
waitForEnabled, waitForDisabled, waitForValue and waitNumberOfVisibleElements called waitForFunction on the helper context, which is a Locator inside within() and a FrameLocator inside within({ frame }). They now poll context.locator(selector).evaluateAll(...) until the timeout, so they work in page, frame and element contexts and only search inside the within element. A navigation during the wait is retried. The selector goes through Playwright's engines, so the WebKit "$XPath is not a function" error (#4749) no longer happens in these functions. Resolves #4840
1 parent 3378016 commit efe3c10

3 files changed

Lines changed: 97 additions & 62 deletions

File tree

‎lib/helper/Playwright.js‎

Lines changed: 25 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3327,21 +3327,9 @@ class Playwright extends Helper {
33273327
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout
33283328
locator = new Locator(locator, 'css')
33293329

3330-
let waiter
33313330
const context = await this._getContext()
3332-
if (!locator.isXPath()) {
3333-
const valueFn = function ([locator]) {
3334-
return Array.from(document.querySelectorAll(locator)).filter(el => !el.disabled).length > 0
3335-
}
3336-
waiter = context.waitForFunction(valueFn, [locator.value], { timeout: waitTimeout })
3337-
} else {
3338-
const enabledFn = function ([locator, $XPath]) {
3339-
eval($XPath)
3340-
return $XPath(null, locator).filter(el => !el.disabled).length > 0
3341-
}
3342-
waiter = context.waitForFunction(enabledFn, [locator.value, $XPath.toString()], { timeout: waitTimeout })
3343-
}
3344-
return waiter.catch(err => {
3331+
const enabledFn = els => els.filter(el => !el.disabled).length > 0
3332+
return waitForElements(context, locator, enabledFn, null, waitTimeout).catch(err => {
33453333
throw new Error(`element (${locator.toString()}) still not enabled after ${waitTimeout / 1000} sec\n${err.message}`)
33463334
})
33473335
}
@@ -3353,21 +3341,9 @@ class Playwright extends Helper {
33533341
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout
33543342
locator = new Locator(locator, 'css')
33553343

3356-
let waiter
33573344
const context = await this._getContext()
3358-
if (!locator.isXPath()) {
3359-
const valueFn = function ([locator]) {
3360-
return Array.from(document.querySelectorAll(locator)).filter(el => el.disabled).length > 0
3361-
}
3362-
waiter = context.waitForFunction(valueFn, [locator.value], { timeout: waitTimeout })
3363-
} else {
3364-
const disabledFn = function ([locator, $XPath]) {
3365-
eval($XPath)
3366-
return $XPath(null, locator).filter(el => el.disabled).length > 0
3367-
}
3368-
waiter = context.waitForFunction(disabledFn, [locator.value, $XPath.toString()], { timeout: waitTimeout })
3369-
}
3370-
return waiter.catch(err => {
3345+
const disabledFn = els => els.filter(el => el.disabled).length > 0
3346+
return waitForElements(context, locator, disabledFn, null, waitTimeout).catch(err => {
33713347
throw new Error(`element (${locator.toString()}) is still enabled after ${waitTimeout / 1000} sec\n${err.message}`)
33723348
})
33733349
}
@@ -3378,24 +3354,10 @@ class Playwright extends Helper {
33783354
async waitForValue(field, value, sec) {
33793355
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout
33803356
const locator = new Locator(field, 'css')
3381-
const matcher = await this.context
3382-
let waiter
3357+
33833358
const context = await this._getContext()
3384-
if (!locator.isXPath()) {
3385-
const valueFn = function ([locator, value]) {
3386-
return Array.from(document.querySelectorAll(locator)).filter(el => (el.value || '').indexOf(value) !== -1).length > 0
3387-
}
3388-
waiter = context.waitForFunction(valueFn, [locator.value, value], { timeout: waitTimeout })
3389-
} else {
3390-
const valueFn = function ([locator, $XPath, value]) {
3391-
eval($XPath)
3392-
return $XPath(null, locator).filter(el => (el.value || '').indexOf(value) !== -1).length > 0
3393-
}
3394-
waiter = context.waitForFunction(valueFn, [locator.value, $XPath.toString(), value], {
3395-
timeout: waitTimeout,
3396-
})
3397-
}
3398-
return waiter.catch(err => {
3359+
const valueFn = (els, value) => els.filter(el => (el.value || '').indexOf(value) !== -1).length > 0
3360+
return waitForElements(context, locator, valueFn, value, waitTimeout).catch(err => {
33993361
const loc = locator.toString()
34003362
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}`)
34013363
})
@@ -3409,27 +3371,14 @@ class Playwright extends Helper {
34093371
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout
34103372
locator = new Locator(locator, 'css')
34113373

3412-
let waiter
34133374
const context = await this._getContext()
3375+
let visibleFn
34143376
if (locator.isCSS()) {
3415-
const visibleFn = function ([locator, num]) {
3416-
const els = document.querySelectorAll(locator)
3417-
if (!els || els.length === 0) {
3418-
return false
3419-
}
3420-
return Array.prototype.filter.call(els, el => el.offsetParent !== null).length === num
3421-
}
3422-
waiter = context.waitForFunction(visibleFn, [locator.value, num], { timeout: waitTimeout })
3377+
visibleFn = (els, num) => els.length > 0 && els.filter(el => el.offsetParent !== null).length === num
34233378
} else {
3424-
const visibleFn = function ([locator, $XPath, num]) {
3425-
eval($XPath)
3426-
return $XPath(null, locator).filter(el => el.offsetParent !== null).length === num
3427-
}
3428-
waiter = context.waitForFunction(visibleFn, [locator.value, $XPath.toString(), num], {
3429-
timeout: waitTimeout,
3430-
})
3379+
visibleFn = (els, num) => els.filter(el => el.offsetParent !== null).length === num
34313380
}
3432-
return waiter.catch(err => {
3381+
return waitForElements(context, locator, visibleFn, num, waitTimeout).catch(err => {
34333382
throw new Error(`The number of elements (${locator.toString()}) is not ${num} after ${waitTimeout / 1000} sec\n${err.message}`)
34343383
})
34353384
}
@@ -4412,6 +4361,20 @@ async function findElements(matcher, locator) {
44124361
return withVisibleLocator(matcher.locator(locatorString)).all()
44134362
}
44144363

4364+
async function waitForElements(context, locator, fn, arg, timeout) {
4365+
const elements = context.locator(buildLocatorString(locator))
4366+
const deadline = Date.now() + timeout
4367+
for (;;) {
4368+
try {
4369+
if (await elements.evaluateAll(fn, arg)) return
4370+
} catch (err) {
4371+
if (!err.message.includes('Execution context was destroyed')) throw err
4372+
}
4373+
if (Date.now() >= deadline) throw new Error(`Timeout ${timeout}ms exceeded.`)
4374+
await new Promise(resolve => setTimeout(resolve, 100))
4375+
}
4376+
}
4377+
44154378
async function findElement(matcher, locator) {
44164379
if (locator.pw) return findByPlaywrightLocator.call(this, matcher, locator).first()
44174380

‎test/acceptance/within_test.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ Scenario('within on form @WebDriverIO @Puppeteer @Playwright', async ({ I }) =>
1818
I.dontSeeCheckboxIsChecked({ css: 'form[name=form1] input[name=first_test_radio]' })
1919
})
2020

21+
Scenario('wait functions inside within @Playwright', async ({ I }) => {
22+
I.amOnPage('/form/wait_enabled')
23+
await within('body', async () => {
24+
I.waitForEnabled('#text', 2)
25+
I.waitForEnabled("//*[@name = 'test']", 2)
26+
})
27+
I.amOnPage('/info')
28+
await within('#grab-multiple', async () => {
29+
I.waitNumberOfVisibleElements('a', 3)
30+
I.waitNumberOfVisibleElements('//a', 3)
31+
})
32+
})
33+
2134
Scenario('switch iframe manually @WebDriverIO @Puppeteer @Playwright', ({ I }) => {
2235
I.amOnPage('/iframe')
2336

‎test/helper/Playwright_test.js‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,12 @@ describe('Playwright', function () {
10291029
.then(() => I.waitForEnabled('#text', 2))
10301030
.then(() => I.click('#button'))
10311031
.then(() => I.see('button was clicked', '#message')))
1032+
1033+
it('should keep waiting when the page navigates', async () => {
1034+
await I.amOnPage('/info')
1035+
await I.executeScript(() => setTimeout(() => (location.href = '/form/wait_enabled'), 300))
1036+
await I.waitForEnabled('#text', 5)
1037+
})
10321038
})
10331039

10341040
describe('#waitForDisabled', () => {
@@ -1076,6 +1082,59 @@ describe('Playwright', function () {
10761082
.then(() => I.seeInField('#text2', 'London')))
10771083
})
10781084

1085+
describe('#waitForEnabled #waitForDisabled #waitForValue #waitNumberOfVisibleElements - within block', () => {
1086+
afterEach(() => I._withinEnd())
1087+
1088+
it('should wait for an element to be enabled', async () => {
1089+
await I.amOnPage('/form/wait_enabled')
1090+
await I._withinBegin('body')
1091+
await I.waitForEnabled('#text', 2)
1092+
await I.waitForEnabled("//*[@name = 'test']", 2)
1093+
})
1094+
1095+
it('should wait for an element to be disabled', async () => {
1096+
await I.amOnPage('/form/wait_disabled')
1097+
await I._withinBegin('body')
1098+
await I.waitForDisabled('#text', 1)
1099+
await I.waitForDisabled("//*[@name = 'test']", 1)
1100+
})
1101+
1102+
it('should wait for a value', async () => {
1103+
await I.amOnPage('/form/wait_value')
1104+
await I._withinBegin('body')
1105+
await I.waitForValue('#text', 'Brisbane', 2.5)
1106+
await I.waitForValue('//input[@name = "test"]', 'Brisbane', 2.5)
1107+
})
1108+
1109+
it('should wait for a number of visible elements inside the within element', async () => {
1110+
await I.amOnPage('/info')
1111+
await I._withinBegin('#grab-multiple')
1112+
await I.waitNumberOfVisibleElements('a', 3)
1113+
await I.waitNumberOfVisibleElements('//a', 3)
1114+
})
1115+
1116+
it('should not find elements outside the within element', async () => {
1117+
await I.amOnPage('/info')
1118+
await I._withinBegin('#grab-multiple')
1119+
let err
1120+
try {
1121+
await I.waitForValue('input[name="rus"]', 'Верно', 0.1)
1122+
} catch (e) {
1123+
err = e
1124+
}
1125+
expect(err).to.exist
1126+
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')
1127+
})
1128+
1129+
it('should wait inside a frame', async () => {
1130+
await I.amOnPage('/iframe')
1131+
await I._withinBegin({ frame: '#number-frame-1234' })
1132+
await I.waitForEnabled('input[name="rus"]')
1133+
await I.waitForValue('//input[@name = "rus"]', 'Верно')
1134+
await I.waitNumberOfVisibleElements('#grab-multiple > a', 3)
1135+
})
1136+
})
1137+
10791138
describe('#waitForText timeout fix', () => {
10801139
it('should wait for the full timeout duration when text is not found', async function () {
10811140
this.timeout(10000) // Allow up to 10 seconds for this test

0 commit comments

Comments
 (0)