> For the complete documentation index, see [llms.txt](https://help.autify.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.autify.com/nocode-web/autify-university/integrations/flexible-test-execution-via-api.md). Agents can also search and read this documentation through its [MCP server](https://help.autify.com/~gitbook/mcp).

# APIによる柔軟なテスト実行

## 概要

[`execute_scenarios` API](https://autifyhq.github.io/autify-api/#/scenario/post_project__project_id__execute_scenarios) を使うと、任意のシナリオをテストプランを作成せずに実行できます。 さらに、実行環境、実行タイプ（並列/直列）、URL置換もその場で指定することができます！

このドキュメントでは、このAPI を有効利用するための[Autify Command Line Interface (CLI)](https://help.autify.com/nocode-web/autify-university/integrations/autify-command-line-interface.md) を使った2つの方法、1. `autify web test run` による簡単な実行、2. `autify web api` を使ったよりカスタマイズ可能な実行、をご紹介します。

## 前提条件: Autify CLI をセットアップする

[Autify CLI をはじめてみよう](https://help.autify.com/nocode-web/autify-university/integrations/autify-command-line-interface.md#getting-started) に従って下さい。数分しかかかりません。

## 1. 1つのテストシナリオを1つの実行環境(`capability`)で実行

1つのテストシナリオを1つの実行環境(`capability`)で実行したいというのは、最も単純ですが最も使われるユースケースです。Autify CLI は`autify web test run` というこのユースケースに対してとても役に立つコマンドを提供しています。使い方は以下の様になります:

```sh-session
autify web test run https://app.autify.com/projects/0000/scenarios/0000 \
  --os Linux \
  --browser Chrome \
  --url-replacements http://example.com=http://example.net \
  --name "Sample"
```

このシンプルなコマンドで、指定されたテストシナリオをLinux/Chrome `capability` 上で、`http://example.com` を`http://example.net` にURL置換しながら実行して、そのテスト実行に"Sample" という名前をつけてくれます(これはAutify のUI でテスト結果を表示する時に使われます)。

利用可能な`capabilities` はお使いのワークスペースによって異なりますが、もし利用可能であれば以下の様な例を指定できます:

### デスクトップ `capabilities`

* `--os`:
  * `Linux / Windows Server / Windows / OS X`
* `--browser`:
  * `Chrome / Firefox / Safari / Edge`

### モバイル `capabilities`

* `--device`:
  * `iPhone X / Galaxy S20 / ...`
* `--device-type`:
  * `emulator / mobile`
* `--os-version`:
  * `11.0 / ...` (Android)
  * `15 / ...` (iOS)

お使いのワークスペースで利用可能な全ての`capabilitites` は以下のコマンドで確認できます。`autify web test run` は同じリストから指定されたオプションで絞り込んでくれます。なので、例えば`--os` と`--browser` だけでユニークな`capability` であれば、全てのオプションを指定する必要はありません:

```sh-session
$ autify web api list-capabilities --project-id 0000
[
  {
    "os": "Linux",
    "os_type": "linux",
    "os_version": null,
    "browser": "Chrome",
    "browser_type": "chrome",
    "browser_version": "126.0",
    "device": null,
    "device_type": "computer",
    "timezone": "JST"
  },
  {
    "os": "Windows",
    "os_type": "windows",
    "os_version": "10",
    "browser": "Edge",
    "browser_type": "edge",
    "browser_version": "124.0",
    "device": null,
    "device_type": "computer",
    "timezone": "JST"
  },
  ...
```

{% hint style="warning" %}
**`autify web test run` は全てのユースケースをサポートはしていません**

以下が`autify web test run` が今日現在サポートしていない幾つかのユースケースになります。もし需要があれば、これらをサポートすることも可能です:

* 複数のテストシナリオを1コマンドで実行
  * ワークアラウンド: 代わりに、テストシナリオ毎に`autify web test run` を実行する
* 複数のcapabilities を1コマンドで実行
  * ワークアラウンド: 代わりに、capability 毎に`autify web test run` を実行する
    {% endhint %}

## 2. 直接API 経由で実行

もし`autify web test run` がカバーしていないより複雑なユースケースが必要であれば、`autify web api` コマンド群を使って、自由に組み合わせることができます。このコマンド群はAutify API と1対1対応しています。

手順は以下の通りです。

1. 実行したいシナリオのシナリオIDを、Autify UI で表示されているURL、または`autify web api list-scenarios` から取得する。
2. `capabilities`（利用可能な実行環境）を、`autify web api list-capabilities` を使って取得する。
3. `autify web api execute-scenarios` を実行する。

実際に、詳しく見ていきましょう。

### シナリオIDの取得

まず、実行するシナリオのシナリオ IDを取得しましょう。

#### URLからシナリオIDを取得する

シナリオが以下のURLの場合、`{workspace_id}`がワークスペースID（旧プロジェクトID）、`{scenario_id}`がシナリオを実行する際に必要となるシナリオIDに当たります。

* <https://app.autify.com/projects/>`{workspace_id}`/scenarios/`{scenario_id}`

#### `scenario` API でシナリオ ID を取得する

シナリオIDは、`autify web api list-scenarios` を使って以下の様に一括で取得することも可能です:

```sh-session
autify web api list-scenarios --project-id {workspace_id}
```

もしご自身のHTTP クライアントを使いたい場合は、以下の様なリクエストを送ります:

```
GET /projects/{workspace_id}/scenarios HTTP/1.1
Accept: application/json, text/plain, */*
Authorization: Bearer YOUR_PERSONAL_ACCESS_TOKEN
Host: app.autify.com
```

### `capabilities` を取得する

次に、利用可能な実行環境の一覧を取得しましょう。`capabilities` とは、 `autify web api execute-scenarios` を実行する際に指定できる実行環境を表すものです。`capabilities` の一覧は`autify web api list-capabilities` を使って取得できます:

```sh-session
autify web api list-capabilities --project-id {workspace_id}
```

レスポンスには以下のようなオブジェクトの配列が返されます。各オブジェクトは、利用可能な `capabilities`（実行環境）を表しています。

```json
[
  {
    "os": "Linux",
    "os_type": "linux",
    "os_version": null,
    "browser": "Chrome",
    "browser_type": "chrome",
    "browser_version": "126.0",
    "device": null,
    "device_type": "computer",
    "timezone": "JST"
  },
  {
    "os": "Windows",
    "os_type": "windows",
    "os_version": "10",
    "browser": "Edge",
    "browser_type": "edge",
    "browser_version": "124.0",
    "device": null,
    "device_type": "computer",
    "timezone": "JST"
  }
]
```

もしご自身のHTTP クライアントを使いたい場合は、以下の様なリクエストを送ります:

```
GET /projects/{workspace_id}/capabilities HTTP/1.1
Accept: application/json, text/plain, */*
Authorization: Bearer YOUR_PERSONAL_ACCESS_TOKEN
Host: app.autify.com
```

### `autify web api execute-scenarios` を実行する

シナリオIDと `capabilities` が取得できたら、`autify web api execute-scenarios` を実行してテストを実行できます。これらの情報はJSON 形式のデータとして設定します。必要に応じて他のパラメータもあわせて指定しましょう。

<table data-search="false"><thead><tr><th>フィールド名</th><th>データ型</th><th></th><th>名前</th><th>必須項目？</th><th>デフォルト値</th></tr></thead><tbody><tr><td>scenarios</td><td>objectの配列</td><td></td><td></td><td>Yes</td><td></td></tr><tr><td></td><td></td><td>number</td><td>id</td><td>Yes</td><td></td></tr><tr><td>name (*1)</td><td>string</td><td></td><td></td><td>No</td><td>API execution + テストが実行された日時</td></tr><tr><td>execution_type</td><td>"parallel” / “sequential”</td><td></td><td></td><td>No</td><td>“parallel” (ワークスペースで並列実行が無効かされている場合は ”sequential” になります)</td></tr><tr><td>capabilities</td><td>objectの配列</td><td></td><td></td><td>Yes</td><td></td></tr><tr><td></td><td></td><td>string</td><td>os</td><td>No</td><td></td></tr><tr><td></td><td></td><td>string</td><td>os_version</td><td>No</td><td></td></tr><tr><td></td><td></td><td>string</td><td>device</td><td>No</td><td></td></tr><tr><td></td><td></td><td>string</td><td>browser</td><td>No</td><td></td></tr><tr><td></td><td></td><td>string</td><td>timezone (*2)</td><td>No</td><td></td></tr><tr><td>url_replacements</td><td>objectの配列</td><td></td><td></td><td>No</td><td>なし (URL置換は行われない)</td></tr><tr><td></td><td></td><td>string</td><td>pattern_url</td><td>Yes</td><td></td></tr><tr><td></td><td></td><td>string</td><td>replacement_url</td><td>Yes</td><td></td></tr></tbody></table>

\*1 この名前はテスト結果の表示だけのために使用されます。実行したいシナリオの名前とは関係ないことに注意してください。 \*2 timezone設定にはテスト環境による制限があります。詳しくはこちらをご参照ください [日本標準時 (JST) テスト実行](https://help.autify.com/nocode-web/autify-university/foundation/execution-environments/jst-test-execution.md)

#### 例

オプションは以下の通りです。

* Chrome/Linux と Edge/Window Server で複数のシナリオを直列実行する
* テスト結果の名前 "Order flow"
* URLの置換の設定 "<https://example.com>" → "<https://autify.com>"
* timezoneの設定 "UTC" (その他の可能な値は"Etc/UTC"と"Asia/Tokyo"と"JST"に限る）

```bash
autify web api execute-scenarios --project-id '{workspace_id}' --execute-scenarios-request '{
  "name": "Order flow", 
  "execution_type": "sequential",
  "url_replacements": [
    {
      "pattern_url": "https://example.com",
      "replacement_url": "https://autify.com",
      "timezone": "UTC"
    }
  ],
  "capabilities": [
    {
      "os": "Linux",
      "browser": "Chrome"
    },
    {
      "os": "Windows Server",
      "os_version": "2019",
      "browser": "Edge",
      "timezone": "UTC"
    }
  ],
  "scenarios": [
    {"id": 1234 },
    {"id": 2345 },
    {"id": 3456 },
    {"id": 4567 }
  ]
}'
```

結果はこちらです:

![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-5f05c57f87b145cdc56c07de1e1278ee4d44b140%2FScreen%20Shot%202022-05-11%20at%2015.53.40.png?alt=media)

![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-8c52f5a0be6e27dae0002fd19ede4aca0a1ae8e6%2FScreen%20Shot%202022-05-11%20at%2015.54.31.png?alt=media)

もしご自身のHTTP クライアントを使いたい場合は、以下の様なリクエストを送ります:

```
POST /projects/{workspace_id}/execute_scenarios HTTP/1.1
Accept: application/json, text/plain, */*
Authorization: Bearer YOUR_PERSONAL_ACCESS_TOKEN
Content-Type: application/json
Content-Length: 323
Host: app.autify.com

{"name":"Order flow","execution_type":"sequential","url_replacements":[{"pattern_url":"https://example.com","replacement_url":"https://autify.com"}],"capabilities":[{"os":"Linux","browser":"Chrome"},{"os":"Windows Server","os_version":"2019","browser":"Edge"}],"scenarios":[{"id":1234},{"id":2345},{"id":3456},{"id":4567}]}
```

## まとめ

これらのAPIを活用すれば、自在にテストを実行できます。その時々で実行したいテストを行うためにテストプランを作成することは必要なくなり、一度きりのテストプランをメンテナンスする必要はありません。皆様がAutifyでどのようにテストを実行されるか、楽しみにしています！


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.autify.com/nocode-web/autify-university/integrations/flexible-test-execution-via-api.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
