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

# 操作

> 像用户一样点击、输入、滚动、滑动和拖动，返回上一页，以及打开深层链接。

操作做的就是用户会做的事。在 `native` 模式下（App 包含 `@quiesco/native` 时的默认模式），它们是真实的触摸和键盘输入；在 `js` 模式下，它们改为调用组件的回调（见 [调用选项](https://docs.quiesco.dev/zh/api.md#call-options)）。每个操作执行前后都会等待 App 空闲，并且需要恰好一个可见的目标（[查找元素](https://docs.quiesco.dev/zh/api.md#finding-elements)）。

坐标使用逻辑单位：iOS 上为 pt，Android 上为 dp，web 上为 CSS px。

## 点击

### `tap(target, options?)`

```ts theme={null}
tap(
  target: MatcherInput,
  options?: TapOptions,
): Promise<ElementSummary | undefined>
```

类型： [`MatcherInput`](./api-types#matcherinput) · [`TapOptions`](./api-types#tapoptions) · [`ElementSummary`](./api-types#elementsummary)

点击元素的中心。被滚动视图或 `overflow` 祖先裁掉一部分的元素，点击其可见部分的中心。

```ts theme={null}
await app.tap('save');

// 元素内的一个点，从其左上角算起
await app.tap('map', { point: { x: 40, y: 120 } });
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `point` | 中心 | 元素自身坐标系中的 `{ x, y }`，代替其中心 |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待目标元素出现、App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

### `doubleTap(target, options?)`

```ts theme={null}
doubleTap(
  target: MatcherInput,
  options?: ActionOptions,
): Promise<ElementSummary | undefined>
```

类型： [`MatcherInput`](./api-types#matcherinput) · [`ActionOptions`](./api-types#actionoptions) · [`ElementSummary`](./api-types#elementsummary)

在一个手势中点击两次。

```ts theme={null}
await app.doubleTap('photo');
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待目标元素出现、App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

### `multiTap(target, count, options?)`

```ts theme={null}
multiTap(
  target: MatcherInput,
  count: number,
  options?: ActionOptions,
): Promise<ElementSummary | undefined>
```

类型： [`MatcherInput`](./api-types#matcherinput) · [`ActionOptions`](./api-types#actionoptions) · [`ElementSummary`](./api-types#elementsummary)

在一个手势中点击 `count` 次。

```ts theme={null}
// 连点三次打开隐藏的调试菜单
await app.multiTap('version-label', 3);
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待目标元素出现、App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

### `longPress(target, options?)`

```ts theme={null}
longPress(
  target: MatcherInput,
  options?: LongPressOptions,
): Promise<ElementSummary | undefined>
```

类型： [`MatcherInput`](./api-types#matcherinput) · [`LongPressOptions`](./api-types#longpressoptions) · [`ElementSummary`](./api-types#elementsummary)

按住元素。

```ts theme={null}
await app.longPress('message');

await app.longPress('message', { durationMs: 1500, point: { x: 10, y: 10 } });
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `durationMs` | `800` | 按住多久 |
| `point` | 中心 | 元素自身坐标系中的 `{ x, y }` |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待目标元素出现、App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

### `tapAt(point, options?)`

```ts theme={null}
tapAt(point: Point, options?: ActionOptions): Promise<void>
```

类型： [`Point`](./api-types#point) · [`ActionOptions`](./api-types#actionoptions)

点击屏幕上的一个点（窗口坐标），不管那里是什么。

```ts theme={null}
// 点击底部面板上方来关闭它
await app.tapAt({ x: 200, y: 100 });
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待 App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

### `longPressAt(point, options?)`

```ts theme={null}
longPressAt(point: Point, options?: LongPressAtOptions): Promise<void>
```

类型： [`Point`](./api-types#point) · [`LongPressAtOptions`](./api-types#longpressatoptions)

按住屏幕上的一个点（窗口坐标）。

```ts theme={null}
await app.longPressAt({ x: 200, y: 400 }, { durationMs: 1000 });
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `durationMs` | `800` | 按住多久 |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待 App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

## 文本输入

### `type(target, text, options?)`

```ts theme={null}
type(
  target: MatcherInput,
  text: string,
  options?: TypeOptions,
): Promise<ElementSummary | undefined>
```

类型： [`MatcherInput`](./api-types#matcherinput) · [`TypeOptions`](./api-types#typeoptions) · [`ElementSummary`](./api-types#elementsummary)

聚焦输入框，并在已有内容之后输入 `text`。在 `native` 模式下，文本经由系统的输入法输入，因此任何语言都可以。iOS 和 Android 上整段文本一次送达；web 上每个字符一次按键。

```ts theme={null}
await app.type('email', 'ann@example.com');

// 逐字输入：用于防抖搜索、输入掩码、onKeyPress
await app.type('search', 'milk', { perCharacter: true });
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `perCharacter` | `false` | 逐字输入，让 App 收到每一次按键。调用会在最后等待空闲 |
| `intervalMs` | `80` | 配合 `perCharacter`：两个字符之间的间隔 |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待目标元素出现、App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

### `replaceText(target, text, options?)`

```ts theme={null}
replaceText(
  target: MatcherInput,
  text: string,
  options?: ActionOptions,
): Promise<ElementSummary | undefined>
```

类型： [`MatcherInput`](./api-types#matcherinput) · [`ActionOptions`](./api-types#actionoptions) · [`ElementSummary`](./api-types#elementsummary)

一次性替换输入框的全部文本。

```ts theme={null}
await app.replaceText('email', 'bob@example.com');
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待目标元素出现、App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

### `clear(target, options?)`

```ts theme={null}
clear(
  target: MatcherInput,
  options?: ActionOptions,
): Promise<ElementSummary | undefined>
```

类型： [`MatcherInput`](./api-types#matcherinput) · [`ActionOptions`](./api-types#actionoptions) · [`ElementSummary`](./api-types#elementsummary)

清空输入框。

```ts theme={null}
await app.clear('search');
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待目标元素出现、App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

### `pressKey(target, key, options?)`

```ts theme={null}
pressKey(
  target: MatcherInput,
  key: KeyName,
  options?: ActionOptions,
): Promise<ElementSummary | undefined>
```

类型： [`MatcherInput`](./api-types#matcherinput) · [`KeyName`](./api-constants#list) · [`ActionOptions`](./api-types#actionoptions) · [`ElementSummary`](./api-types#elementsummary)

向字段发送回车键（`'enter'`）或 `'backspace'`。`'enter'` 在单行输入框中提交，在多行输入框中换行。字段只需存在即可：即使键盘的回车键够不着，按键也能送达。

```ts theme={null}
await app.type('search', 'milk');
await app.pressKey('search', KeyName.enter);

await app.pressKey('code-input', KeyName.backspace);
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待目标元素出现、App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

### `dismissKeyboard(options?)`

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

类型： [`ActionOptions`](./api-types#actionoptions)

隐藏软键盘。

```ts theme={null}
await app.type('notes', 'Call before noon');
await app.dismissKeyboard();
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待 App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

## 滚动

### `scroll(target, direction, options?)`

```ts theme={null}
scroll(
  target: MatcherInput,
  direction: Direction,
  options?: ScrollOptions,
): Promise<ElementSummary | undefined>
```

类型： [`MatcherInput`](./api-types#matcherinput) · [`Direction`](./api-constants#list) · [`ScrollOptions`](./api-types#scrolloptions) · [`ElementSummary`](./api-types#elementsummary)

滚动一个滚动视图。`'down'` 显示下方的内容。在 `native` 模式下，手指拖动时逐渐减速直到停下，所以内容移动约 `distance`，不会惯性滑动（会少掉系统的触摸 slop：iOS 约 10 pt，Android 约 7%）。

```ts theme={null}
await app.scroll('feed', Direction.down);

await app.scroll('feed', Direction.down, { distance: 300, from: { x: 0.5, y: 0.8 } });
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `distance` | — | 滚动多远，逻辑单位 |
| `from` | — | 手指的起点，在元素内归一化为 0–1 |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待目标元素出现、App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

### `scrollTo(container, target, options?)`

```ts theme={null}
scrollTo(
  container: MatcherInput,
  target: MatcherInput,
  options?: ActionOptions,
): Promise<ElementSummary | undefined>
```

类型： [`MatcherInput`](./api-types#matcherinput) · [`ActionOptions`](./api-types#actionoptions) · [`ElementSummary`](./api-types#elementsummary)

滚动 `container`，直到 `target` 的中心可见（点击会落在那里）：先向下再向上；横向列表先向右再向左。每一步移动不到一屏的距离，所以不会跳过任何元素。滚到内容尽头时，中心仍被裁掉的目标按原样返回。

```ts theme={null}
await app.scrollTo('settings-list', 'sign-out');
await app.tap('sign-out');
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待目标元素出现、App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

### `scrollToEdge(target, edge, options?)`

```ts theme={null}
scrollToEdge(
  target: MatcherInput,
  edge: Edge,
  options?: ActionOptions,
): Promise<ElementSummary | undefined>
```

类型： [`MatcherInput`](./api-types#matcherinput) · [`Edge`](./api-constants#list) · [`ActionOptions`](./api-types#actionoptions) · [`ElementSummary`](./api-types#elementsummary)

滚动到 `'top'`、`'bottom'`、`'left'` 或 `'right'` 一端。滚动时会渲染更多行的虚拟化列表，在增长时会被一直跟随（`native` 模式下使用真实的快速滑动）。

```ts theme={null}
await app.scrollToEdge('feed', Edge.bottom);
await app.expectVisible({ text: 'No more posts' });
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待目标元素出现、App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

### `scrollToIndex(list, index, options?)`

```ts theme={null}
scrollToIndex(
  list: MatcherInput,
  index: number,
  options?: ScrollToIndexOptions,
): Promise<ElementSummary | undefined>
```

类型： [`MatcherInput`](./api-types#matcherinput) · [`ScrollToIndexOptions`](./api-types#scrolltoindexoptions) · [`ElementSummary`](./api-types#elementsummary)

将 `FlatList` / `FlashList` 的第 `index` 行滚入视图，无需 `getItemLayout`。

```ts theme={null}
await app.scrollToIndex('contacts', 120);

// 第 120 行位于列表中间
await app.scrollToIndex('contacts', 120, { viewPosition: 0.5 });
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `viewPosition` | `0` | 该行最终所在的位置：`0` 顶部，`0.5` 中间，`1` 底部 |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待目标元素出现、App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

`scrollTo`、`scrollToEdge` 和 `scrollToIndex` 分多步完成（最多 60 步）；每一步最多可等待一个调用超时时长，让 App 稳定下来。

## 手势

### `swipe(target, direction, options?)`

```ts theme={null}
swipe(
  target: MatcherInput,
  direction: Direction,
  options?: SwipeOptions,
): Promise<ElementSummary | undefined>
```

类型： [`MatcherInput`](./api-types#matcherinput) · [`Direction`](./api-constants#list) · [`SwipeOptions`](./api-types#swipeoptions) · [`ElementSummary`](./api-types#elementsummary)

一个带惯性的滑动手势。`direction` 指手指移动的方向。

```ts theme={null}
// 露出该行的删除按钮
await app.swipe({ id: 'todo-row', index: 0 }, Direction.left);

await app.swipe('carousel', Direction.left, { speed: Speed.fast, distance: 250 });
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `speed` | `'normal'` | `'slow'`、`'normal'` 或 `'fast'` |
| `distance` | | 手指移动多远 |
| `from` | | 手指的起点，在元素内归一化为 0–1 |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待目标元素出现、App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

### `drag(target, to, options?)`

```ts theme={null}
drag(
  target: MatcherInput,
  to: MatcherInput | { point: Point },
  options?: DragOptions,
): Promise<ElementSummary | undefined>
```

类型： [`MatcherInput`](./api-types#matcherinput) · [`Point`](./api-types#point) · [`DragOptions`](./api-types#dragoptions) · [`ElementSummary`](./api-types#elementsummary)

按住元素，移动到 `to`（一个元素）的中心或 `{ point }`（窗口坐标），然后松开。它需要真实的触摸：`js` 模式下不可用。

```ts theme={null}
// 重新排序：把第一行拖到第三行上
await app.drag({ id: 'todo-row', index: 0 }, { id: 'todo-row', index: 2 });

await app.drag('pin', { point: { x: 160, y: 420 } }, { holdMs: 300, speed: Speed.slow });
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `holdMs` | `800` | 移动之前按住多久 |
| `speed` | `'normal'` | `'slow'`、`'normal'` 或 `'fast'` |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待目标元素出现、App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

捏合、旋转以及其他多指手势见 [多指手势](https://docs.quiesco.dev/zh/api-gestures.md)。

## 导航

### `goBack(options?)`

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

类型： [`ActionOptions`](./api-types#actionoptions)

像用户在各平台上那样返回上一页：Android 按返回键（先关闭键盘），web 浏览器后退，iOS 点导航栏的返回按钮，没有时从左边缘右滑。使用 Expo Router 的 App，路由没有变化时会失败。

```ts theme={null}
await app.tap({ text: 'Buy milk' });
await app.goBack();
await app.expectVisible('todo-list-screen');
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待 App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

### `pressBack(options?)`

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

类型： [`ActionOptions`](./api-types#actionoptions)

Android 返回键，web 上是浏览器后退。iOS 上不可用（`ACTION_NOT_SUPPORTED`）：要在所有平台上运行的测试请使用 `goBack()`。

```ts theme={null}
// Android：用返回键关闭底部面板
await app.pressBack();
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待 App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

### `openUrl(url, options?)`

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

类型： [`ActionOptions`](./api-types#actionoptions)

打开一个深层链接：iOS 上在 App 内打开，Android 上用 `am start`，web 上通过地址栏。App 在后台时（`app.device.sendToBackground()`），像其他 App 那样从 App 外打开该链接；iOS 上会回答系统的「在 … 中打开？」确认。App 回到前台并空闲后 resolve。

```ts theme={null}
await app.openUrl('myapp://todos/42');
await app.expectText('detail-title', 'Buy milk');
```

| 选项 | 默认值 | 含义 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10 秒） | 等待 App 空闲的最长时间 |
| `sync` | `true` | `false`：这次调用前后不等待 App 空闲（[通用选项](https://docs.quiesco.dev/zh/api.md#call-options)） |
| `mode` | `actions.mode` | `'native'`：真实的触摸和键盘输入；`'js'`：调用组件的回调 |

## 其他输入

持有值的控件——开关、滑块、选择器、日期选择器——用 `setValue` 和 `adjustSlider` 设置，`accessibilityAction` 则执行屏幕阅读器的操作：见 [值控件](https://docs.quiesco.dev/zh/api-value-controls.md)。

## `js` 模式下

当操作调用回调而不是触摸屏幕时：

* 点击某个点会按下该处最上层的元素；
* 点击没有 `onPress` 的开关时，通过 `onValueChange` 切换它；
* `pressKey` 调用 `onSubmitEditing` / `onKeyPress`；
* `scrollToEdge` 直接设置滚动偏移；
* `from` 被忽略；
* `drag` 不受支持，因为它需要真实的触摸。


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