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

# クエリと状態

> 要素と要素ツリーを読み取り、アイドルを待ち、アプリをビジーにしているものを調べ、バックドアを呼び出し、アプリをリセットする。

画面の確認には、通常はアサーションを使います。このページのメソッドはそれ以外の用途のためのものです。要素の詳細をテストに読み込む、アプリを待つ、またはアプリがビジーな理由を調べる、アプリのバックドアで状態をセットアップする、そしてアプリをリセットする、といった用途です。

## 要素を読み取る

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

```ts theme={null}
query(
  target: MatcherInput,
  options?: CallOptions,
): Promise<ElementSummary[]>
```

型： [`MatcherInput`](./api-types#matcherinput) · [`CallOptions`](./api-types#calloptions) · [`ElementSummary`](./api-types#elementsummary)

マッチするすべての要素の[概要](#element-summary)をすぐに返します。ほかの呼び出しと同じくアプリがアイドルになるのは待ちますが、マッチが現れるのは待ちません。マッチがなければ`[]`です。

```ts theme={null}
const rows = await app.query({ id: 'todo-row' });
console.log(rows.map((r) => r.text));

// オンボーディングのヒントはあるか？（なくても失敗しない）
if ((await app.query('onboarding-tip')).length > 0) await app.tap('dismiss-tip');
```

| オプション | デフォルト | 意味 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10秒） | アプリがアイドルになるまで待つ最大時間 |
| `sync` | `true` | `false`：この呼び出しの前後でアプリのアイドルを待ちません（[共通オプション](https://docs.quiesco.dev/ja/api.md#call-options)） |

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

```ts theme={null}
attributes(
  target: MatcherInput,
  options?: CallOptions,
): Promise<ElementSummary>
```

型： [`MatcherInput`](./api-types#matcherinput) · [`CallOptions`](./api-types#calloptions) · [`ElementSummary`](./api-types#elementsummary)

要素が存在するまで待ったうえで、マッチした唯一の要素の[概要](#element-summary)を返します（Detoxの`getAttributes`）。複数の要素がマッチした場合は`MULTIPLE_MATCHES`で失敗します。

```ts theme={null}
const slider = await app.attributes('volume-slider');
expect(slider.value).toBeGreaterThan(0.5);

const row = await app.attributes({ id: 'todo-row', index: 0 });
console.log(row.rect); // { x, y, width, height }
```

| オプション | デフォルト | 意味 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10秒） | 要素が現れるまで待つ最大時間 |
| `sync` | `true` | `false`：この呼び出しの前後でアプリのアイドルを待ちません（[共通オプション](https://docs.quiesco.dev/ja/api.md#call-options)） |

### `getTree()`

```ts theme={null}
getTree(): Promise<TreeNode | null>
```

型： [`TreeNode`](./api-types#treenode)

セマンティックな型付きの要素ツリーをJSONで返します。各ノードは`type`、`testID`、`text`、`role`、`semanticType`、丸めた`rect`、そして`children`を持ちます。ツリーは失敗したテストにも添付されます（[成果物](https://docs.quiesco.dev/ja/api-artifacts.md)）。

```ts theme={null}
const tree = await app.getTree();
console.log(JSON.stringify(tree, null, 2));
```

## 要素の概要

`query`、`attributes`、そしてすべてのアサーションは要素の概要を返します。概要には次のフィールドがあります。

| フィールド | 意味 |
| - | - |
| `type` | ホストコンポーネント。例：`RCTView`、`RCTText`、`div` |
| `semanticType` | [セマンティックな型](https://docs.quiesco.dev/ja/api-matchers.md#semantic-types)（そのいずれかに当たる場合） |
| `testID` | テストID |
| `text` | 要素とその子孫のテキスト（後述） |
| `label` | アクセシビリティラベル |
| `role` | アクセシビリティロール |
| `value` | 値：入力欄のテキスト、スイッチの状態、スライダーやピッカーの値、または`accessibilityValue` |
| `checked` | スイッチ、チェックボックス、ラジオのチェック状態：`true`、`false`、`'mixed'`のいずれか |
| `enabled` | 無効なら`false` |
| `focused` | 入力フォーカスがあるかどうか |
| `rect` | ウィンドウ座標・論理単位での`{ x, y, width, height }` |
| `visible` | [表示されている](https://docs.quiesco.dev/ja/api-matchers.md#visibility)かどうか |
| `visibleRatio` | 面積のうち表示されている割合（0–1） |
| `props` | シリアライズ可能なprops（関数とReact要素は除く） |

概要の`text`（エラーメッセージや`elements.json`でも同じ）は、別々のテキストの間に空白を入れます。たとえばアバター、名前、学籍番号は`张 张三 S2024001`となります。1つのテキストの中の部分はつながったままです：`<Text>a<Text>b</Text></Text>`は`ab`になります。`text`での照合は変わらず、空白を入れないテキストで比べます。

## アプリを待つ

### `waitForIdle(options?)`

```ts theme={null}
waitForIdle(options?: WaitForIdleOptions): Promise<number>
```

型： [`WaitForIdleOptions`](./api-types#waitforidleoptions)

アプリがアイドルになるまで待ち、かかった時間（ミリ秒）で解決します。すべての呼び出しが既にこれを行っています。アプリのバックエンドを読むなど、ドライバーの外で作業する前に使ってください。

```ts theme={null}
await app.tap('sync-now');
const ms = await app.waitForIdle({ timeoutMs: 30_000 });
console.log(`sync took ${ms} ms`);
```

| オプション | デフォルト | 意味 |
| - | - | - |
| `timeoutMs` | `sync.timeoutMs`（10秒） | 待つ時間 |

### `getIdleState()`

```ts theme={null}
getIdleState(): Promise<TrackerState[]>
```

型： [`TrackerState`](./api-types#trackerstate)

すべての[トラッカー](https://docs.quiesco.dev/ja/sync.md#trackers)の現在の状態を返します。アプリを何がビジーにしているかがわかります。

```ts theme={null}
for (const t of await app.getIdleState()) {
  if (!t.idle) console.log(t.name, t.busy);
  // network [ 'GET https://api.example.com/todos (1200ms)' ]
}
```

各エントリーには、トラッカーの`name`、アイドルかどうかを表す`idle`、ビジーな理由を表す`busy`があります。

## セットアップとリセット

### `backdoor(name, ...args)`

```ts theme={null}
backdoor<T = JsonValue>(name: string, ...args: JsonValue[]): Promise<T>
```

型： [`JsonValue`](./api-types#jsonvalue)

アプリが`quiesco.backdoor`で登録したバックドアを呼び出し（[アプリ内API](https://docs.quiesco.dev/ja/api-in-app.md)）、その結果で解決します。引数と結果はJSONです（TypeScriptのinterfaceも可）。JSONでない引数は`NOT_SERIALIZABLE`で失敗します。

```ts theme={null}
// ログインフォームに入力せずにサインインする
await app.backdoor('loginAs', 'ann@example.com');

const todos = await app.backdoor<Todo[]>('todos.list');
```

`Backdoors`を宣言すると、名前、引数、結果が型チェックされます（[型付きバックドア](https://docs.quiesco.dev/ja/api-in-app.md#typed-backdoors)）。

### `reset()`

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

ソフトリセットです。アプリのストレージをクリアし、`onReset`フックを実行し、アプリを再マウントしてアイドルを待ちます。かかった時間（ミリ秒）で解決します。Quiescoは各テストの前にこれを行うので、テストでこれを呼ぶのは途中でやり直したいときだけです。

```ts theme={null}
await app.backdoor('loginAs', 'ann@example.com');
// ...
await app.reset();
await app.expectVisible('login-screen');
```

## プラットフォーム

### `platform`

テスト対象のプラットフォームで、`'ios'`、`'android'`、`'web'`のいずれかです。メソッドではなくプロパティです。Cucumberの`QuiescoWorld`では`this.platform`、また`session.platform`でも取得できます（[プラットフォームごとに異なるステップ](https://docs.quiesco.dev/ja/driver-pattern.md#per-platform-steps)）。

```ts theme={null}
if (app.platform === Platform.android) {
  // 戻るボタンでボトムシートを閉じる
  await app.pressBack();
} else {
  await app.tap('sheet-close');
}
```

一部のプラットフォームでだけ実行するテストには、[タグを付ける](https://docs.quiesco.dev/ja/api-tags.md)ほうが適しています。


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