Exploring an app, then writing the test
The MCP tools are the test API in interactive form. Explore with them until each step works, then write the same steps as a test.
TV traps
- No click. Nothing is clicked, tapped or typed into. Move focus with the D-pad, then
SELECT. - Focus first. Every interaction acts on the focused element. Read focus from
snapshotbefore pressing, and assert it after. - Virtualized lists. Items far offscreen often do not exist yet. Navigate toward them; do not expect
queryto find them up front. - Platform keys differ. Only D-pad,
SELECTand (usually)BACKare universal. Checkcapabilitiesbefore using media, color or numeric keys. - Assertions wait.
assertretries until its timeout, likeexpect. Never add sleeps; wait for a state.
Loop
launch, thensnapshotto see the start screen and where focus is.- Reach the element:
press_until_focuseda direction with its test ID (navigate_towhen it is several directions away,pressfor single steps). - Activate with
press["SELECT"];snapshotagain, screens change. - Check the outcome with
assert(toBeFocused,toBeVisible,toHaveText). - Copy the
Code:lines from the responses into the test.
When a test fails, the same loop reproduces it: launch with the test's state, repeat its steps and assert the failing matcher to read the same message.
Example
Exploration (tool calls and the code they return):
text
launch
-> Focus: card_1 "Movie 1"
press_until_focused { key: "RIGHT", target: { testId: "card_2" } }
-> Code: await app.focus.pressUntilFocused(Key.RIGHT, app.getByTestId('card_2'));
press { keys: ["SELECT"] }
-> Code: await app.remote.press(Key.SELECT);
assert { target: { testId: "play" }, matcher: "toBeFocused" }
-> PASS in 40ms: await expect(app.getByTestId('play')).toBeFocused();The test:
ts
import { expect, Key, test } from '@toets/test';
test('opens details from the popular rail', async ({ app }) => {
await app.launch();
await app.focus.pressUntilFocused(Key.RIGHT, app.getByTestId('card_2'));
await app.remote.press(Key.SELECT);
await expect(app.getByTestId('play')).toBeFocused();
});Run it on every platform; if a selector or key differs per platform, fix the app's test IDs or gate the test with requires, never branch on platform in the test.