MCPcopy Index your code
hub / github.com/codeceptjs/CodeceptJS / waitForText

Method waitForText

lib/helper/Playwright.js:3501–3572  ·  view source on GitHub ↗

* {{> waitForText }}

(text, sec = null, context = null)

Source from the content-addressed store, hash-verified

3499 * {{> waitForText }}
3500 */
3501 async waitForText(text, sec = null, context = null) {
3502 const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout
3503 const errorMessage = `Text "${text}" was not found on page after ${waitTimeout / 1000} sec.`
3504
3505 const contextObject = await this._getContext()
3506
3507 if (context) {
3508 const locator = new Locator(context, 'css')
3509 try {
3510 if (!locator.isXPath()) {
3511 return contextObject
3512 .locator(`${locator.simplify()} >> text=${text}`)
3513 .first()
3514 .waitFor({ timeout: waitTimeout, state: 'visible' })
3515 .catch(e => {
3516 throw new Error(errorMessage)
3517 })
3518 }
3519
3520 if (locator.isXPath()) {
3521 return contextObject
3522 .waitForFunction(
3523 ([locator, text, $XPath]) => {
3524 eval($XPath)
3525 const el = $XPath(null, locator)
3526 if (!el.length) return false
3527 return el[0].innerText.indexOf(text) > -1
3528 },
3529 [locator.value, text, $XPath.toString()],
3530 { timeout: waitTimeout },
3531 )
3532 .catch(e => {
3533 throw new Error(errorMessage)
3534 })
3535 }
3536 } catch (e) {
3537 throw new Error(`${errorMessage}\n${e.message}`)
3538 }
3539 }
3540
3541 // Based on original implementation but fixed to check title text and remove problematic promiseRetry
3542 // Original used timeoutGap for waitForFunction to give it slightly more time than the locator
3543 const timeoutGap = waitTimeout + 1000
3544
3545 return Promise.race([
3546 // Strategy 1: waitForFunction that checks both body AND title text
3547 // Use this.page instead of contextObject because FrameLocator doesn't have waitForFunction
3548 // Original only checked document.body.innerText, missing title text like "TestEd"
3549 this.page.waitForFunction(
3550 function (text) {
3551 // Check body text (original behavior)
3552 if (document.body && document.body.innerText && document.body.innerText.indexOf(text) > -1) {
3553 return true
3554 }
3555 // Check document title (fixes the TestEd in title issue)
3556 if (document.title && document.title.indexOf(text) > -1) {
3557 return true
3558 }

Callers

nothing calls this directly

Calls 7

_getContextMethod · 0.95
isXPathMethod · 0.95
simplifyMethod · 0.95
$XPathFunction · 0.70
firstMethod · 0.45
waitForFunctionMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected