fix(web): report a selector the engine cannot parse as INVALID_LOCATOR
Merged
DE
2d ago+83+83+01234567890123456789-1-1-0123456789
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
isSelectorParseErrorinpackages/web/src/support.tsmatches two groups of messages, taken from Playwright 1.65.1: Playwright's ownwhile parsing selectorandwhile parsing css selectorandMalformed selector:(the unknown-engine message already contains the first), and, only when Playwright'slocator.<call>:prefix is there, the browser'sis not a valid XPath expressionand JavaScript'sInvalid regular expressionforxpath=//[andtext=/(/. The prefix keeps an unrelated XPath or regex failure outside a locator call an engine fault, and a test pins that.translatePwError(packages/web/src/support.ts:371) returnsnew TestError('INVALID_LOCATOR', ...)with the raw error as itscauseahead of theENGINE_FAILUREfallback.Surface.guardwraps every operation with this translator by default, so locate, navigation, artifact calls, and theexpecttier all reach it.staleOrchecks the selector beforeSTALE_PATTERN, so alocateon a selector whose text happens to contain "not found" is still a test-code mistake and notNODE_STALE.classifyActionErrorinpackages/web/src/actions.ts:506decides the selector right afterisClassified, before every message-shaped branch, so an action whose selector text contains "not found" or "not editable" is not read as stale or unactionable. Thecauseit attaches is the same sensitive-aware variable the other branches use, so a secret fill still drops the raw Playwright error.ENGINE_ERROR_CODESstays closed: aTestErrorcrosses the engine protocol as a type-e2eerror (packages/core/src/engine/protocol/protocol.ts), so a remote engine keepsINVALID_LOCATORand itstestcategory without adding a code to the engine set.docs/reference/errors.mdxextends theINVALID_LOCATORrow with the selector forms, and the mapping table comment insupport.tsgains 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 withpackages/web/src/support.tsandactions.tsrestored tomainand the new tests in place, then with the branch. On main the two new tests fail withENGINE_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.
| main | this PR |
|---|---|
translatePwError(new Error('... while parsing selector ...'), 'locate') returns ENGINE_FAILURE, category infrastructure | INVALID_LOCATOR, category test, cause kept |
classifyActionError on the same message returns ENGINE_FAILURE | INVALID_LOCATOR, category test, cause kept |
staleOr on a parse error containing "not found" returns NODE_STALE, retryable | INVALID_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 typecheckandoxlinton 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 thatlocateappends. 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'sdebuggingreference groupsINVALID_LOCATORwithINVALID_ARGUMENTas a step-argument failure, which stays true. - Contract change: emitted
.d.tsreviewed,tests/types/sdk-types.tsupdated; wire change edits the schema and both fixtures. - Engine contract change (
e2e/engine): changesets fore2e,@e2e-dev/web, and@e2e-dev/mobile.