Writing a driver
A driver connects toets to one platform. It is a package whose default export is a DriverModule; @toets/test imports it by name only when a project uses it.
import type { DriverModule } from '@toets/core';
const module: DriverModule = {
platform: 'android-tv',
async createDriver(options, ctx) {
return new AndroidTvDriver(options, ctx.app, ctx.device);
},
createDeviceProvider: (options) => new EmulatorProvider(options), // for `managed` devices
};
export default module;Contract
- Small and dumb. A driver answers
query,focused,tree,press,launch,screenshotand declares itscapabilities. It never claimsnetwork.mock(the core grants it when the mock server is on) or SDK capabilities. Retries, strictness, matchers, navigation, idle and reporting live in core, so every platform behaves the same. - Selector semantics are shared. Answer
querywithevaluateChain(andfocusedwithfocusedIn) from@toets/coreover your tree instead of reimplementing them: the first step searches the whole tree, text matches the deepest element (matchText), regexes are stateless (testRegExp),nthpicks among the current matches. A driver that must evaluate elsewhere for speed (the web driver evaluates in the page) keeps exactly these semantics. - Stable refs.
ElementSnapshot.refmust be the same for one element acrossquery,focusedandtree, and stay stable while the element is attached. Focus navigation andfocus.current()compare refs. - One coordinate space. Bounds and
UITree.viewportshare the tree's coordinate space (points, dp or CSS pixels), with the viewport origin at 0,0. The viewport screenshot may be in device pixels; core scales by image width over viewport width when it crops elements and masks. - Launch passes the payload (
sdkUrl,token,mockUrl,state,flags) to the app in the platform's way (URL parameter, intent extra, launch arguments), always encoded withencodeLaunchPayloadParamfrom@toets/protocol(base64url JSON). - Deep links. A driver that declares
deeplink.launchprovidesdeepLinks(DriverDeepLinks):translate(link)turns the canonical URL into the platform's form through the user'sdeepLinkmapper (DeepLinkMapper<T>), with a one-linedescriptionfor reports;launch({ deepLink })starts the app from it, through the OS so its routing is tested;open(link)(withdeeplink.open) delivers it to the running app. Fail withDEEPLINK_INVALIDfor a mapper result the platform can not open andDEEPLINK_UNHANDLEDwhen the app does not handle it. - Text entry. A driver that declares
text.inputprovidestext(DriverTextInput): replace, insert, delete backward, submit and close, through the platform's own text entry, into the focused field (the core passes the field as it sees it,valueincluded). The core moves focus there with real presses, verifies the result and closes a keyboardfillopened. Report fields aseditablewith theirvalue, masked (one•per character) andsecurefor passwords. Never inject text through the SDK. - Devices: use
DeviceAcquireOptions.parallelIndexto pick an exclusive device per worker slot. - Errors are
ToetsErrorwith ahintand adocsslug.
Runners
@toets/core/runner brings a target up and down for a runner: RunningTarget.start loads the driver module, picks the device provider (managed, external, command), leases a device, starts the driver, the mock server and the SDK host, and builds the AppRuntime. @toets/test (per worker) and @toets/mcp (per session) both use it, so a target behaves the same in tests and in agent exploration. It also has the small helpers interactive runners need to report like tests: settleFocus (wait for a press to land, as navigateTo does), jsString and describeKeyOverride.
Testing a driver
@toets/core/testing has FakeDriver (a scripted screen with geometry-based focus), FakeSdkHost and FakeMockServer. Run the core behaviour against the fake, and keep real-device tests for the mapping your driver owns.
TV trap: focus is not keyboard focus
On Android TV and tvOS the focused element comes from the platform's focus engine, not from accessibility focus. Read the real focus state (focused attribute, hasFocus), and never move focus with taps or accessibility actions.