diff --git a/README.md b/README.md index 73f34ee..c24550d 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 in the query must match. + +- `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 eaff870..2291152 100644 --- a/src/backends/adb-backend.test.ts +++ b/src/backends/adb-backend.test.ts @@ -863,6 +863,10 @@ describe('AdbBackend.tapElement', () => { await expect( backend.tapElement({ identifier: 'not-on-screen' }), ).rejects.toThrow('Element not found'); + 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 398a861..94a89c8 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 29d8120..149a6f6 100644 --- a/src/backends/types.ts +++ b/src/backends/types.ts @@ -117,6 +117,7 @@ export type ElementQuery = { identifier?: string; text?: string; type?: string; + 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..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() diff --git a/src/tools/long-press.ts b/src/tools/long-press.ts index 6efc3a8..f925b28 100644 --- a/src/tools/long-press.ts +++ b/src/tools/long-press.ts @@ -14,7 +14,8 @@ 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'), diff --git a/src/tools/scroll-to-element.ts b/src/tools/scroll-to-element.ts index a8726f9..c6c51f4 100644 --- a/src/tools/scroll-to-element.ts +++ b/src/tools/scroll-to-element.ts @@ -17,7 +17,8 @@ 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 diff --git a/src/tools/tap-element.ts b/src/tools/tap-element.ts index 32be4d7..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() diff --git a/src/tools/wait-for.ts b/src/tools/wait-for.ts index dfcd948..d13b71d 100644 --- a/src/tools/wait-for.ts +++ b/src/tools/wait-for.ts @@ -15,7 +15,8 @@ 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 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..292605d 100644 --- a/src/utils/element.ts +++ b/src/utils/element.ts @@ -3,7 +3,8 @@ 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. + * `account-picker-avatar`); otherwise, unless `query.exact`, the first fuzzy + * match is returned. * * @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,8 @@ 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. * @returns True when the element matches. */ export function matchesQuery( @@ -82,7 +86,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());