Skip to content

Commit ab84a94

Browse files
committed
fix: add role locator support and XPath handling to Puppeteer findElements/findElement
- Add role locator check in findElements to call findByRole (like Playwright) - Add role locator check in findElement to call findByRole - Fix XPath handling in findElement to use findElements (removes $x dependency) - Fixes 6 failing Puppeteer tests: - role locators with text filter (exact match case-sensitive) - role locator combinations (checkbox state) - grab elements by role - waitForClickable by XPath (3 tests) All role locator tests now pass with proper text filtering and exact matching.
1 parent d41da19 commit ab84a94

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

lib/helper/Puppeteer.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2877,6 +2877,9 @@ async function findElements(matcher, locator) {
28772877
if (isReactLocator) return findReactElements.call(this, locator)
28782878

28792879
locator = new Locator(locator, 'css')
2880+
2881+
// Check if locator is a role locator and call findByRole
2882+
if (locator.isRole()) return findByRole.call(this, matcher, locator)
28802883

28812884
// Use proven legacy approach - Puppeteer Locator API doesn't have .all() method
28822885
if (!locator.isXPath()) return matcher.$$(locator.simplify())
@@ -2934,18 +2937,22 @@ async function findElements(matcher, locator) {
29342937
async function findElement(matcher, locator) {
29352938
if (locator.react) return findReactElements.call(this, locator)
29362939
locator = new Locator(locator, 'css')
2940+
2941+
// Check if locator is a role locator and call findByRole
2942+
if (locator.isRole()) {
2943+
const elements = await findByRole.call(this, matcher, locator)
2944+
return elements[0]
2945+
}
29372946

29382947
// Use proven legacy approach - Puppeteer Locator API doesn't have .first() method
29392948
if (!locator.isXPath()) {
29402949
const elements = await matcher.$$(locator.simplify())
29412950
return elements[0]
29422951
}
2943-
// puppeteer version < 19.4.0 is no longer supported. This one is backward support.
2944-
if (puppeteer.default?.defaultBrowserRevision) {
2945-
const elements = await matcher.$$(`xpath/${locator.value}`)
2946-
return elements[0]
2947-
}
2948-
const elements = await matcher.$x(locator.value)
2952+
2953+
// For XPath in Puppeteer 24.x+, use the same approach as findElements
2954+
// $x method was removed, so we use ::-p-xpath() or fallback
2955+
const elements = await findElements.call(this, matcher, locator)
29492956
return elements[0]
29502957
}
29512958

0 commit comments

Comments
 (0)