fix(web): report a selector the engine cannot parse as INVALID_LOCATOR

Merged
DE
2d ago
+83-1

What

A selector the engine cannot parse now fails as INVALID_LOCATOR (category test, exit 1), through locate, count(), an action, or expect(...), instead of ENGINE_FAILURE (infrastructure, exit 3).

Why

Playwright parses a selector lazily, inside the call that first uses it, so button[type="submit", xpath=//[, nope=x, div >>, and text=/(/ arrive as ordinary errors. translatePwError and classifyActionError passed those to their ENGINE_FAILURE fallback, so a typo in test code read as an engine fault and an agent treated it as retryable infrastructure. The taxonomy already has the right code for it, and issue #2040 lists the forms a page's own tests can produce.

How it works

  1. isSelectorParseError in packages/web/src/support.ts matches two groups of messages, taken from Playwright 1.65.1: Playwright's own while parsing selector and while parsing css selector and Malformed selector: (the unknown-engine message already contains the first), and, only when Playwright's locator.<call>: prefix is there, the browser's is not a valid XPath expression and JavaScript's Invalid regular expression for xpath=//[ and text=/(/. The prefix keeps an unrelated XPath or regex failure outside a locator call an engine fault, and a test pins that.
  2. translatePwError (packages/web/src/support.ts:371) returns new TestError('INVALID_LOCATOR', ...) with the raw error as its cause ahead of the ENGINE_FAILURE fallback. Surface.guard wraps every operation with this translator by default, so locate, navigation, artifact calls, and the expect tier all reach it.
  3. staleOr checks the selector before STALE_PATTERN, so a locate on a selector whose text happens to contain "not found" is still a test-code mistake and not NODE_STALE.
  4. classifyActionError in packages/web/src/actions.ts:506 decides the selector right after isClassified, before every message-shaped branch, so an action whose selector text contains "not found" or "not editable" is not read as stale or unactionable. The cause it attaches is the same sensitive-aware variable the other branches use, so a secret fill still drops the raw Playwright error.
  5. ENGINE_ERROR_CODES stays closed: a TestError crosses the engine protocol as a type-e2e error (packages/core/src/engine/protocol/protocol.ts), so a remote engine keeps INVALID_LOCATOR and its test category without adding a code to the engine set.
  6. docs/reference/errors.mdx extends the INVALID_LOCATOR row with the selector forms, and the mapping table comment in support.ts gains the row.

Issue

Fixes #2040

Verified

Ran it locally: yes

  • pnpm --filter @e2e-dev/web exec vitest run tests/unit/errors.test.ts tests/unit/actions.test.ts, first with packages/web/src/support.ts and actions.ts restored to main and the new tests in place, then with the branch. On main the two new tests fail with ENGINE_FAILURE; here all 71 pass.
  • The five selector forms in the tests come from the installed Playwright, not from guessing, captured with page.locator(sel).count() on a blank page.
mainthis PR
translatePwError(new Error('... while parsing selector ...'), 'locate') returns ENGINE_FAILURE, category infrastructureINVALID_LOCATOR, category test, cause kept
classifyActionError on the same message returns ENGINE_FAILUREINVALID_LOCATOR, category test, cause kept
staleOr on a parse error containing "not found" returns NODE_STALE, retryableINVALID_LOCATOR, not retried
"button[" => "locator.count: Unexpected token \"\" while parsing css selector \"button[\". Did you mean to CSS.escape it?"
"xpath=//[" => "locator.count: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//[' is not a valid XPath expression."
"nope=x" => "locator.count: Unknown engine \"nope\" while parsing selector nope=x"
"div >>" => "locator.count: Unexpected token \"\" while parsing css selector \"\"."
"text=/(/" => "locator.count: SyntaxError: Invalid regular expression: /(/: Unterminated group"
AssertionError: expected EngineError: locate failed: locator.evalu... { ...(2) } to match object { code: 'INVALID_LOCATOR', ...(1) }
Tests  2 failed | 66 passed (68)
Tests  71 passed (71)
  • pnpm --filter @e2e-dev/web run typecheck and oxlint on the four changed files: clean.
  • Not benchmarked: the branch runs only on a selector parse failure, which no passing step reaches.
  • The message keeps Playwright's parse line, including the internal >> e2e-read=... selector that locate appends. Only the code, the category, and the cause change.

Checklist

  • Changeset added (pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only.
  • Breaking change: title carries !, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md).
  • Docs updated in the same PR: docs/reference/errors.mdx. The skill's debugging reference groups INVALID_LOCATOR with INVALID_ARGUMENT as a step-argument failure, which stays true.
  • Contract change: emitted .d.ts reviewed, tests/types/sdk-types.ts updated; wire change edits the schema and both fixtures.
  • Engine contract change (e2e/engine): changesets for e2e, @e2e-dev/web, and @e2e-dev/mobile.