Skip to content

fix(playwright): make waitForEnabled and similar waits work inside within() - #5822

Open
cschmertz wants to merge 1 commit into
codeceptjs:4.xfrom
cschmertz:fix/wait-functions-inside-within
Open

cschmertz wants to merge 1 commit into
codeceptjs:4.xfrom
cschmertz:fix/wait-functions-inside-within

Conversation

@cschmertz

Copy link
Copy Markdown

Motivation/Description of the PR

  • Inside within(), waitForEnabled, waitForDisabled, waitForValue and waitNumberOfVisibleElements fail. They call waitForFunction on the helper's context, and inside a within block that context is a Playwright Locator (a FrameLocator inside within({ frame })), not a page. Before Playwright 1.65 the error is context.waitForFunction is not a function. 1.65 added Locator.waitForFunction, which passes the element as the first argument, so now it fails with TypeError: object is not iterable.
  • These four now run the same checks through context.locator(selector).evaluateAll(...) and poll every 100 ms until the timeout. That works for page, frame and element contexts, and only searches inside the within element, the way see and waitForVisible already do. A navigation during the wait doesn't fail it, as with waitForFunction. The checks and error messages are unchanged. That includes waitNumberOfVisibleElements treating "no matches" differently for CSS and XPath, which I left alone.
  • The selector now goes through Playwright's CSS and XPath engines instead of querySelectorAll and the injected $XPath function. So these four match the same elements click and see do (open shadow roots included), and the WebKit error from Playwright: WebKit: TypeError: $XPath is not a function. (In '$XPath(null, locator)', '$XPath' is "function $XPath(element, selector) #4749 ($XPath is not a function) no longer happens in them.
  • waitForDetached with an XPath locator and waitForText with an XPath context use the same context.waitForFunction call. I left them out to keep this focused.
  • Resolves Wrong context set when using within #4840

Applicable helpers:

  • Playwright
  • Puppeteer
  • WebDriver
  • REST
  • FileHelper
  • Appium

Applicable plugins:

  • aiTrace
  • autoDelay
  • autoLogin
  • customLocator
  • pause
  • coverage
  • heal
  • retryFailedStep
  • screenshot
  • selenoid
  • stepTimeout
  • subtitles

Type of change

  • 🔥 Breaking changes
  • 🚀 New functionality
  • 🐛 Bug fix
  • 🧹 Chore
  • 📋 Documentation changes/updates
  • ♨️ Hot fix
  • 🔨 Markdown files fix - not related to source code
  • 💅 Polish code

Checklist:

  • Tests have been added
  • Documentation has been added (Run npm run docs)
  • Lint checking (Run npm run lint)
  • Local tests are passed (Run npm test): see below

Verification:

  • 7 new tests in test/helper/Playwright_test.js and a scenario in test/acceptance/within_test.js. The within and frame tests fail on 4.x with the errors above and pass with the fix. One test navigates in the middle of a wait.
  • test/helper/Playwright_test.js on Chromium: 514 passing. The one failure, should see recording traffics, fails on 4.x too (it expects an image on codecept.io). The waitFor* tests also pass on Firefox and WebKit, where 4.x fails the existing XPath cases with the Playwright: WebKit: TypeError: $XPath is not a function. (In '$XPath(null, locator)', '$XPath' is "function $XPath(element, selector) #4749 error.
  • Acceptance @Playwright on Chromium (also with BROWSER_RESTART=browser and session), Firefox and WebKit: 125 passed, 2 skipped in each.
  • npm run lint, test:unit and test:rest pass. test:runner has 2 failures that also fail on 4.x (npm run def needs bun, and a Testomatio multi-run report test).

Written with Claude Code; I reviewed the changes and ran the full suite locally.

…thin()

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 (codeceptjs#4749)
no longer happens in these functions.

Resolves codeceptjs#4840

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong context set when using within

1 participant