From bf7d41786cd8bfe28c1afaabdfe46fa9fa6f2109 Mon Sep 17 00:00:00 2001 From: cryptotavares Date: Thu, 3 Dec 2026 15:23:16 +0000 Subject: [PATCH 1/2] fix!: match element identifiers exactly 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. --- README.md | 6 ++- src/backends/adb-backend.test.ts | 5 +++ src/backends/idb-backend.test.ts | 21 ++++++++++ src/backends/types.ts | 3 ++ src/tools/get-element-text.ts | 4 +- src/tools/long-press.ts | 5 ++- src/tools/scroll-to-element.ts | 2 +- src/tools/tap-element.ts | 4 +- src/tools/wait-for.ts | 2 +- src/utils/element.test.ts | 67 +++++++++++++++++++++++++----- src/utils/element.ts | 18 +++++--- src/utils/wait-for-element.test.ts | 13 ++++++ 12 files changed, 127 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index b43bc23..f3b09a1 100644 --- a/README.md +++ b/README.md @@ -206,7 +206,11 @@ The `.device-session` file is typically written by the test runner when it creat ### Element Identification -Elements are identified by accessibility attributes — not internal refs. Matching is **fuzzy**: partial text and case-insensitive matches work. For example, querying `{ label: "Confirm" }` matches an element with label `"Confirm Transaction"`. An exact match anywhere on screen wins over a partial one: `{ identifier: "account-picker" }` matches `account-picker`, not an earlier `account-picker-avatar`. +Elements are identified by accessibility attributes — not internal refs. Every field given in the query must match: + +- `identifier` matches **exactly**: the whole string, case-sensitive. `{ identifier: "account-picker" }` never matches `account-picker-avatar`; when no element has that identifier, the query finds nothing (taps fail with `Element not found`, waits time out). On Android, use the whole resource-id (`io.metamask:id/title`, not `title`). +- `label`, `text` (the element's value or label) and `type` match **fuzzily**: an exact match anywhere on screen wins; otherwise the first element whose field equals the query ignoring case, or contains it, matches. For example, `{ label: "Confirm" }` matches an element with label `"Confirm Transaction"` when no element is labelled exactly `"Confirm"`. +- `exact: true` (library API, not an MCP tool parameter) turns off the fuzzy fallback: `label`, `text` and `type` must then equal the element's value too. - **iOS**: accessibility label, accessibility identifier - **Android**: content-description, resource-id, text diff --git a/src/backends/adb-backend.test.ts b/src/backends/adb-backend.test.ts index eaff870..c3001b0 100644 --- a/src/backends/adb-backend.test.ts +++ b/src/backends/adb-backend.test.ts @@ -863,6 +863,11 @@ describe('AdbBackend.tapElement', () => { await expect( backend.tapElement({ identifier: 'not-on-screen' }), ).rejects.toThrow('Element not found'); + // `io.metamask:id/title` is on screen: only its whole resource-id matches. + await expect(backend.tapElement({ identifier: 'title' })).rejects.toThrow( + 'Element not found', + ); + expect(tapCalls()).toHaveLength(0); }); }); diff --git a/src/backends/idb-backend.test.ts b/src/backends/idb-backend.test.ts index 8013005..fda89ee 100644 --- a/src/backends/idb-backend.test.ts +++ b/src/backends/idb-backend.test.ts @@ -883,6 +883,27 @@ describe('IdbBackend.tapElement and longPress viewport handling', () => { ); }); + it('does not tap an element whose identifier only contains the requested one', async () => { + mockScreen([ + { + type: 'Button', + AXLabel: 'Balance', + identifier: 'homepage-balance-breakdown-hero', + frame: { x: 10, y: 20, width: 100, height: 44 }, + enabled: true, + }, + ]); + + await expect( + backend.tapElement({ identifier: 'homepage-balance-breakdown' }), + ).rejects.toThrow('Element not found'); + expect(mockExecStrict).not.toHaveBeenCalledWith( + '/usr/local/bin/idb', + expect.arrayContaining(['ui', 'tap']), + connectionOptions, + ); + }); + it('taps the center of the visible part of a partially scrolled element', async () => { mockScreen([ { diff --git a/src/backends/types.ts b/src/backends/types.ts index 2b79ff0..496fdba 100644 --- a/src/backends/types.ts +++ b/src/backends/types.ts @@ -107,9 +107,12 @@ export type LogsResult = { export type ElementQuery = { label?: string; + /** Always matched exactly: the whole string, case-sensitive. */ identifier?: string; text?: string; type?: string; + /** Match `label`, `text` and `type` exactly too, with no partial fallback. */ + exact?: boolean; }; export type DeviceButton = 'home' | 'back' | 'enter' | 'lock'; diff --git a/src/tools/get-element-text.ts b/src/tools/get-element-text.ts index 6cc53a8..e91ba77 100644 --- a/src/tools/get-element-text.ts +++ b/src/tools/get-element-text.ts @@ -26,7 +26,9 @@ export function registerGetElementTextTool( identifier: z .string() .optional() - .describe('Accessibility identifier (iOS) or resource-id (Android)'), + .describe( + 'Accessibility identifier (iOS) or resource-id (Android), matched exactly', + ), text: z.string().optional().describe('Visible text content'), type: z.string().optional().describe('Element type/class name'), }, diff --git a/src/tools/long-press.ts b/src/tools/long-press.ts index 6efc3a8..50abaed 100644 --- a/src/tools/long-press.ts +++ b/src/tools/long-press.ts @@ -17,7 +17,10 @@ export function registerLongPressTool( 'and other long-press interactions. Default duration is 1000ms.', inputSchema: { label: z.string().optional().describe('Accessibility label'), - identifier: z.string().optional().describe('Identifier/resource-id'), + identifier: z + .string() + .optional() + .describe('Identifier/resource-id, matched exactly'), text: z.string().optional().describe('Text content'), type: z.string().optional().describe('Element type'), durationMs: z diff --git a/src/tools/scroll-to-element.ts b/src/tools/scroll-to-element.ts index a8726f9..dd3d0c8 100644 --- a/src/tools/scroll-to-element.ts +++ b/src/tools/scroll-to-element.ts @@ -23,7 +23,7 @@ export function registerScrollToElementTool( identifier: z .string() .optional() - .describe('Identifier/resource-id to match'), + .describe('Identifier/resource-id to match exactly'), text: z.string().optional().describe('Text content to match'), type: z.string().optional().describe('Element type to match'), direction: z diff --git a/src/tools/tap-element.ts b/src/tools/tap-element.ts index 32be4d7..4b35ebd 100644 --- a/src/tools/tap-element.ts +++ b/src/tools/tap-element.ts @@ -26,7 +26,9 @@ export function registerTapElementTool( identifier: z .string() .optional() - .describe('Accessibility identifier (iOS) or resource-id (Android)'), + .describe( + 'Accessibility identifier (iOS) or resource-id (Android), matched exactly', + ), text: z.string().optional().describe('Visible text content'), type: z.string().optional().describe('Element type/class name'), }, diff --git a/src/tools/wait-for.ts b/src/tools/wait-for.ts index dfcd948..a0f7e4c 100644 --- a/src/tools/wait-for.ts +++ b/src/tools/wait-for.ts @@ -21,7 +21,7 @@ export function registerWaitForTool( identifier: z .string() .optional() - .describe('Identifier/resource-id to match'), + .describe('Identifier/resource-id to match exactly'), text: z.string().optional().describe('Text content to match'), type: z.string().optional().describe('Element type to match'), timeoutMs: z diff --git a/src/utils/element.test.ts b/src/utils/element.test.ts index 23b6396..fce286e 100644 --- a/src/utils/element.test.ts +++ b/src/utils/element.test.ts @@ -62,9 +62,15 @@ describe('matchesQuery', () => { expect(matchesQuery(el, { label: 'confirm trans' })).toBe(true); }); - it('matches partial identifier', () => { + it('matches the identifier exactly and case-sensitively', () => { const el = makeElement({ identifier: 'io.metamask:id/submit-btn' }); - expect(matchesQuery(el, { identifier: 'submit-btn' })).toBe(true); + expect(matchesQuery(el, { identifier: 'io.metamask:id/submit-btn' })).toBe( + true, + ); + expect(matchesQuery(el, { identifier: 'submit-btn' })).toBe(false); + expect(matchesQuery(el, { identifier: 'IO.METAMASK:ID/SUBMIT-BTN' })).toBe( + false, + ); }); it('matches only equal strings when exact', () => { @@ -135,28 +141,67 @@ describe('findElement', () => { expect(findElement(elements, { text: 'Send' })?.value).toBe('Send'); }); - it('falls back to the first partial match when nothing matches exactly', () => { + it('falls back to the first partial label or text match when nothing matches exactly', () => { const elements = [ + makeElement({ label: 'Send feedback' }), + makeElement({ label: 'Resend', value: 'Resend' }), + ]; + expect(findElement(elements, { label: 'send' })?.label).toBe( + 'Send feedback', + ); + expect(findElement(elements, { text: 'RESEND' })?.label).toBe('Resend'); + }); + + it('never matches a missing identifier to one that contains it', () => { + const elements = [ + makeElement({ identifier: 'homepage-balance-breakdown-hero' }), makeElement({ identifier: 'io.metamask:id/submit-btn' }), - makeElement({ identifier: 'submit-btn-2' }), + makeElement({ identifier: 'account-picker-avatar' }), ]; expect( - findElement(elements, { identifier: 'submit-btn' })?.identifier, - ).toBe('io.metamask:id/submit-btn'); + findElement(elements, { identifier: 'homepage-balance-breakdown' }), + ).toBeNull(); + expect(findElement(elements, { identifier: 'submit-btn' })).toBeNull(); + expect(findElement(elements, { identifier: 'account-picker' })).toBeNull(); + expect( + findElement(elements, { identifier: 'ACCOUNT-PICKER-AVATAR' }), + ).toBeNull(); expect( - findElement(elements, { identifier: 'SUBMIT-BTN-2' })?.identifier, - ).toBe('submit-btn-2'); + findElement(elements, { identifier: 'account-picker-avatar' }) + ?.identifier, + ).toBe('account-picker-avatar'); }); - it('requires every field to match exactly for the exact pass', () => { + it('keeps the identifier exact when the label falls back to a partial match', () => { const elements = [ makeElement({ identifier: 'btn-long', label: 'Go' }), makeElement({ identifier: 'btn', label: 'Go now' }), ]; - // No element matches both fields exactly, so the first fuzzy match wins. expect( findElement(elements, { identifier: 'btn', label: 'Go' })?.identifier, - ).toBe('btn-long'); + ).toBe('btn'); + }); + + it('has no partial fallback when the query is exact', () => { + const elements = [ + makeElement({ type: 'StaticText', label: 'Send feedback' }), + makeElement({ type: 'Button', label: 'Send', value: '2' }), + ]; + expect( + findElement(elements, { label: 'Send', type: 'Button', exact: true }) + ?.value, + ).toBe('2'); + expect(findElement(elements, { label: 'Send', exact: true })?.value).toBe( + '2', + ); + expect( + findElement(elements, { label: 'send', type: 'Button', exact: true }), + ).toBeNull(); + expect( + findElement(elements, { label: 'Send', type: 'StaticText', exact: true }), + ).toBeNull(); + expect(findElement(elements, { text: 'feedback', exact: true })).toBeNull(); + expect(findElement(elements, { type: 'Butt', exact: true })).toBeNull(); }); it('returns null when not found', () => { diff --git a/src/utils/element.ts b/src/utils/element.ts index ffb20a5..0b9e88b 100644 --- a/src/utils/element.ts +++ b/src/utils/element.ts @@ -2,8 +2,9 @@ import type { UIElement, ElementQuery } from '../backends/types.js'; /** * Finds the element that matches the query. An exact match anywhere in the - * tree wins over an earlier fuzzy one (`account-picker` must not return - * `account-picker-avatar`); otherwise the first fuzzy match is returned. + * tree wins over an earlier fuzzy one (`label: 'Send'` must not return + * `Send feedback`); otherwise, unless `query.exact`, the first fuzzy match is + * returned. The identifier is always matched exactly (see `matchesQuery`). * * @param elements - The hierarchy to search. * @param query - The query. @@ -13,7 +14,10 @@ export function findElement( elements: UIElement[], query: ElementQuery, ): UIElement | null { - return findFirst(elements, query, true) ?? findFirst(elements, query, false); + return ( + findFirst(elements, query, true) ?? + (query.exact ? null : findFirst(elements, query, false)) + ); } /** @@ -72,8 +76,10 @@ export function findAncestors( /** * @param element - The element. * @param query - The query. Every given field must match. - * @param exact - Require equal strings instead of a fuzzy match - * (case-insensitive, contains). Default false. + * @param exact - Require equal strings for `label`, `text` and `type` instead + * of a fuzzy match (case-insensitive, contains). Default false. The + * identifier is always matched exactly: a missing test ID must not match + * another one that contains it. * @returns True when the element matches. */ export function matchesQuery( @@ -82,7 +88,7 @@ export function matchesQuery( exact = false, ): boolean { const match = exact ? exactMatch : fuzzyMatch; - if (query.identifier && !match(element.identifier, query.identifier)) { + if (query.identifier && !exactMatch(element.identifier, query.identifier)) { return false; } if (query.label && !match(element.label, query.label)) { diff --git a/src/utils/wait-for-element.test.ts b/src/utils/wait-for-element.test.ts index d592a35..a2360e9 100644 --- a/src/utils/wait-for-element.test.ts +++ b/src/utils/wait-for-element.test.ts @@ -117,6 +117,19 @@ describe('pollForElement', () => { now.mockRestore(); }); + it('times out when only an identifier that contains the requested one is on screen', async () => { + const snapshot = reads( + screen( + { ...sheetButton(500), identifier: 'confirm-footer' }, + { ...sheetButton(600), identifier: 'Confirm' }, + ), + ); + + await expect(pollForElement(snapshot, query, 30, 5)).rejects.toThrow( + 'Timed out waiting for element', + ); + }); + it('throws when the element does not appear before the timeout', async () => { const snapshot = reads(screen()); From 89c69f16156bb48a5b9a83bf424fca0e30c9acf9 Mon Sep 17 00:00:00 2001 From: cryptotavares Date: Fri, 4 Dec 2026 11:56:08 +0000 Subject: [PATCH 2/2] refactor: drop redundant comments; describe the partial match --- README.md | 8 ++++---- src/backends/adb-backend.test.ts | 1 - src/backends/types.ts | 2 -- src/tools/get-element-text.ts | 7 +++---- src/tools/long-press.ts | 8 +++----- src/tools/scroll-to-element.ts | 5 +++-- src/tools/tap-element.ts | 7 +++---- src/tools/wait-for.ts | 5 +++-- src/utils/element.ts | 10 ++++------ 9 files changed, 23 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index f3b09a1..0cdf955 100644 --- a/README.md +++ b/README.md @@ -206,11 +206,11 @@ The `.device-session` file is typically written by the test runner when it creat ### Element Identification -Elements are identified by accessibility attributes — not internal refs. Every field given in the query must match: +Elements are identified by accessibility attributes — not internal refs. Every field in the query must match. -- `identifier` matches **exactly**: the whole string, case-sensitive. `{ identifier: "account-picker" }` never matches `account-picker-avatar`; when no element has that identifier, the query finds nothing (taps fail with `Element not found`, waits time out). On Android, use the whole resource-id (`io.metamask:id/title`, not `title`). -- `label`, `text` (the element's value or label) and `type` match **fuzzily**: an exact match anywhere on screen wins; otherwise the first element whose field equals the query ignoring case, or contains it, matches. For example, `{ label: "Confirm" }` matches an element with label `"Confirm Transaction"` when no element is labelled exactly `"Confirm"`. -- `exact: true` (library API, not an MCP tool parameter) turns off the fuzzy fallback: `label`, `text` and `type` must then equal the element's value too. +- `identifier` matches exactly and case-sensitively (on Android, the whole resource-id: `io.metamask:id/title`). +- `label`, `text` (value or label) and `type`: an exact match anywhere on screen wins; otherwise the first partial, case-insensitive match. `{ label: "Confirm" }` matches `"Confirm Transaction"` when no element is labelled `"Confirm"`. +- `exact: true` (library API only) turns off the partial match. - **iOS**: accessibility label, accessibility identifier - **Android**: content-description, resource-id, text diff --git a/src/backends/adb-backend.test.ts b/src/backends/adb-backend.test.ts index c3001b0..2291152 100644 --- a/src/backends/adb-backend.test.ts +++ b/src/backends/adb-backend.test.ts @@ -863,7 +863,6 @@ describe('AdbBackend.tapElement', () => { await expect( backend.tapElement({ identifier: 'not-on-screen' }), ).rejects.toThrow('Element not found'); - // `io.metamask:id/title` is on screen: only its whole resource-id matches. await expect(backend.tapElement({ identifier: 'title' })).rejects.toThrow( 'Element not found', ); diff --git a/src/backends/types.ts b/src/backends/types.ts index 496fdba..67483a3 100644 --- a/src/backends/types.ts +++ b/src/backends/types.ts @@ -107,11 +107,9 @@ export type LogsResult = { export type ElementQuery = { label?: string; - /** Always matched exactly: the whole string, case-sensitive. */ identifier?: string; text?: string; type?: string; - /** Match `label`, `text` and `type` exactly too, with no partial fallback. */ exact?: boolean; }; diff --git a/src/tools/get-element-text.ts b/src/tools/get-element-text.ts index e91ba77..2ad3e0b 100644 --- a/src/tools/get-element-text.ts +++ b/src/tools/get-element-text.ts @@ -15,7 +15,8 @@ export function registerGetElementTextTool( description: 'Find an element in the UI hierarchy by query and return its text content (label or value). ' + 'Specify at least one of: label, identifier, text, type. ' + - 'Call device_snapshot first to see available elements.', + 'Call device_snapshot first to see available elements. ' + + '`label`, `text` and `type` match partially (case-insensitive) when no element matches exactly.', inputSchema: { label: z .string() @@ -26,9 +27,7 @@ export function registerGetElementTextTool( identifier: z .string() .optional() - .describe( - 'Accessibility identifier (iOS) or resource-id (Android), matched exactly', - ), + .describe('Accessibility identifier (iOS) or resource-id (Android)'), text: z.string().optional().describe('Visible text content'), type: z.string().optional().describe('Element type/class name'), }, diff --git a/src/tools/long-press.ts b/src/tools/long-press.ts index 50abaed..f925b28 100644 --- a/src/tools/long-press.ts +++ b/src/tools/long-press.ts @@ -14,13 +14,11 @@ export function registerLongPressTool( title: 'Long Press', description: 'Long press an element. Used for context menus, drag initiation, ' + - 'and other long-press interactions. Default duration is 1000ms.', + 'and other long-press interactions. Default duration is 1000ms. ' + + '`label`, `text` and `type` match partially (case-insensitive) when no element matches exactly.', inputSchema: { label: z.string().optional().describe('Accessibility label'), - identifier: z - .string() - .optional() - .describe('Identifier/resource-id, matched exactly'), + identifier: z.string().optional().describe('Identifier/resource-id'), text: z.string().optional().describe('Text content'), type: z.string().optional().describe('Element type'), durationMs: z diff --git a/src/tools/scroll-to-element.ts b/src/tools/scroll-to-element.ts index dd3d0c8..c6c51f4 100644 --- a/src/tools/scroll-to-element.ts +++ b/src/tools/scroll-to-element.ts @@ -17,13 +17,14 @@ export function registerScrollToElementTool( '(being in the UI tree is not enough), and report how many swipes ' + 'it took. Fails when the element is not in view after maxAttempts ' + 'swipes or the screen stops moving. ' + - 'Specify at least one of: label, identifier, text, type.', + 'Specify at least one of: label, identifier, text, type. ' + + '`label`, `text` and `type` match partially (case-insensitive) when no element matches exactly.', inputSchema: { label: z.string().optional().describe('Accessibility label to match'), identifier: z .string() .optional() - .describe('Identifier/resource-id to match exactly'), + .describe('Identifier/resource-id to match'), text: z.string().optional().describe('Text content to match'), type: z.string().optional().describe('Element type to match'), direction: z diff --git a/src/tools/tap-element.ts b/src/tools/tap-element.ts index 4b35ebd..0ded04b 100644 --- a/src/tools/tap-element.ts +++ b/src/tools/tap-element.ts @@ -15,7 +15,8 @@ export function registerTapElementTool( description: 'Find an element in the UI hierarchy by query and tap its center. ' + 'Specify at least one of: label, identifier, text, type. ' + - 'Call device_snapshot first to see available elements.', + 'Call device_snapshot first to see available elements. ' + + '`label`, `text` and `type` match partially (case-insensitive) when no element matches exactly.', inputSchema: { label: z .string() @@ -26,9 +27,7 @@ export function registerTapElementTool( identifier: z .string() .optional() - .describe( - 'Accessibility identifier (iOS) or resource-id (Android), matched exactly', - ), + .describe('Accessibility identifier (iOS) or resource-id (Android)'), text: z.string().optional().describe('Visible text content'), type: z.string().optional().describe('Element type/class name'), }, diff --git a/src/tools/wait-for.ts b/src/tools/wait-for.ts index a0f7e4c..d13b71d 100644 --- a/src/tools/wait-for.ts +++ b/src/tools/wait-for.ts @@ -15,13 +15,14 @@ export function registerWaitForTool( description: 'Poll the UI hierarchy until an element matching the query appears ' + '(and, when it appears during the wait, stops moving). ' + - 'Useful for waiting on transitions, loading states, or navigation.', + 'Useful for waiting on transitions, loading states, or navigation. ' + + '`label`, `text` and `type` match partially (case-insensitive) when no element matches exactly.', inputSchema: { label: z.string().optional().describe('Accessibility label to match'), identifier: z .string() .optional() - .describe('Identifier/resource-id to match exactly'), + .describe('Identifier/resource-id to match'), text: z.string().optional().describe('Text content to match'), type: z.string().optional().describe('Element type to match'), timeoutMs: z diff --git a/src/utils/element.ts b/src/utils/element.ts index 0b9e88b..292605d 100644 --- a/src/utils/element.ts +++ b/src/utils/element.ts @@ -2,9 +2,9 @@ import type { UIElement, ElementQuery } from '../backends/types.js'; /** * Finds the element that matches the query. An exact match anywhere in the - * tree wins over an earlier fuzzy one (`label: 'Send'` must not return - * `Send feedback`); otherwise, unless `query.exact`, the first fuzzy match is - * returned. The identifier is always matched exactly (see `matchesQuery`). + * tree wins over an earlier fuzzy one (`account-picker` must not return + * `account-picker-avatar`); otherwise, unless `query.exact`, the first fuzzy + * match is returned. * * @param elements - The hierarchy to search. * @param query - The query. @@ -77,9 +77,7 @@ export function findAncestors( * @param element - The element. * @param query - The query. Every given field must match. * @param exact - Require equal strings for `label`, `text` and `type` instead - * of a fuzzy match (case-insensitive, contains). Default false. The - * identifier is always matched exactly: a missing test ID must not match - * another one that contains it. + * of a fuzzy match (case-insensitive, contains). Default false. * @returns True when the element matches. */ export function matchesQuery(