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

# WebView

> アプリ内に表示されたWebページの要素を探し、タップし、入力し、確認します。ページ内でスクリプトも実行できます。

`app.webview()`は、WebViewに表示されたページの要素を扱います。iOS / Androidではreact-native-webview、Webでは`<iframe>`です。react-native-webviewにはWeb実装がないため、アプリはWebではiframeを自分で描画します（例：`MyWebView.web.tsx`の中で）。

```ts theme={null}
const help = app.webview('help');

await help.element({ css: '#name' }).type('Bob');
await help.element({ text: 'Say hello' }).tap();
await help.element({ id: 'result' }).expectText('Hello, Bob');
```

* **どの呼び出しも**、アプリがアイドルになり、ページが落ち着くのを待ちます。落ち着くとは、読み込みが終わり、ページの`fetch` / `XMLHttpRequest`が実行中でないことです（Quiescoがページに追加するスクリプトで数えます）。ページのタイマーは追跡しませんが、アサーションはいずれにせよタイムアウトまで再試行します。
* **対象はWebViewのメインフレームだけです。** ページ内のiframeやshadow DOMには届きません。
* **iOSとAndroidでは`@quiesco/native`が必要です。** スクリプトはネイティブのWebView経由で実行されるので、アプリに`onMessage`は不要です。ない場合、呼び出しは`ACTION_NOT_SUPPORTED`で失敗します。

