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

# API 参考

> 测试如何与 App 交互：driver、每个调用如何等待 App、所有调用共有的选项，以及 API 各部分在哪里说明。

测试通过一个对象来驱动 App，即 **driver**。它像用户一样点击、输入和滚动，检查屏幕上显示的内容，并能深入 App 内部完成准备工作。本节按主题分页介绍每个方法，并为每个方法给出示例。

## Driver

在每个测试运行器中，driver 都是同一个对象，只是测试获取它的方式不同：

```ts Vitest theme={null}
import { test } from '@quiesco/vitest';

test('a new todo appears in the list', async ({ app }) => {
  await app.tap('add-todo');
});
```

```js Jest theme={null}
test('a new todo appears in the list', async () => {
  await quiesco.app.tap('add-todo');
});
```

```ts Cucumber theme={null}
import { When } from '@cucumber/cucumber';
import type { QuiescoWorld } from '@quiesco/cucumber';

When('I add a todo', async function (this: QuiescoWorld) {
  await this.app.tap('add-todo');
});
```

本节的示例使用 Vitest 的写法 `app`。在 Jest 中写作 `quiesco.app`，在 Cucumber 步骤中写作 `this.app`。示例中的固定字符串使用[常量](https://docs.quiesco.dev/zh/api-constants.md)（`Direction.left`、
`Tag.native` …），从所用 runner 的包导入（`@quiesco/vitest`、`@quiesco/cucumber`）；使用 Jest 时它们也在全局对象上（`quiesco.Direction`）。

每个方法的标题下面是它的签名，以及签名中出现的[类型](https://docs.quiesco.dev/zh/api-types.md)的链接（`MatcherInput`、
`TapOptions`、`ElementSummary` …）。这些类型都由 `@quiesco/core` 导出，自己写辅助函数时可以直接引用：
`import type { MatcherInput, TapOptions } from '@quiesco/core'`。

## 每个调用都会等待 App

每个方法都是 `async` 的。执行之前，它会等待 App 进入**空闲**状态：没有待触发的定时器、进行中的请求、渲染或动画（[同步](https://docs.quiesco.dev/zh/sync.md)）。操作完成之后还会再等待一次。因此测试中无需 sleep 或轮询：

```ts theme={null}
// 新页面的转场和请求都完成后，tap 才返回
await app.tap('open-settings');

// 所以这一步在新页面上执行
await app.tap('dark-mode-switch');
```

断言（`expectVisible`、`expectText`……）也会不断重试直到成立，最长为该调用的超时时间。

## 所有调用共有的选项

每个方法都接受一个可选的最后参数：

| 选项 | 默认值 | 含义 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10 s） | 本次调用最多等待多久 |
| `sync` | `true` | `false`：本次调用前后不等待空闲。用于动画永不停止的页面 |
| `mode` | `actions.mode` | 仅限操作。`'native'`：真实的触摸和键盘输入（iOS / Android 需要 `@quiesco/native`，web 上为浏览器输入）。`'js'`：调用组件的回调（`onPress`、`onChangeText`） |

```ts theme={null}
// 较慢的页面：最多等待 30 s
await app.expectVisible('report', { timeoutMs: 30_000 });

// 这个页面上有循环动画：不等待空闲
await app.tap('stop-animation', { sync: false });

// 调用 onPress，而不是触摸屏幕
await app.tap('hidden-debug-button', { mode: ActionMode.js });
```

## 查找元素

大多数方法都接受一个 **target**，即要操作的元素。字符串表示 `testID`；对象可以组合文本、角色、位置等多个条件。见 [匹配器](https://docs.quiesco.dev/zh/api-matchers.md)。

```ts theme={null}
await app.tap('save');
await app.tap({ role: 'button', text: 'Save' });
await app.expectText({ id: 'todo-title', index: 0 }, 'Buy milk');
```

一个操作需要恰好一个**可见**元素。它会等待目标存在且可见，如果超时仍未出现，则以 `ELEMENT_NOT_FOUND` 失败（错误信息会说明是缺失还是被隐藏）。匹配到多个元素时会立即以 `MULTIPLE_MATCHES` 失败。在 `native` 模式下，触摸还必须能到达该元素：如果被对话框或其他视图遮挡，或者要触摸的点被滚动视图裁掉，操作会以 `ELEMENT_OBSCURED` 失败，并指出挡在上面的是什么。遮挡目标的软键盘会先被收起。

## 按主题查看 API

> [card] [匹配器](https://docs.quiesco.dev/zh/api-matchers.md)
>
> target 如何找到元素：test id、文本、角色、类型、位置、祖先。

> [card] [操作](https://docs.quiesco.dev/zh/api-actions.md)
>
> 点击、输入、滚动、滑动、拖动、返回、打开深层链接。

> [card] [值控件](https://docs.quiesco.dev/zh/api-value-controls.md)
>
> 开关、滑块、选择器、分段控件、日期选择器。

> [card] [多指手势](https://docs.quiesco.dev/zh/api-gestures.md)
>
> 捏合、旋转，以及任意触摸路径。

> [card] [断言](https://docs.quiesco.dev/zh/api-assertions.md)
>
> 可见、文本、值、启用、选中、数量——不断重试直到成立。

> [card] [查询与状态](https://docs.quiesco.dev/zh/api-queries.md)
>
> 读取元素和元素树、等待空闲、调用后门、重置。

> [card] [运行时同步](https://docs.quiesco.dev/zh/api-runtime-sync.md)
>
> 在测试中更改 driver 要等待的内容。

> [card] [App 与设备](https://docs.quiesco.dev/zh/api-device.md)
>
> 重新启动、切到后台、屏幕方向、深色模式、语言区域、网络、剪贴板。

> [card] [系统 UI](https://docs.quiesco.dev/zh/api-system.md)
>
> 权限弹窗、生物识别、通知、浏览器对话框。

> [card] [截图](https://docs.quiesco.dev/zh/api-screenshots.md)
>
> 整个屏幕、单个元素，或整个滚动页面。

> [card] [WebView](https://docs.quiesco.dev/zh/api-webview.md)
>
> App 内网页中的元素。

> [card] [App 内 API](https://docs.quiesco.dev/zh/api-in-app.md)
>
> App 中注册的内容：后门、重置 hook、存储、启动参数。

> [card] [测试标签](https://docs.quiesco.dev/zh/api-tags.md)
>
> 只在部分平台或浏览器上运行测试，或在完全重启后运行。

> [card] [常量](https://docs.quiesco.dev/zh/api-constants.md)
>
> 用常量代替手写字符串：标签、方向、权限、错误名 …

> [card] [类型](https://docs.quiesco.dev/zh/api-types.md)
>
> 签名中出现的每个类型：选项、返回值、匹配器。

> [card] [错误](https://docs.quiesco.dev/zh/api-errors.md)
>
> 调用可能失败的每一种错误及其含义。

> [card] [产物](https://docs.quiesco.dev/zh/api-artifacts.md)
>
> 一次运行的截图、元素树、视频、日志和时间线。

> [card] [其他测试运行器](https://docs.quiesco.dev/zh/api-custom-runners.md)
>
> Vitest 和 Jest 适配器详解，以及在任意运行器中驱动 Quiesco。

所有配置选项见 [配置](https://docs.quiesco.dev/zh/config.md) 页面。


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