Skip to content

fix: make XPath relative in buildLocatorString for within() scope#5474

Open
mirao wants to merge 1 commit intocodeceptjs:4.xfrom
mirao:fix/xpath-within-scope-5473
Open

fix: make XPath relative in buildLocatorString for within() scope#5474
mirao wants to merge 1 commit intocodeceptjs:4.xfrom
mirao:fix/xpath-within-scope-5473

Conversation

@mirao
Copy link
Contributor

@mirao mirao commented Mar 5, 2026

Summary

  • Make XPath relative in buildLocatorString() so locate().at(), .first(), .last() work correctly inside within() with Playwright helper
  • Playwright's XPath engine auto-converts //... to .//... when the root is not a Document, but only when the selector starts with /. Locator methods like at() wrap XPath in parentheses (e.g. (//...)[position()=1]), bypassing that auto-conversion
  • The fix prepends . before the first // that follows any leading parentheses

Fixes #5473

Test plan

  • Unit tests for buildLocatorString covering at(), first(), last(), at().find(), plain XPath, already-relative XPath, and CSS passthrough
  • All 336 Playwright helper integration tests pass (1 pre-existing failure in #seeAttributesOnElements unrelated to this change)

🤖 Generated with Claude Code

…deceptjs#5473)

Playwright's XPath engine auto-converts "//..." to ".//..." when searching
within an element, but only when the selector starts with "/". Locator methods
like at(), first(), last() wrap XPath in parentheses (e.g. "(//...)[position()=1]"),
bypassing that auto-conversion and causing XPath to search from the document root
instead of the within() scope.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

1 participant