> ## Documentation Index
> Fetch the complete documentation index at: https://docs.quiesco.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Driver pattern

> A driver class in business language between the tests and the app, so tests survive UI changes.

Tests should say what the user wants, not which buttons they press. Put the taps and the test ids in a
**driver** of your own, a class with methods in business language (`addTodo('Buy milk')`). The tests
call only the driver. When the UI changes, the driver changes; the tests do not.

```
test / step  →  TodoDriver (addTodo, todoTitles, …)  →  app (tap, type, query, …)
```

The driver works the same with every runner; only how a test gets one differs. The examples use the
[tutorial](https://docs.quiesco.dev/tutorial.md)'s todo app.

## The driver

An interface with what the tests need, and a class that does it through the UI:

```ts theme={null}
// e2e/todo-driver.ts
import type { Driver } from '@quiesco/core';

import type { SeedTodo } from '../src/backdoors';

/** What the todo tests need, in the language of the business. */
export interface TodoDriver {
  seed(todos: SeedTodo[]): Promise<void>;
  addTodo(title: string, notes?: string): Promise<void>;
  todoTitles(): Promise<string[]>;
}

/** Through the UI, with Quiesco. Only this class knows the test ids. */
export class UiTodoDriver implements TodoDriver {
  private readonly app: Driver;

  constructor(app: Driver) {
    this.app = app;
  }

  async seed(todos: SeedTodo[]) {
    // setup without the UI
    await this.app.backdoor('seedTodos', todos);
  }

  async addTodo(title: string, notes?: string) {
    await this.app.tap('add-todo');
    await this.app.type('title-input', title);
    if (notes !== undefined) await this.app.type('notes-input', notes);
    await this.app.tap('save-todo');
    await this.app.expectVisible('todo-list-screen');
  }

  async todoTitles() {
    return (await this.app.query('todo-title')).map((e) => e.text);
  }
}
```

* `app` is the [Driver](https://docs.quiesco.dev/api.md#driver-thisapp) the runner gives each test.
* Prefer Quiesco's assertions inside the driver (`expectVisible`, `expectText`): they retry until the app
  shows the expected state. A plain assertion on a query result checks once, which is fine after an
  action, because the action already waited for the app to be idle.

## Per-platform steps

When one step is done differently per platform, for example a date picker that is inline on iOS and two
system dialogs on Android, the driver branches on `this.app.platform` (`'ios' | 'android' | 'web'`):

```ts theme={null}
async setDueDate(date: Date) {
  if (this.app.platform === Platform.android) {
    await this.app.tap('pick-date');
    await this.app.setValue('due-date-picker', date);
    await this.app.tap('pick-time');
    await this.app.setValue('due-time-picker', date);
  } else {
    await this.app.setValue('due-picker', date);
  }
}
```

A test for one platform only gets a tag instead (`@ios`, `@android`, `@web`; see [tags](https://docs.quiesco.dev/api-tags.md)).

## The same tests against the API

Because the tests only see `TodoDriver`, a second implementation can run them without any UI, against
the app's backend API, in seconds:

```ts theme={null}
export class ApiTodoDriver implements TodoDriver {
  private readonly baseUrl: string;

  constructor(baseUrl: string) {
    this.baseUrl = baseUrl;
  }

  async seed(todos: SeedTodo[]) {
    await fetch(`${this.baseUrl}/todos`, { method: 'PUT', body: JSON.stringify(todos) });
  }

  async addTodo(title: string, notes?: string) {
    await fetch(`${this.baseUrl}/todos`, { method: 'POST', body: JSON.stringify({ title, notes }) });
  }

  async todoTitles() {
    const res = await fetch(`${this.baseUrl}/todos`);
    return ((await res.json()) as { title: string }[]).map((t) => t.title);
  }
}
```

Pick the driver with an environment variable. With Cucumber, the API run can leave Quiesco out entirely
([API run](https://docs.quiesco.dev/driver-pattern-cucumber.md#api-run)).

## Pick a runner

> [card] [Vitest](https://docs.quiesco.dev/driver-pattern-vitest.md)
>
> The driver as a test fixture.

> [card] [Jest](https://docs.quiesco.dev/driver-pattern-jest.md)
>
> The driver in JavaScript, made in `beforeEach`.

> [card] [Cucumber](https://docs.quiesco.dev/driver-pattern-cucumber.md)
>
> The driver on the World, steps in business language.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.