どのメソッドも、最後の引数に`timeoutMs`と`sync`の[呼び出しオプション](https://docs.quiesco.dev/ja/api.md#call-options)を受け取ります。

## ページとその要素を探す

### `webview(target?)`

```ts theme={null}
webview(target?: MatcherInput): WebView
```

型： [`MatcherInput`](./api-types#matcherinput) · [`WebView`](./api-webview#webview)

`target`に一致するWebViewです。testIDなど、任意の[マッチャー](https://docs.quiesco.dev/ja/api-matchers.md)を指定できます。引数なしなら、画面上の唯一のWebViewです。

```ts theme={null}
const help = app.webview('help');

// 画面上の唯一のWebView
const page = app.webview();
```

### `element(query)`

```ts theme={null}
element(query: WebElementQuery): WebElement
```

型： [`WebElementQuery`](./api-types#webelementquery) · [`WebElement`](./api-webview#element)

ページ内の要素です。`query`には`css`、`xpath`、`id`、`text`のうちちょうど1つと、複数の要素がマッチする場合は省略可能な`index`を指定します。

```ts theme={null}
help.element({ css: '#name' });
help.element({ xpath: '//input[@name="name"]' });
help.element({ id: 'result' });

// 空白をまとめた要素のテキストとの完全一致
help.element({ text: 'Say hello' });
help.element({ text: /^Loading/ });

// 複数のマッチのうち2番目
help.element({ css: '.faq-item', index: 1 });
```

| フィールド | 意味 |
| - | - |
| `css` | CSSセレクター |
| `xpath` | XPath式 |
| `id` | 要素の`id`属性 |
| `text` | 空白をまとめた要素のテキストとの完全一致、またはRegExp。マッチする最も内側の要素 |
| `index` | 何番目のマッチか（`0`始まり） |

## 要素を操作する

### `tap(options?)`

```ts theme={null}
tap(options?: CallOptions): Promise<PageElement>
```

型： [`CallOptions`](./api-types#calloptions) · [`PageElement`](./api-types#pageelement)

要素が見える位置までスクロールしてから、実際にタップします。iOS / Androidでは画面上の要素の位置をタッチし、WebではPlaywrightの入力を使います。

```ts theme={null}
await help.element({ text: 'Contact us' }).tap();
```

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

複数の要素がマッチすると直ちに`MULTIPLE_MATCHES`で失敗し、表示されていて有効なマッチがタイムアウトまでに現れなければ`ELEMENT_NOT_FOUND`で失敗します。

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

```ts theme={null}
type(text: string, options?: CallOptions): Promise<PageElement>
```

型： [`CallOptions`](./api-types#calloptions) · [`PageElement`](./api-types#pageelement)

フィールドをタップし（ユーザーの操作と同じくフォーカスが入ります）、キャレット位置に`text`を挿入します（`execCommand('insertText')`。入力時と同じ`input`イベントが発生します）。ソフトウェアキーボードからの入力ではありません。

```ts theme={null}
await help.element({ css: '#name' }).type('Alice');
```

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

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

```ts theme={null}
replaceText(text: string, options?: CallOptions): Promise<PageElement>
```

型： [`CallOptions`](./api-types#calloptions) · [`PageElement`](./api-types#pageelement)

フィールドをタップし、値全体を`text`で置き換えます。

```ts theme={null}
await help.element({ xpath: '//input[@name="name"]' }).replaceText('Bob');
```

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

### `scrollIntoView(options?)`

```ts theme={null}
scrollIntoView(options?: CallOptions): Promise<PageElement>
```

型： [`CallOptions`](./api-types#calloptions) · [`PageElement`](./api-types#pageelement)

要素が見える位置までページをスクロールします。

```ts theme={null}
await help.element({ css: '#footer' }).scrollIntoView();
```

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

### `screenshot(options?)`

```ts theme={null}
screenshot(options?: CallOptions): Promise<Buffer>
```

型： [`CallOptions`](./api-types#calloptions)

先に要素が見える位置までスクロールしてから撮る、要素のPNGです。WebViewの表示領域内にある部分が写ります。詳しくは[スクリーンショット](https://docs.quiesco.dev/ja/api-screenshots.md)を参照してください。

```ts theme={null}
const png = await help.element({ css: '#chart' }).screenshot();
```

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

## アサーション

アプリ自体の[アサーション](https://docs.quiesco.dev/ja/api-assertions.md)と同じく、成り立つか呼び出しのタイムアウトが切れるまで再試行します。

### `expectText(text, options?)`

```ts theme={null}
expectText(text: TextQuery, options?: CallOptions): Promise<PageElement>
```

型： [`TextQuery`](./api-types#textquery) · [`CallOptions`](./api-types#calloptions) · [`PageElement`](./api-types#pageelement)

唯一のマッチのテキストが`text`と等しい（空白はまとめる）、またはRegExpにマッチすることを確認します。入力欄では値を比べます。

```ts theme={null}
await help.element({ id: 'result' }).expectText('Hello, Bob');
await help.element({ css: '#name' }).expectText(/^Bo/);
```

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

### `expectExists(options?)`

```ts theme={null}
expectExists(options?: CallOptions): Promise<PageElement[]>
```

型： [`CallOptions`](./api-types#calloptions) · [`PageElement`](./api-types#pageelement)

表示されているかどうかにかかわらず、少なくとも1つの要素がマッチすることを確認します。

```ts theme={null}
await help.element({ css: 'form#contact' }).expectExists();
```

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

### `expectNotExists(options?)`

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

型： [`CallOptions`](./api-types#calloptions)

マッチする要素がないことを確認します。

```ts theme={null}
await help.element({ css: '.error' }).expectNotExists();
```

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

### `expectVisible(options?)`

```ts theme={null}
expectVisible(options?: CallOptions): Promise<PageElement[]>
```

型： [`CallOptions`](./api-types#calloptions) · [`PageElement`](./api-types#pageelement)

マッチが表示されていることを確認します。表示されているとは、ページのビューポート内にあり（スクロールで外れていない）、CSSで隠されていないことです。

```ts theme={null}
await help.element({ text: 'Thanks for your message' }).expectVisible();
```

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

### `expectNotVisible(options?)`

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

型： [`CallOptions`](./api-types#calloptions)

どのマッチも表示されていないことを確認します。いずれもスクロールで外れているか、CSSで隠されているか、存在しない状態です。

```ts theme={null}
await help.element({ text: /^Loading/ }).expectNotVisible();
```

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

## ページ

### `runScript(body, args?, options?)`

```ts theme={null}
runScript<T extends JsonValue = JsonValue>(
  body: string,
  args?: JsonValue[],
  options?: CallOptions,
): Promise<T>
```

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

ページ内で関数本体を実行し、その値をJSONで返します。`args`は`arguments`として渡され、返されたPromiseは待たれます。

```ts theme={null}
const title = await help.runScript<string>('return document.title');

const count = await help.runScript<number>(
  'return document.querySelectorAll(arguments[0]).length',
  ['.faq-item'],
);

// Promiseは待たれる
const status = await help.runScript<number>('return fetch("/api/ping").then((r) => r.status)');
```

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

ページで例外が発生すると、呼び出しは`APP_ERROR`で失敗します。`runScript`は`new Function`を使うため、ページのContent-Security-Policyによっては禁止されることがあります。

### `url(options?)`

```ts theme={null}
url(options?: CallOptions): Promise<string>
```

型： [`CallOptions`](./api-types#calloptions)

ページのURL（`location.href`）です。

```ts theme={null}
await help.element({ text: 'Pricing' }).tap();
expect(await help.url()).toMatch(/\/pricing$/);
```

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

### `title(options?)`

```ts theme={null}
title(options?: CallOptions): Promise<string>
```

型： [`CallOptions`](./api-types#calloptions)

ページのタイトル（`document.title`）です。

```ts theme={null}
expect(await help.title()).toBe('Help center');
```

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


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