Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/backends/adb-backend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down
21 changes: 21 additions & 0 deletions src/backends/idb-backend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
{
Expand Down
1 change: 1 addition & 0 deletions src/backends/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export type ElementQuery = {
identifier?: string;
text?: string;
type?: string;
exact?: boolean;
};

export type DeviceButton = 'home' | 'back' | 'enter' | 'lock';
Expand Down
3 changes: 2 additions & 1 deletion src/tools/get-element-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion src/tools/long-press.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
3 changes: 2 additions & 1 deletion src/tools/scroll-to-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/tools/tap-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion src/tools/wait-for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 56 additions & 11 deletions src/utils/element.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
14 changes: 9 additions & 5 deletions src/utils/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
);
}

/**
Expand Down Expand Up @@ -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(
Expand All @@ -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)) {
Expand Down
13 changes: 13 additions & 0 deletions src/utils/wait-for-element.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Loading