> ## 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.

# App and device

> Relaunch, stop and background the app, and change the device: orientation, dark mode, text size, locale, location, permissions, network, clipboard and status bar.

`app.device` controls the app's process and the device it runs on: start the app again with launch
arguments or a deep link, send it to the background, rotate the screen, switch to dark mode, go offline.
Each change is undone before the next test ([between tests](#between-tests)), so tests stay independent.

System UI outside the app — permission dialogs, biometrics, notifications — is on [system UI](https://docs.quiesco.dev/api-system.md);
screenshots are on [screenshots](https://docs.quiesco.dev/api-screenshots.md).

## App lifecycle

### `relaunch(options?)`

```ts theme={null}
relaunch(options?: RelaunchOptions): Promise<void>
```

Types: [`RelaunchOptions`](./api-types#relaunchoptions)

Terminates the app and starts it again — always a new process. The app keeps its data unless `clearData`
or `reinstall` is set. Resolves once the app has connected.

```ts theme={null}
// typed launch arguments and a deep link: a real cold start, getInitialURL sees the link
await app.device.relaunch({ args: { user: 'ann', retries: 3 }, url: 'myapp://todos/1' });

// a first launch: the app's data is deleted first
await app.device.relaunch({ clearData: true });

// a fresh install, with the camera already allowed
await app.device.relaunch({ reinstall: true, permissions: [Permission.camera] });
```

| Option | Meaning |
| - | - |
| `args` | Launch arguments (any JSON), merged over the config's [`launchArgs`](./config#top-level). The app reads them with `quiesco.launchArgs()` ([in-app API](https://docs.quiesco.dev/api-in-app.md)) |
| `url` | Open the app with this deep link. On the web its path is opened |
| `clearData` | Delete the app's data first (native: the app's storage; web: a fresh browser context) |
| `reinstall` | Uninstall and install the app first. Needs the app's `binaryPath`; on the web the same as `clearData` |
| `permissions` | Permissions to grant before the launch, besides the config's ([permissions](https://docs.quiesco.dev/platforms.md#permissions)). After `clearData` / `reinstall` the config's are granted again |

Without `@quiesco/native`, an app that cannot start with the link is started first and the link is opened
after the launch, with a warning: `getInitialURL` will not return it then.

### `terminate()`

```ts theme={null}
terminate(): Promise<void>
```

Stops the app. Until the next `relaunch()`, every element call fails at once with `APP_NOT_RUNNING`,
without waiting.

```ts theme={null}
await app.device.terminate();
await app.device.relaunch();
await app.expectVisible('home-screen');
```

### `sendToBackground()`

```ts theme={null}
sendToBackground(): Promise<void>
```

Goes to the home screen, like the Home button; the process keeps running. Until `bringToForeground()`,
element calls fail with `APP_IN_BACKGROUND`. Not on the web (`ACTION_NOT_SUPPORTED`).

```ts theme={null}
await app.device.sendToBackground();

// a deep link now opens from outside the app, as from another app
await app.openUrl('myapp://todos/42');
```

### `bringToForeground()`

```ts theme={null}
bringToForeground(): Promise<void>
```

Brings the backgrounded app back — the same process, not a restart — and waits until it is active and
idle. Not on the web (`ACTION_NOT_SUPPORTED`).

```ts theme={null}
await app.device.sendToBackground();
await app.device.bringToForeground();

// the app locked itself while it was away
await app.expectVisible('pin-screen');
```

### `install()`

```ts theme={null}
install(): Promise<void>
```

Installs the app binary from the config's `binaryPath` ([apps](https://docs.quiesco.dev/config.md#apps)). A running app is stopped:
start it with `relaunch()`.

```ts theme={null}
await app.device.install();
await app.device.relaunch();
```

### `uninstall()`

```ts theme={null}
uninstall(): Promise<void>
```

Removes the app from the device. The web has nothing to uninstall (`ACTION_NOT_SUPPORTED`): use
`relaunch({ clearData: true })` there.

```ts theme={null}
await app.device.uninstall();
await app.device.install();
await app.device.relaunch();
```

### `appState`

What the test did to the app last: `'running'`, `'background'` or `'terminated'`. A property, not a method.

```ts theme={null}
await app.device.sendToBackground();
expect(app.device.appState).toBe(AppRunState.background);
```

### `openUrl(url)`

```ts theme={null}
openUrl(url: string): Promise<void>
```

The same as [`app.openUrl`](./api-actions#openurl): opens a deep link.

```ts theme={null}
await app.device.openUrl('myapp://settings');
```

### `pressBack()`

```ts theme={null}
pressBack(): Promise<void>
```

The same as [`app.pressBack`](./api-actions#pressback): the Android back button, or the browser's back on
the web.

```ts theme={null}
await app.device.pressBack();
```

## Network ports

### `reversePort(port)`

```ts theme={null}
reversePort(port: number): Promise<void>
```

Makes a port on the host reachable as `localhost:port` from an Android device (`adb reverse`). A no-op on
the iOS simulator and the web, which share the host's network.

```ts theme={null}
// the app talks to a mock server on the host
await app.device.reversePort(3001);
```

### `unreversePort(port)`

```ts theme={null}
unreversePort(port: number): Promise<void>
```

Removes a forwarding set with `reversePort`. A no-op on the iOS simulator and the web.

```ts theme={null}
await app.device.unreversePort(3001);
```

## Device state

### `setOrientation(orientation)`

```ts theme={null}
setOrientation(orientation: Orientation): Promise<void>
```

Types: [`Orientation`](./api-constants#list)

Rotates the app to `'portrait'` or `'landscape'` and resolves once the app's window has the new shape and
the app is idle. The app must not lock its orientation (app.json `"orientation": "default"`).

```ts theme={null}
await app.device.setOrientation(Orientation.landscape);
await app.expectVisible('side-by-side-layout');
```

### `setAppearance(scheme)`

```ts theme={null}
setAppearance(scheme: ColorScheme): Promise<void>
```

Types: [`ColorScheme`](./api-constants#list)

Switches the system color scheme to `'light'` or `'dark'`, and checks that the app reports the new one.
The app must follow the system (app.json `"userInterfaceStyle": "automatic"`).

```ts theme={null}
await app.device.setAppearance(ColorScheme.dark);
await app.device.screenshot(undefined, { name: 'home-dark' });
```

### `setFontScale(scale)`

```ts theme={null}
setFontScale(scale: number): Promise<number>
```

Sets the system text size, from `0.5` to `4`, and resolves with the scale applied. iOS uses the nearest
Dynamic Type size and applies it live; Android starts the app again (data kept). Not on the web.

```ts theme={null}
const applied = await app.device.setFontScale(1.3);

// the long title still fits
await app.expectVisible('todo-title', { ratio: 1 });
```

### `setLocale(locale)`

```ts theme={null}
setLocale(locale: string | null): Promise<void>
```

Sets the app's language and region, a BCP 47 tag such as `'ja-JP'`; `null` goes back to the device
default. The app is started again with it (data kept).

```ts theme={null}
await app.device.setLocale('ja-JP');
await app.expectText('add-todo', '追加');

await app.device.setLocale(null);
```

* It passes when the app's preferred language (iOS `AppleLanguages`, what expo-localization reads) or its
  JS locale is the new one.
* iOS resolves `Intl` and the app's native strings only to a language listed in `CFBundleLocalizations`.
  Without it a warning says so; an app localized in JS works as it is.

### `setLocation(latitude, longitude)`

```ts theme={null}
setLocation(latitude: number, longitude: number): Promise<void>
```

Sets the simulated GPS position. On Android, emulators only.

```ts theme={null}
// Tokyo
await app.device.setLocation(35.68, 139.76);
await app.tap('nearby-stores');
```

### `setPermission(name, state)`

```ts theme={null}
setPermission(name: Permission, state: PermissionState): Promise<void>
```

Types: [`Permission`](./api-constants#list) · [`PermissionState`](./api-constants#list)

Changes a permission while the app runs. `state` is `'grant'`, `'revoke'` or `'reset'`; the names are
those of [permissions](https://docs.quiesco.dev/platforms.md#permissions) (`camera`, `microphone`, `location`, `notifications`,
`contacts`, `photos`, `calendar`).

```ts theme={null}
await app.device.setPermission(Permission.location, PermissionState.revoke);
await app.tap('nearby-stores');
await app.expectVisible('location-denied-banner');

// ask again: the app's next request shows the system dialog
await app.device.setPermission(Permission.camera, PermissionState.reset);
```

* **Revoke on Android** kills the app. Quiesco starts it again (data kept) before resolving, so the app is
  back on its first screen.
* **`'reset'`** means not determined, as after a fresh install: the app's next request shows the system
  dialog again. Android: revoked, with "don't ask again" cleared; iOS: `simctl privacy reset`; web: the
  browser asks.
* **On iOS, notifications and the camera** can only be reset by reinstalling the app. Quiesco reinstalls
  and starts it, **deleting its data** (needs `binaryPath`; a warning says so).

Answering the system's permission dialog itself is on [system UI](https://docs.quiesco.dev/api-system.md#system-allowpermission).

### `setNetwork(state)`

```ts theme={null}
setNetwork(state: NetworkState): Promise<void>
```

Types: [`NetworkState`](./api-constants#list)

`'offline'`: the app's new `fetch` / `XMLHttpRequest` / `WebSocket` connections fail as they do without a
network. Quiesco's own connection keeps working, and connections already open stay open. `'online'` undoes
it.

```ts theme={null}
await app.device.setNetwork(NetworkState.offline);
await app.tap('sync-now');
await app.expectVisible({ text: 'You are offline' });

await app.device.setNetwork(NetworkState.online);
```

* Android also turns Wi-Fi and mobile data off, and the web reports `navigator.onLine === false`, so
  NetInfo sees it there.
* The iOS simulator has no network switch: NetInfo keeps reporting online.
* It lasts across relaunches, until `'online'` or the next test.

To make the app's own requests not count for idle instead, see [runtime synchronization](https://docs.quiesco.dev/api-runtime-sync.md)
(Detox's `setURLBlacklist` is `app.sync.ignoreUrls.add()` here).

### `clipboard.set(text)`

```ts theme={null}
clipboard.set(text: string): Promise<void>
```

Writes plain text to the device clipboard, inside the app: what the app pastes. Cleared before the next
test.

```ts theme={null}
await app.device.clipboard.set('SAVE20');
await app.tap('paste-coupon');
await app.expectValue('coupon-input', 'SAVE20');
```

iOS and Android need `@quiesco/native` (≥ Phase 6.6).

### `clipboard.get()`

```ts theme={null}
clipboard.get(): Promise<string>
```

Reads the device clipboard: what the app copied.

```ts theme={null}
await app.tap('copy-invite-link');
expect(await app.device.clipboard.get()).toMatch(/^https:\/\/example\.com\/invite\//);
```

iOS and Android need `@quiesco/native` (≥ Phase 6.6); on iOS a read falls back to `simctl pbpaste`.

### `setStatusBar(options)`

```ts theme={null}
setStatusBar(options: StatusBarOptions): Promise<void>
```

Types: [`StatusBarOptions`](./api-types#statusbaroptions)

Overrides the status bar — clock, battery, signal — for clean screenshots. Nothing to do on the web, which
has no status bar.

```ts theme={null}
await app.device.setStatusBar({ time: '9:41', batteryLevel: 100, wifiBars: 3, cellularBars: 4 });
await app.device.screenshot(undefined, { name: 'store-listing' });
```

| Option | Meaning |
| - | - |
| `time` | The clock, `'H:MM'` / `'HH:MM'`, e.g. `'9:41'` |
| `batteryLevel` | 0–100 |
| `charging` | `true` / `false` |
| `wifiBars` | 0–3 |
| `cellularBars` | 0–4 |
| `dataNetwork` | `'wifi'`, `'3g'`, `'4g'`, `'lte'`, `'5g'`, or `'hide'` to hide the cellular indicator |

### `resetStatusBar()`

```ts theme={null}
resetStatusBar(): Promise<void>
```

Back to the real status bar.

```ts theme={null}
await app.device.resetStatusBar();
```

## Screenshots

`app.device.screenshot()` takes a PNG of the screen, an element or a whole scrolling page: see
[screenshots](https://docs.quiesco.dev/api-screenshots.md).

## Between tests

* Before each test Quiesco starts an app a previous test left terminated, and brings a backgrounded one
  back.
* Every device change is undone before the next test, and when the session ends: permissions go back to
  the configured ones, the rest to what the device had. Only what was changed is touched.
* A call that starts the app again (`setLocale`, `setFontScale` on Android, a revoke on Android) leaves it
  on its first screen.


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