選項
首頁首頁 Skill 瀏覽器自動化 vitest-midscene-e2e

透過 Midscene 強化 Vitest,實現橫跨 Web(Playwright)、Android(ADB)及 iOS(WDA)的 AI 驅動式 UI 測試。可為新專案建立架構、轉換現有專案,並透過自然語言的 UI 互動來建立/更新/除錯/執行端到端(E2E)測試。 觸發詞:寫測試、新增測試、建立測試、更新測試、修復測試、除錯測試、執行測試、端到端測試、Midscene 測試、新專案、轉換專案、初始化專案、寫測試、 加測試、建立測試、更新測試、修復測試、除錯測試、執行測試、新建專案、轉換專案。

...展開全部
20
更新時間 2026-08-25

關於vitest-midscene-e2e

vitest-midscene-e2e 透過整合 Midscene 來強化 Vitest 測試框架,藉此在 Web(Playwright Chromium)、Android(ADB 和 scrcpy)以及 iOS(WebDriverAgent)平台上,建構由 AI 驅動的自然語言端到端 UI 測試。 此方案解決了基於選取器的端到端測試(E2E)易碎性問題:測試人員無需將使用者流程分解為易受影響的點擊與輸入操作,而是將自然語言意圖傳遞給 Midscene 的代理程式,由其規劃並執行互動操作。 此技能能為新測試專案建立骨架、轉換現有專案,並透過雙語(英文與中文)觸發短語來建立、更新、除錯及執行測試。

工作流程首先透過內建腳本克隆標準範本,接著將當前專案與其進行比對,僅針對使用者所需的平台填入缺失的部分,同時不會覆寫現有設定,並將 .env.example 複製為 .env 供使用者填寫。 其核心原則是:使用者描述的 UI 步驟必須透過主要的 aiAct API 來實現,而非使用細粒度的 aiTap/aiInput/aiAssert 呼叫,讓 AI 負責處理規劃、斷言、資料擷取及等待。 該工具文件記載了共享相同 AI 方法的各平台專用代理類別、依據頁面或階段邊界將長提示拆分為多個階段、受 fileChooserAllowedDir 限制的提示驅動式檔案上傳(明確不建議使用專案根目錄或家目錄), aiActionContext 系統提示選項(用於設定測試人員的專業程度)、應避免的常見定位器錯誤,以及針對失敗情況的疑難排解參考指南。

目標使用者為撰寫跨平台端到端(E2E)測試的開發人員與品質保證(QA)工程師,他們希望在 Web、Android 及 iOS 平台上實現具彈性且採用自然語言的自動化測試。 執行此技能需預先設定環境變數(包括 Midscene 的 AI 模型憑證)及平台工具鏈,例如 Playwright、ADB 或 WebDriverAgent。此技能會執行克隆腳本並驅動測試執行,但其作用範圍僅限於合法的測試工作流程,並建議將檔案上傳目錄範圍縮小,而非暴露廣泛的路徑。

常見問題

它支援哪些平台?

Web 透過 Playwright Chromium、Android 透過 ADB 與 scrcpy,以及 iOS 透過 WebDriverAgent。在 Web 環境中,您可同時使用 ctx.agent 和 ctx.page;在 Android 與 iOS 環境中,則僅能使用 ctx.agent。這三種代理皆共享相同的 AI 方法。

如何編寫測試步驟?

請將使用者的意圖以自然語言形式傳遞給主要 API「aiAct」,而非將其分解為 aiTap、aiInput 或 aiAssert。aiAct 同時處理斷言、資料擷取及等待;已廢棄的 aiAction 應改用 aiAct 取代。

需要哪些設定?

請使用提供的腳本克隆範本代碼,安裝依賴項,並根據需求(包括 Midscene 的 AI 模型憑證)配置 .env 檔案(可從 .env.example 複製)。您還需要相關的平台工具鏈(Playwright、ADB/scrcpy 或 WebDriverAgent)。

如何安全地處理檔案上傳?

當 aiAct 提示要求上傳檔案時,請將 fileChooserAllowedDir 設定為包含該測試固定資料(fixtures)的最小目錄。該技能明確指出,請勿使用專案根目錄或家目錄。

若提示包含多個步驟該如何處理?

請依據頁面或階段邊界將其拆分為獨立的 aiAct 呼叫,以確保 AI 在流程中不會失去上下文,同時確保所有階段加總起來符合原始意圖。另有一份疑難排解參考指南,說明如何處理錯誤。

所有檔案

3 個檔案SKILL.md7.0KB檢視scripts/clone-boilerplate.sh1.2KB檢視references/troubleshooting.md2.2 KB檢視
在 GitHub 上查看

Modules

ModuleRole
VitestTypeScript test framework. Provides describe/it/expect/hooks for test organization, assertions, and lifecycle.
MidsceneAI-driven UI automation. Interacts with UI elements via natural language — no fragile selectors. Core API: aiAct.

Supported platforms:

  • Web — WebTest (Playwright Chromium): ctx.agent + ctx.page
  • Android — AndroidTest (ADB + scrcpy): ctx.agent only
  • iOS — IOSTest (WebDriverAgent): ctx.agent only

Workflow

Step 1: Clone boilerplate & ensure project ready

bash scripts/clone-boilerplate.sh

The boilerplate at ~/.midscene/boilerplate/vitest-all-platforms-demo/ is the canonical reference for project structure, configs, platform context classes, and test conventions. Compare the current project against it. If anything is missing, ask the user which platform(s) they need (Web / Android / iOS), then fill in what's missing using the boilerplate as the target state. Only include files for the requested platform(s). Do NOT overwrite existing configs or files. Copy .env.example from the boilerplate as .env if it doesn't exist, and prompt the user to fill in the env vars.

