fix(webdriverio): give out-of-range indexes of a selector-less element list undefined by NUAAwqy · Pull Request #16129 · webdriverio/webdriverio · GitHub
Skip to content

fix(webdriverio): give out-of-range indexes of a selector-less element list undefined - #16129

Open
NUAAwqy wants to merge 1 commit into
webdriverio:mainfrom
NUAAwqy:fix/element-list-no-common-selector-index
Open

NUAAwqy wants to merge 1 commit into
webdriverio:mainfrom
NUAAwqy:fix/element-list-no-common-selector-index

Conversation

@NUAAwqy

@NUAAwqy NUAAwqy commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

What

Fixes #16122.

$$([elemA, elemB]) normalizes to a single selector only when every element shares one; otherwise the list has no selector and can never be re-queried. But elementAt still treated such a list as an original query: an index past the end called parent[foundWith](selector, ...props) with selector === undefined, findElements threw selector needs to be typeof `string` or `function`, but found: `undefined` inside browser.waitUntil, and the catch in elementAt rethrew it because the message is not the timeoutMsg. Reading the index without await additionally left an unhandled rejection, because readIndex returned a truthy chained proxy whose promise rejected with no handler.

The fix marks a list non-refetchable in prepareMetadata when its selector was an element array that normalizeSelector could not reduce to one selector, mirroring derivedMetadata. An index past the end then resolves to undefined (the second acceptance option in the issue, and what a slice()/filter() list already does), and readIndex returns the raw undefined for a resolved list, so there is no chained proxy and no unhandled rejection.

Lists whose elements share one selector keep refetching as before, and $$(selector) lists are untouched.

Test output (before the fix)

$ npx vitest --config vitest.config.ts --run packages/webdriverio/tests/element/array.test.ts
 ❯ packages/webdriverio/tests/element/array.test.ts (22 tests)
   × gives undefined for an index past a resolved list of elements without a common selector (#16122)
     → AssertionError: expected { Object () } to be undefined
   × gives undefined for an index past a pending list of elements without a common selector (#16122)
     → AssertionError: promise rejected "Error: selector needs to be typeof `string` or `function`, but found: `undefined`" instead of resolving

⎯⎯⎯ Unhandled Rejection ⎯⎯⎯
Error: selector needs to be typeof `string` or `function`, but found: `undefined`

 Test Files  1 failed (1)
      Tests  2 failed | 20 passed (22)

The unhandled rejection in the run above is the second reported symptom of the issue, reproduced by the unit test without any browser.

Test output (after the fix)

$ npx vitest --config vitest.config.ts --run packages/webdriverio/tests/element/array.test.ts
 ✓ packages/webdriverio/tests/element/array.test.ts (22 tests) 40ms
 Test Files  1 passed (1)
      Tests  22 passed (22)

$ npx vitest --config vitest.config.ts --run packages/webdriverio/tests
 Test Files  212 passed (212)
      Tests  3491 passed | 41 skipped (3532)
Type Errors  no errors

Notes

  • The regression tests cover the resolved list, the pending list, and a guard test asserting that a list of elements that share one selector still waits and refetches.
  • No behavior change for string/function-selector lists; the change is localized to prepareMetadata in packages/webdriverio/src/element/array.ts.

AI assistance was used to draft this change; the code and tests were verified by hand.

…t list undefined

A $$([elemA, elemB]) list whose elements come from different selectors has
no selector to run again, but an index past its end still re-ran $$ with an
undefined selector and surfaced its error inside waitUntil (webdriverio#16122). Mark
such lists non-refetchable in prepareMetadata, like derived lists, so an
index past the end resolves to undefined and reading it without awaiting no
longer leaves an unhandled rejection.
@NUAAwqy
NUAAwqy requested a review from a team as a code owner October 18, 2026 11:06
@wdio-bot wdio-bot added the PR: Bug Fix 🐛 PRs that contain bug fixes label Oct 18, 2026
@greptile-apps

greptile-apps Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

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

PR: Bug Fix 🐛 PRs that contain bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[🐛 Bug]: An index past the end of a $$([a, b]) list with mixed selectors throws a selector error and leaves an unhandled rejection

2 participants