Repository navigation
fix: match element identifiers exactly - #68
Merged
Merged
Conversation
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.
…atch' into cryptotavares/exact-identifier-match
pnarayanaswamy
approved these changes
Dec 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of the mm CLI v2 effort. Core follow-up: MetaMask/client-mcp-core#143 (draft, needs this release).
Problem
findElementtries an exact match first, then falls back to the first fuzzy match (case-insensitive equality, then contains) for every field,identifierincluded. 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.waitForElementthen passes,getElementTextreads the wrong element, andtapElementtaps it.Live on iOS (wallet home, the accessibility tree fell back to a partial read,
homepage-balance-breakdownabsent; device-mcp 0.9.0, amounts sanitized):A
clickwith the same target would taphomepage-balance-breakdown-hero. The same applies toaccount-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
matchesQuerycomparesidentifierwithexactMatchin both passes.findElementskips the fuzzy pass whenquery.exactis set. The match rule stays in one function, so every caller gets it: the three backends (tapElement,longPress,getElementText),pollForElement(waitForElement) andscrollToElement.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 stopYellow 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:
{ identifier: 'homepage-balance-breakdown' }(only…-heroon screen)homepage-balance-breakdown-heronull{ identifier: 'account-picker' }(both on screen)account-pickeraccount-picker{ identifier: 'ACCOUNT-PICKER' }account-picker-avatarnull{ identifier: 'title' }(Androidio.metamask:id/title)io.metamask:id/titlenull{ label: 'Send' }(onlySend feedback)Send feedbackSend feedback(no change){ label: 'Send', exact: true }Send feedbacknullDecisions for reviewers
identifieris always exact, with no opt-out--testidand refs, the MCP tools) means one element.fix!): needs a minor bump (0.x)Element not found. On Android this includes a bare ID without thepackage:id/prefix.label/text/typeby defaultdevice_tap_element { label: "Confirm" }), anddismissAlertlooks up its button labels with it.exactis a library field, not an MCP tool parameterhit-test.tsandandroid-hit-test.tscompare the hit with the target by equality and frame. They do not callmatchesQuery.Review guide
Read the files in this order:
src/utils/element.tsexactMatchonidentifierinmatchesQuery; thequery.exactguard infindElement.src/backends/types.tsElementQuery.exact.src/tools/*.tslabel,textandtypematch partially (case-insensitive) when no element matches exactly."element.test.ts(match rules),wait-for-element.test.ts(wait times out),idb-backend.test.tsandadb-backend.test.ts(no tap).README.mdTesting
yarn build,yarn lintandyarn testpass (990 passed, 6 skipped, after mergingmain). Coverage did not drop: all files 83.04% statements,element.ts97.24%.New or changed tests:
findElementreturnsnull; the whole identifier still matches;pollForElementtimes out when onlyconfirm-footerandConfirmare on screen forconfirm;tapElement({ identifier: 'homepage-balance-breakdown' })with only…-heroon screen →Element not found, noidb ui tap;tapElement({ identifier: 'title' })withio.metamask:id/titleon screen →Element not found, noinput tap;send→Send feedback,RESEND→Resend); withexact: truethey do not;Mutation check: reverting
exactMatchtomatchforidentifier, or removing thequery.exactguard, fails these tests.Live, iOS sim, read-only, this build linked into the Mobile app with yalc (core 0.15.0 unchanged), login screen:
wait-for --testid homepage-balance-breakdown(wallet home, only…-heropresent)wait-for --testid login-password-inputwait-for/get-text --testid login-password(prefix)MM_WAIT_TIMEOUT/ exit 4MM_GETTEXT_FAILEDwait-for --testid log-in(prefix),LOG-IN-BUTTON(case)MM_WAIT_TIMEOUTget-text --testid log-in-button"Unlock"Core needs fix: match mobile test IDs and refs exactly client-mcp-core#143 to pass
exact: truefor refs;--testidneeds only this release.Docs changed
README.md"Element Identification":identifierexact and case-sensitive;label,text,typeexact first, then partial and case-insensitive;exact: true(library API only) turns off the partial match.