Skip to content

fix: match element identifiers exactly - #68

Merged
cryptotavares merged 4 commits into
mainfrom
cryptotavares/exact-identifier-match
Dec 4, 2026
Merged

cryptotavares merged 4 commits into
mainfrom
cryptotavares/exact-identifier-match

Conversation

@cryptotavares

@cryptotavares cryptotavares commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Part of the mm CLI v2 effort. Core follow-up: MetaMask/client-mcp-core#143 (draft, needs this release).

Summary: An element query's identifier now matches only the whole, case-sensitive identifier. A test ID that is not on screen fails instead of matching another element whose ID contains it. A new ElementQuery.exact turns off the fuzzy fallback for label, text and type.

Problem

findElement tries an exact match first, then falls back to the first fuzzy match (case-insensitive equality, then contains) for every field, identifier included. When the requested element is missing (not rendered yet, scrolled away, or a partial accessibility tree), a longer test ID that contains it matches instead. waitForElement then passes, getElementText reads the wrong element, and tapElement taps it.

Live on iOS (wallet home, the accessibility tree fell back to a partial read, homepage-balance-breakdown absent; device-mcp 0.9.0, amounts sanitized):

mm wait-for --testid homepage-balance-breakdown --timeout 3000
→ {"found":true,"target":"testId:homepage-balance-breakdown"}                         exit 0
mm get-text --testid homepage-balance-breakdown --timeout 3000
→ {"text":"$<AMT>, $<AMT>, (<PCT>%), Today","target":"testId:homepage-balance-breakdown"}  exit 0
   (the text of homepage-balance-breakdown-hero)

A click with the same target would tap homepage-balance-breakdown-hero. The same applies to account-picker → account-picker-avatar, wallet-screen → wallet-screen-…, and on Android to a short ID (title) → io.metamask:id/title. The wallets under test hold real funds.

Solution

matchesQuery compares identifier with exactMatch in both passes. findElement skips the fuzzy pass when query.exact is set. The match rule stays in one function, so every caller gets it: the three backends (tapElement, longPress, getElementText), pollForElement (waitForElement) and scrollToElement.

flowchart TD
    Q["findElement(hierarchy, query)"] --> E{"exact pass:<br/>every field equal"}
    E -->|found| R[return the element]
    E -->|none| X{"query.exact?"}
    X -->|yes| N["null: Element not found / wait times out"]
    X -->|no| F{"fuzzy pass:<br/>identifier still exact,<br/>label / text / type: ignore case, contains"}
    F -->|found| R
    F -->|none| N2["null: Element not found / wait times out"]

    classDef new fill:#fff3cd,stroke:#d39e00
    classDef stop fill:#f8d7da,stroke:#c82333
    class X,F new
    class N,N2 stop
Loading

Yellow boxes are new. Red boxes stop the lookup.

What changes

Each query run against the same tree through the published 0.9.0 build and this branch's build:

Query Before After
{ identifier: 'homepage-balance-breakdown' } (only …-hero on screen) homepage-balance-breakdown-hero null
{ identifier: 'account-picker' } (both on screen) account-picker account-picker
{ identifier: 'ACCOUNT-PICKER' } account-picker-avatar null
{ identifier: 'title' } (Android io.metamask:id/title) io.metamask:id/title null
{ label: 'Send' } (only Send feedback) Send feedback Send feedback (no change)
{ label: 'Send', exact: true } Send feedback null

Decisions for reviewers

Decision Reason
identifier is always exact, with no opt-out An identifier is an ID. A partial ID match has no safe use, and every caller (core's --testid and refs, the MCP tools) means one element.
Breaking (fix!): needs a minor bump (0.x) A caller that relied on a partial or case-insensitive identifier now gets Element not found. On Android this includes a bare ID without the package:id/ prefix.
Keep fuzzy label / text / type by default The MCP tools serve humans and agents who type a label by hand (device_tap_element { label: "Confirm" }), and dismissAlert looks up its button labels with it.
exact is a library field, not an MCP tool parameter Its only caller is core, which builds ref queries from exact snapshot text. The tools keep their current schema.
Hit test unchanged hit-test.ts and android-hit-test.ts compare the hit with the target by equality and frame. They do not call matchesQuery.

Review guide

Read the files in this order:

# File What to check
1 src/utils/element.ts exactMatch on identifier in matchesQuery; the query.exact guard in findElement.
2 src/backends/types.ts ElementQuery.exact.
3 src/tools/*.ts Each tool description adds: "label, text and type match partially (case-insensitive) when no element matches exactly."
4 Tests element.test.ts (match rules), wait-for-element.test.ts (wait times out), idb-backend.test.ts and adb-backend.test.ts (no tap).
5 README.md "Element Identification": how each field matches.
Testing
  • yarn build, yarn lint and yarn test pass (990 passed, 6 skipped, after merging main). Coverage did not drop: all files 83.04% statements, element.ts 97.24%.

  • New or changed tests:

    • a missing identifier that is a substring or a prefix of another, or differs only in case → findElement returns null; the whole identifier still matches;
    • pollForElement times out when only confirm-footer and Confirm are on screen for confirm;
    • iOS tapElement({ identifier: 'homepage-balance-breakdown' }) with only …-hero on screen → Element not found, no idb ui tap;
    • Android tapElement({ identifier: 'title' }) with io.metamask:id/title on screen → Element not found, no input tap;
    • label and text keep the fuzzy fallback (send → Send feedback, RESEND → Resend); with exact: true they do not;
    • an identifier stays exact when the label falls back to a partial match.
  • Mutation check: reverting exactMatch to match for identifier, or removing the query.exact guard, fails these tests.

  • Live, iOS sim, read-only, this build linked into the Mobile app with yalc (core 0.15.0 unchanged), login screen:

    Command Before (0.9.0) After
    wait-for --testid homepage-balance-breakdown (wallet home, only …-hero present) exit 0, false pass not re-run: the relaunch after the swap locked the wallet (one unlock per session)
    wait-for --testid login-password-input — exit 0
    wait-for / get-text --testid login-password (prefix) — exit 5 MM_WAIT_TIMEOUT / exit 4 MM_GETTEXT_FAILED
    wait-for --testid log-in (prefix), LOG-IN-BUTTON (case) — exit 5 MM_WAIT_TIMEOUT
    get-text --testid log-in-button — exit 0 "Unlock"
  • Core needs fix: match mobile test IDs and refs exactly client-mcp-core#143 to pass exact: true for refs; --testid needs only this release.

Docs changed
  • README.md "Element Identification": identifier exact and case-sensitive; label, text, type exact first, then partial and case-insensitive; exact: true (library API only) turns off the partial match.

A query's identifier now matches only an element with the same whole,
case-sensitive identifier. A missing test ID no longer matches another
element whose identifier contains it. Add ElementQuery.exact to turn off
the fuzzy fallback for label, text and type.
@cryptotavares cryptotavares changed the title fix!: match element identifiers exactly fix: match element identifiers exactly Dec 4, 2026
@cryptotavares
cryptotavares merged commit 7d48e1e into main Dec 4, 2026
35 checks passed
@cryptotavares
cryptotavares deleted the cryptotavares/exact-identifier-match branch December 4, 2026 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants