diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js index 6cfcf420a..1bc4c7737 100644 --- a/lib/helper/Playwright.js +++ b/lib/helper/Playwright.js @@ -3576,13 +3576,16 @@ class Playwright extends Helper { const locator = new Locator(context, 'css') try { if (!locator.isXPath()) { - return contextObject - .locator(`${locator.simplify()} >> text=${text}`) - .first() - .waitFor({ timeout: waitTimeout, state: 'visible' }) - .catch(e => { - throw new Error(errorMessage) - }) + const deadline = Date.now() + waitTimeout + do { + const els = await this._locate(locator) + for (const el of els) { + const texts = await el.evaluateAll(nodes => nodes.map(node => node.innerText ?? node.textContent)) + if (texts.some(t => typeof t === 'string' && t.includes(text))) return + } + await new Promise(resolve => setTimeout(resolve, 100)) + } while (Date.now() < deadline) + throw new Error(errorMessage) } if (locator.isXPath()) { diff --git a/test/helper/webapi.js b/test/helper/webapi.js index 9f1b08ad9..ad3acb105 100644 --- a/test/helper/webapi.js +++ b/test/helper/webapi.js @@ -1678,6 +1678,11 @@ export function tests() { await I.see('Dynamic text') }) + it('should wait for text in context with locator.at', async () => { + await I.amOnPage('/dynamic') + await I.waitForText('Dynamic text', 2, new Locator('#text').at(1)) + }) + it('should fail if no context', async function () { let failed = false await I.amOnPage('/dynamic')