blog.dopana

戻る

単純なユーザーフローをテストするために数十行のコードを書くことに疲れたことはありませんか?UIが変わるとすべてのテストが壊れることにイライラしたことは?e2eは、これらの問題を解決するためにAIを使用する次世代のエンドツーエンドテストフレームワークです。

この記事では、e2eがどのように機能するか、そしてなぜテストの書き方を変える可能性があるのかを探ります。

なぜe2eが必要なのか#

サインアップフローをテストしていると想像してください。PlaywrightやCypressなどの従来のフレームワークでは、次のように書く必要があります:

await page.click('button[name="signup"]');
await page.fill('input[name="email"]', 'test@example.com');
await page.fill('input[name="password"]', 'password123');
await page.click('button[type="submit"]');
await expect(page.locator('.welcome-message')).toBeVisible();
typescript

ボタン名が変わったりレイアウトが変わったりすると、テストが失敗します。コードを手動で修正する必要があります。

e2eでは、AIに伝えるだけです:

await agent.act('sign up with email {email}', {
  params: { email: 'test@example.com' }
});
await agent.assert('the welcome screen is showing');
typescript

AIは自動的にアクションを実行する方法を理解し、結果をチェックします。UIが変わると、AIは自動的に適応します。

e2eとは何ですか?#

e2eは、今日のソフトウェア出荷速度に対応するために構築された、Webおよびモバイルアプリ向けのオープンソースのエンドツーエンドテストフレームワークです。主な特徴:

  • AI駆動: AIモデルを使用してアクションを実行し、結果をチェック
  • 決定論的: ロケーターとアサーションを持つ従来のAPIもサポート
  • ハイブリッド: 同じテストで両方のアプローチを混在可能
  • クロスプラットフォーム: Web(Chromium)、iOS、Androidでテストを実行

e2eの仕組み#

e2eには3つの主要なコンポーネントがあります:

Test FileTargetsWeb / iOS / AndroidAgent Stepsagent.act / agent.assertCacheReplay verified stepsEnginesPlaywright for Webagent-device for MobileAI ModelExecute ActionsJudge ScreenNo Model CallsFast & Cheap

ターゲット#

各テストはターゲットごとに1回実行されます:

  • Web: Playwright経由のChromium、Firefox、Safari
  • モバイル: agent-device経由のiOSシミュレーターとAndroidエミュレーター
  • カスタム: 他のプラットフォーム用に独自のエンジンを記述可能

エージェントステップ#

  • agent.act: 自然言語で目標を完了
  • agent.assert: 画面上の条件をチェック
  • agent.waitFor: 条件がtrueになるのを待機
  • agent.extract: 画面から構造化データを抽出

キャッシュ#

検証されたactステップは記録され、AIモデル呼び出しなしでキャッシュから再生されます。これにより:

  • トークンコストを削減
  • テスト実行を高速化
  • アプリが変わっていない場合、古いアクションを保持

e2eの始め方#

クイックセットアップ#

npx e2e init
bash

このコマンドは:

  • e2e.config.ts設定ファイルを作成
  • サンプルテストを作成
  • 依存関係を追加
  • コーディングエージェントをセットアップ

Web設定#

e2e.config.ts
import type { E2EConfig } from 'e2e';
import { web } from '@e2e-dev/web';
import { gateway } from 'ai';

export default {
  agents: { default: { model: gateway('openai/gpt-6-luna-fast') } },
  targets: [{ engine: web(), app: { url: 'http://localhost:3000' } }],
} satisfies E2EConfig;
typescript

最初のテスト#

tests/example.e2e.ts
import { test } from '@e2e-dev/web';
import { expect } from 'e2e';

test('app opens', async ({ app, browser }) => {
  await app.open('/');
  await expect(browser.locator('body')).toBeVisible();
});
typescript

テストを実行:

npx e2e run
bash

AIでテストを書く#

エージェントアクション#

import { test } from '@e2e-dev/web';
import { expect } from 'e2e';

test('a visitor signs up for a trial', async ({ app, agent, screen }) => {
  await app.open('/');

  await agent.act('sign up for a free trial as {name} with email {email}', {
    params: { name: 'Ada Lovelace', email: 'ada@example.test' },
  });

  await agent.assert('the welcome screen greets Ada by name');
  await expect(screen.getByRole('status')).toContainText('trial');
});
typescript

決定論的とAIの混合#

test('todos survive a filter round-trip', async ({ app, agent, screen }) => {
  await app.open('/todos');

  // AIは複雑な相互作用を処理
  await agent.act('add todos "Buy milk" and "Walk the dog", complete the first');
  await agent.assert('only one todo remains open');

  // 正確な値の決定論的チェック
  await screen.getByRole('tab', 'Done').tap();
  await expect(screen.getByTestId('todo')).toHaveCount(1);
});
typescript

両方のアプローチの組み合わせ#

e2eでは、同じテストでエージェントステップと決定論的APIを混在させることができます:

  • エージェントステップ: 必要な正確なアクションがわからない場合
  • 決定論的API: 正確な値をチェックする必要がある場合、またはロケーターを知っている場合
// エージェントは複雑なフローを処理
await agent.act('complete checkout with the test card');

// 正確な結果の決定論的チェック
await expect(screen.getByText('Order #12345')).toBeVisible();
typescript

e2eの利点#

時間の節約#

セレクターと待機のスクリプトの代わりに目標を書きます。UIが変わると、エージェントが適応するため、テストの作成と修正に費やす時間が減ります。

リスクの削減#

Webとモバイルで、ユーザーが依存するフローをより多くカバーし、本番環境に到達する前に回帰をキャッチします。

安価な実行#

検証されたagent.actステップは、モデル呼び出しなしでキャッシュから再生されます。トークン請求や遅いCIなしで、より多くのフローをテストできます。

認証とシークレット#

e2eにはシークレットの組み込みサポートがあります:

import { credentials } from 'e2e';

const member = credentials.user('member');
await agent.act('Sign in', {
  params: { username: member.username, password: member.password },
});
typescript

シークレットはセキュリティのためにモデル入力から編集されます。

モバイルテスト#

e2eはiOSとAndroidもサポートしています:

e2e.config.ts
import { mobile } from '@e2e-dev/mobile';

export default {
  targets: [{
    name: 'ios',
    engine: mobile({ platform: 'ios' }),
    app: { bundleId: 'com.yourapp' }
  }],
  workers: 1,
} satisfies E2EConfig;
typescript
tests/mobile.e2e.ts
import { test } from '@e2e-dev/mobile';

test('the agent opens General', async ({ agent, app, device }) => {
  await app.open();
  await agent.act('open {section} settings', { params: { section: 'General' } });

  await agent.assert('the General settings screen is showing');
  await expect(device.locator('role=NavigationBar id=General')).toBeVisible();
});
typescript

コーディングエージェント#

e2eは、Claude、Cursor、Codexなどのコーディングエージェントで動作するように設計されています。セットアップ後、次のことができます:

  • “Add a test for checkout”
  • “Why did this run fail?”
  • “Debug this test”

エージェントは自動的にスキルを読み取り、必要なアクションを実行します。

結論#

e2eは、エンドツーエンドテストにおける大きな飛躍を表しています。AIと決定論的APIを組み合わせることで、次のことが可能になります:

  • テストをより速く書く
  • テストをより簡単に維持する
  • より多くのユーザーフローをカバーする
  • 複数のプラットフォームでテストを実行する

アプリケーションをテストするための現代的なアプローチを探している場合、e2eは試す価値があります。

References#