diff --git a/docs/helpers/Playwright.md b/docs/helpers/Playwright.md index ac3d8cff7..4a84dad36 100644 --- a/docs/helpers/Playwright.md +++ b/docs/helpers/Playwright.md @@ -2729,7 +2729,8 @@ I.waitForText('Thank you, form has been submitted', 5, '#modal'); * `sec` **[number][17]** (optional, `1` by default) time in seconds to wait * `context` **([string][7] | [object][8])?** (optional) element located by CSS|XPath|strict locator. -Returns **CodeceptJS.StepPromise** automatically synchronized promise through #recorder +Returns **CodeceptJS.StepPromise** automatically synchronized promise through #recorder. **Note:** Text matching is case-sensitive. For case-insensitive matching, use XPath or CSS locators with normalized text. + ### waitForURL diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js index 4b8e88b75..d621eca17 100644 --- a/lib/helper/Playwright.js +++ b/lib/helper/Playwright.js @@ -3566,6 +3566,8 @@ class Playwright extends Helper { /** * {{> waitForText }} + * + * . **Note:** Text matching is case-sensitive. For case-insensitive matching, use XPath or CSS locators with normalized text. */ async waitForText(text, sec = null, context = null) { const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout @@ -3577,13 +3579,20 @@ 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) - }) + try { + 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) + } catch (e) { + throw new Error(errorMessage) + } } if (locator.isXPath()) { diff --git a/test/helper/webapi.js b/test/helper/webapi.js index bde263077..a260cac70 100644 --- a/test/helper/webapi.js +++ b/test/helper/webapi.js @@ -1684,6 +1684,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')