Step 2: Read the Midscene Agent API section below before writing tests

It contains mandatory rules for using aiAct — the primary API for all UI operations. Do NOT skip this step.

Step 3: Create, update, or run tests

Use the boilerplate's e2e/ directory and src/context/ as reference for patterns and conventions. Before running tests, ensure dependencies are installed and .env is configured. When debugging failures, check troubleshooting.md.

Midscene Agent API

ctx.agent is a platform-specific agent instance. All methods return Promises.

  • Web: PlaywrightAgent from @midscene/web/playwright
  • Android: AndroidAgent from @midscene/android
  • iOS: IOSAgent from @midscene/ios

All three agents share the same AI methods below.

Mandatory Rule: Use aiAct for User-Described Steps

When the user describes a UI action or state confirmation in natural language, you MUST use aiAct to implement it. Do NOT decompose user instructions into aiTap/aiInput/aiAssert or other fine-grained APIs. Pass the user's intent directly to aiAct and let Midscene's AI handle the planning and execution.

// User says: "type iPhone in the search box and click search"// WRONG — manually decomposing into fine-grained APIsawait ctx.agent.aiInput('search box', { value: 'iPhone' });await ctx.agent.aiTap('search button');// CORRECT — pass intent directly to aiActawait ctx.agent.aiAct('type "iPhone" in the search box, then click the search button');

Assertions, data extraction, and waiting should also be done via aiAct — it handles all of these. Do NOT use aiAssert, aiQuery, aiWaitFor, aiTap, or aiInput separately.

aiAct(taskPrompt, opt?) — Primary API

aiAct is the primary API for all UI operations and state confirmations. It accepts natural language instructions and autonomously plans and executes multi-step interactions.

// UI operationsawait ctx.agent.aiAct('type "iPhone" in the search box, then click the search button');await ctx.agent.aiAct('hover over the user avatar in the top right');// State confirmations / assertions — also use aiActawait ctx.agent.aiAct('verify the page shows "Login successful"');await ctx.agent.aiAct('verify the error message is visible');

Prompt-driven File Uploads (Web only)

When an aiAct prompt asks Midscene to upload files, pass fileChooserAllowedDir explicitly. Use the smallest directory containing that test case's fixtures, and refer to files relative to it in the prompt. Do not use the project root or a home directory. Replace ./fixtures below with the fixture directory relative to the test process working directory.

await ctx.agent.aiAct(  'click the upload button and upload avatar.png',  { fileChooserAllowedDir: './fixtures' },);

Phase splitting: If the task prompt is too long or covers multiple distinct stages, split it into separate aiAct calls — one per phase. Each phase should be a self-contained logical step, and all phases combined must match the user's original intent.

// Incorrect — prompt spans multiple pages and too many steps, AI may lose context mid-wayawait ctx.agent.aiAct('click the settings button in the top nav, go to settings page, find personal info and click into it, change email to "[email protected]", change phone to "13800000000", click save, wait for success');// Correct — split by page/stage boundary, each phase stays within one logical contextawait ctx.agent.aiAct('click the settings button in the top nav, go to settings page, find personal info and click into it');await ctx.agent.aiAct('change email to "[email protected]", change phone to "13800000000", click save');await ctx.agent.aiAct('verify the save success message appears');

aiAction is deprecated. Use aiAct or ai instead.

Common Mistakes

  • Vague locators — 'button' is ambiguous; use 'the blue "Submit" button at the top of the page'
  • Deprecated aiAction — use aiAct instead
  • Ambiguous multi-element targets — specify row/position: 'the delete button in the first product row'

Agent Configuration — aiActionContext

aiActionContext is a system prompt string appended to all AI actions performed by the agent. Use it to define the AI's role and expertise.

// Set via agentOptions in setup()const ctx = WebTest.setup('https://example.com', {  agentOptions: {    aiActionContext: 'You are a Web UI testing expert.',  },});

Good examples:

  • 'You are a Web UI testing expert.'
  • 'You are an Android app testing expert who is familiar with Chinese UI.'

Bad examples:

  • 'Click the login button.' — specific actions belong in aiAct(), not aiActionContext
  • 'The page is in Chinese.' — this is page description, not a system prompt

How to Look Up More

  1. In node_modules/@midscene/web, node_modules/@midscene/android, and node_modules/@midscene/ios, find the type definitions for the agent classes
  2. If types are not enough, follow the source references in the .d.ts files to read the implementation code in node_modules
  3. Download https://midscenejs.com/llms.txt, then use grep to search for the API or concept you need (the file is large, do not read it in full)

安裝 vitest-midscene-e2e

請下載並將技能檔案解壓縮至您的 .claude/skills/ 目錄中。

下載 ZIP

複製儲存庫並將技能檔案複製到您的專案中。

git clone https://github.com/web-infra-dev/midscene-skills/blob/main/skills/vitest-midscene-e2e/SKILL.md # Copy SKILL.md to your .claude/skills/ directory

複製 複製
快速設定: 將技能資料夾複製到 .claude/skills/,Claude 會自動偵測並使用該技能

相關技能

playwright-cli
更新時間 2026-06-29
frontend-testing-best-practices
更新時間 2026-07-07
Playwright Browser Automation
更新時間 2026-06-29
playwright-generate-test
更新時間 2026-06-29
OR