Text input
A TV has no keyboard. Users type with one of two kinds:
- The system keyboard, which the platform shows for its own text fields: an
<input>on web, anEditTexton Android TV, aUITextFieldon tvOS. toets enters text into these with the platform's text entry (text.inputcapability). - An app keyboard (on-screen keyboard, OSK): a grid of focusable keys the app draws itself, common in search and PIN screens. Nothing on the platform knows it is a keyboard, so you declare it once with
defineKeyboard(). toets then types each character the way a user does: D-pad presses to its key, thenSELECT.
The test code is the same for both. toets picks the keyboard for each field.
ts
import { defineKeyboard, expect, Key, test } from '@toets/test';
// Usually in its own file, passed to defineConfig({ keyboards }).
const search = defineKeyboard({
name: 'search',
root: { testId: 'osk' },
fields: [{ testId: 'search-input' }],
layers: [{ testId: 'osk-layer' }],
space: { testId: 'osk-space' },
backspace: { testId: 'osk-del' },
clear: { testId: 'osk-clear' },
submit: { testId: 'osk-search' },
});
test.use({ keyboards: [search] });
test('searches with the app keyboard', async ({ app }) => {
await app.launch();
await app.focus.pressUntilFocused(Key.LEFT, app.getByTestId('menu-home'));
await app.focus.pressUntilFocused(Key.DOWN, app.getByTestId('menu-search'));
await app.remote.press(Key.SELECT);
const query = app.getByTestId('search-input');
await query.fill('big buck');
await expect(query).toHaveText('big buck');
await expect(app.keyboard).toBeOpen();
await app.keyboard.type(' 2'); // switches to the digits layer for "2"
await app.keyboard.erase(2);
await app.keyboard.submit();
await expect(app.getByTestId('search-results')).toHaveText('Results for "big buck"');
await expect(app.keyboard).not.toBeOpen();
});
test('signs in with system text fields', async ({ app }) => {
await app.launch();
await app.focus.pressUntilFocused(Key.LEFT, app.getByTestId('menu-home'));
await app.focus.pressUntilFocused(Key.DOWN, app.getByTestId('menu-sign-in'));
await app.remote.press(Key.SELECT);
const email = app.getByTestId('email');
await email.fill('ada@example.com');
await expect(email).toHaveValue('ada@example.com');
await app.getByTestId('password').fill('tv-pass-1'); // reported as •••••••••
await app.keyboard.submit();
await expect(app.getByTestId('sign-in-status')).toHaveText('Signed in as ada@example.com');
});API
locator.fill(text, { mask })replaces the field's text: focus moves to the field with real presses, then its keyboard enters the text. It never submits, and returns with no system keyboard covering the app.app.keyboardacts on the keyboard in use:type(text, { mask })types at the end,erase(count)deletes before the caret with the keyboard's backspace,submit()presses its submit key (the action key of the system keyboard: Search, Go, Done).toHaveValue(text | RegExp)asserts the value of a system text field. App keyboards type into ordinary elements: assert those withtoHaveText.expect(app.keyboard).toBeOpen()asserts that a keyboard is open, with.notfor closed.mask: truereports the text as••••in step titles, errors and MCP output. System password fields mask on their own.
There is no locator.type, clear or close: fill replaces, type appends to the field the keyboard holds, and keyboards close the way the app closes them (submit() or Key.BACK).
Which keyboard
fill uses, in order:
- The declared keyboard whose
fieldslist the field. When it is closed, toets moves focus to the field, pressesSELECTand tries again. - System text entry, when the field is a system text field.
- Otherwise it fails with
NO_KEYBOARD.
app.keyboard uses the declared keyboard holding focus, else system text entry on the focused system text field, else the only declared keyboard on screen.
Declarations, layers, speed and the step titles: text input reference.
Traps
- An app keyboard is not a system keyboard. Typing a search OSK through the platform's text entry would skip the code under test (key handling, layer switching, the app's own field). Declare it, and toets types it with the remote like a user.
- System text entry is not key events. On web toets inserts text the way an IME does (
insertText):inputevents fire,keydownper character does not. Apps that only listen forkeydownon a system field see nothing; test that handler withapp.remote.press. - Mask secrets. Step titles, the HTML report, traces and MCP output keep the text. Pass
{ mask: true }for PINs and passwords typed on app keyboards.toHaveValuerefuses system password fields (SECURE_FIELD): their value is masked by the platform. - Web has no system keyboard. There,
app.keyboardcounts as open while a text field holds focus:fillon web leaves focus in the field, soexpect(app.keyboard).toBeOpen()passes until focus moves away. On Android TV and tvOSfillcloses the system keyboard it opened. - tvOS password fields reopen empty. Its fullscreen keyboard starts with an empty entry for a secure field, so
app.keyboardafter afilldoes not edit the password there. Set a password with onefilland submit with the screen's button or another field's action key. - Keep keyboard declarations free of test imports. The config loads the
keyboardsfile, so it may import onlydefineKeyboardfrom@toets/test. Importing page objects or fixtures from it pulls test code into the config. keyboardslevels replace, not merge.defineConfig({ keyboards }),platforms.*({ keyboards })andtest.use({ keyboards })each replace the level above: list every keyboard at the level you set.- Old SDKs hide system fields. With the SDK on, the field's
editableflag comes from the SDK (protocol 1.1). An older SDK reports none, andfillfails withNO_KEYBOARD: update the SDK.