Repository navigation
feat: support element-scoped accessibility snapshots - #3506
Conversation
| page.pptrPage.accessibility.snapshot({ | ||
| includeIframes: true, | ||
| interestingOnly: !verbose, | ||
| ...(root ? {root} : {}), |
There was a problem hiding this comment.
Would it make sense to always take full snapshot and filter it down according to the root param?
OrKoN
left a comment
There was a problem hiding this comment.
Additionally please check this:
-
Honor
verbosefor scoped snapshots (src/TextSnapshot.ts,interestingOnly: !root && !verbose). A scoped snapshot always returns the full tree, ignoresverbose, and still setsTextSnapshot.verbosetofalse, which reduces the token saving that #716 asks for. -
Fix the root cause in Puppeteer first (
Accessibility.snapshotinpuppeteer-core). WithrootandinterestingOnly: true,serializeTree(needle, interestingNodes)[0]returns only the first interesting descendant when the root is not interesting, andAGENTS.mdrequires an upstream fix instead of a local workaround. -
Take full snapshots and filter output based on the selector instead of taking partial snapshot.
-
State the selector syntax in the
selectordescription (src/tools/snapshot.ts).page.$accepts Puppeteer selectors (::-p-xpath,::-p-text,>>>), not only CSS, and the description does not tell the agent which syntax is valid. -
Reject whitespace-only selectors in the schema (
src/tools/snapshot.ts, use.trim().min(1)).min(1)accepts" ", andpage.$then throws a raw selector syntax error instead of a validation error. -
Replace the real-browser test in
tests/McpResponse.test.tswith a mock test that assertsselectorreachesTextSnapshot.create. The test runs five scenarios in one browser session, and the mock tests intests/TextSnapshot.test.tsalready cover the missing-selector and root-option branches. -
Call
findAncestorNodeone time per handle (src/TextSnapshot.ts,#insertExtraNodes). The expressionancestor || (await findAncestorNode(handle)) || rootNodeWithIdhides that the scoped and full-page paths use different call orders. -
Rebase on
mainand resolve the conflict intests/TextSnapshot.test.ts.mainchanged the extra-handle code in #3493, so the new extra-handle test must run against that version.
OrKoN
left a comment
There was a problem hiding this comment.
Please check the following:
-
Make
resolveCdpElementIdfind nodes outside the selected subtree (src/TextSnapshot.ts; useidToNode, and use a separate subtree check forselectedElementUid). The method walksthis.root, so after a scoped snapshotget_css_stylescontainer queries and issue details inget_console_messageprint no UID for those nodes, andMcpPage.resolveBackendNodeIdtakes a second full snapshot. -
Remove the
resolveCdpElementId(outsideId) === undefinedassertion inkeeps retained extra handles outside the selected subtree out of the snapshot(tests/TextSnapshot.test.ts). The assertion makes the behavior from item 1 a requirement, and it contradicts the PR description sentence "The full UID lookup remains available". -
Add mock tests for the grouped-container branch and for the
No accessibility nodes for selectorerror (tests/TextSnapshot.test.ts). Only one real-browser test reaches theelsebranch, it asserts with regular expressions on JSON, and no test reaches the error. -
Run
does not suggest verbose mode for a selected element outside the subtreewithverbose: false(tests/TextSnapshot.test.ts).SnapshotFormatterprints the note only whenverboseisfalse, so the test withverbose: truedoes not cover the case in its name. -
Move the
shadowRootstraversal infindDescendantNodesto a separate PR, or add a test and list it in the PR description (src/TextSnapshot.ts). The change also applies to#insertExtraNodes, so extra handles now re-parent shadow-DOM descendants, and no test or description text covers that. -
Throw the
DOM.describeNodefailure for the selector path instead of theNo accessibility nodeserror (src/TextSnapshot.ts,findDescendantNodes). Thecatchreturns an empty set, so a CDP failure reports a wrong cause to the agent. -
Keep
hasSelectedElementtrue when compact mode prunes a DevTools-selected element that is inside the selected subtree (src/TextSnapshot.ts,!root || Boolean(snapshot.selectedElementUid)). The expression also removes the "get a verbose snapshot" note for that element, and a verbose scoped snapshot does include it.
|
Updated in a629491. Shadow-root traversal is now limited to selector lookups, which also propagate |
Fixes #716.
take_snapshotaccepts an optional Puppeteer selector (CSS,::-p-text,::-p-xpath, or>>>) for the first matching main-frame element. The tool takes the usual full-page accessibility snapshot, including iframe documents, then filters the inline, structured, or file output to the selected subtree. Other tools can still resolve UIDs anywhere on the page. The DevTools selection marker is scoped to the output, and compact mode suggests a verbose snapshot when it prunes the selected element inside the subtree. Omitting the selector returns the full page.Scoped output respects
verbose. If compact mode prunes a container, its visible accessibility descendants are grouped under a UID-less container. A missing match or a subtree without accessibility content produces an error. Selector traversal includes shadow roots and propagates protocol errors from DOM.describeNode. Whitespace-only selectors fail schema validation.The CLI, tool reference, and tool-selection guidance document the selector. The separate fix for Puppeteer's handling of multiple promoted descendants in its rooted snapshot API is tracked in puppeteer/puppeteer#15884.