vitest-midscene-e2e
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseVitest Midscene E2E
Vitest Midscene E2E
Modules
模块
| Module | Role |
|---|---|
| Vitest | TypeScript test framework. Provides |
| Midscene | AI-driven UI automation. Interacts with UI elements via natural language — no fragile selectors. Core API: |
Supported platforms:
- Web — (Playwright Chromium):
WebTest+ctx.agentctx.page - Android — (ADB + scrcpy):
AndroidTestonlyctx.agent - iOS — (WebDriverAgent):
IOSTestonlyctx.agent
| 模块 | 作用 |
|---|---|
| Vitest | TypeScript测试框架。提供 |
| Midscene | AI驱动的UI自动化工具。通过自然语言与UI元素交互——无需脆弱的选择器。核心API: |
支持的平台:
- Web — (Playwright Chromium):
WebTest+ctx.agentctx.page - Android — (ADB + scrcpy):仅支持
AndroidTestctx.agent - iOS — (WebDriverAgent):仅支持
IOSTestctx.agent
Workflow
工作流程
Step 1: Clone boilerplate & ensure project ready
步骤1:克隆模板项目并确保项目就绪
bash
bash scripts/clone-boilerplate.shThe boilerplate at 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 from the boilerplate as if it doesn't exist, and prompt the user to fill in the env vars.
~/.midscene/boilerplate/vitest-all-platforms-demo/.env.example.envbash
bash scripts/clone-boilerplate.sh位于的模板项目是项目结构、配置、平台上下文类和测试规范的标准参考。请将当前项目与模板进行对比。如果有缺失内容,请询问用户需要支持哪些平台(Web / Android / iOS),然后以模板为基准补充缺失的内容。仅包含用户指定平台的相关文件,请勿覆盖现有配置或文件。如果文件不存在,从模板中复制并重命名为,并提示用户填写环境变量。
~/.midscene/boilerplate/vitest-all-platforms-demo/.env.env.example.envStep 2: Read the Midscene Agent API section below before writing tests
步骤2:编写测试前请阅读下方的Midscene Agent API章节
It contains mandatory rules for using — the primary API for all UI operations. Do NOT skip this step.
aiAct该章节包含使用的强制规则——是所有UI操作的核心API,请务必不要跳过此步骤。
aiActaiActStep 3: Create, update, or run tests
步骤3:创建、更新或运行测试
Use the boilerplate's directory and as reference for patterns and conventions. Before running tests, ensure dependencies are installed and is configured. When debugging failures, check troubleshooting.md.
e2e/src/context/.env请参考模板项目的目录和目录中的模式与规范。运行测试前,请确保已安装依赖并配置好文件。调试失败时,请查看troubleshooting.md。
e2e/src/context/.envMidscene Agent API
Midscene Agent API
ctx.agent- Web: from
PlaywrightAgent@midscene/web/playwright - Android: from
AndroidAgent@midscene/android - iOS: from
IOSAgent@midscene/ios
All three agents share the same AI methods below.
ctx.agent- Web:来自的
@midscene/web/playwrightPlaywrightAgent - Android:来自的
@midscene/androidAndroidAgent - iOS:来自的
@midscene/iosIOSAgent
这三种Agent共享以下AI方法。
Mandatory Rule: Use aiAct
for User-Described Steps
aiAct强制规则:使用aiAct
实现用户描述的步骤
aiActWhen the user describes a UI action or state confirmation in natural language, you MUST useto implement it. Do NOT decompose user instructions intoaiAct/aiTap/aiInputor other fine-grained APIs. Pass the user's intent directly toaiAssertand let Midscene's AI handle the planning and execution.aiAct
typescript
// User says: "type iPhone in the search box and click search"
// WRONG — manually decomposing into fine-grained APIs
await ctx.agent.aiInput('search box', { value: 'iPhone' });
await ctx.agent.aiTap('search button');
// CORRECT — pass intent directly to aiAct
await ctx.agent.aiAct('type "iPhone" in the search box, then click the search button');Assertions, data extraction, and waiting should also be done via — it handles all of these. Do NOT use , , , , or separately.
aiActaiAssertaiQueryaiWaitForaiTapaiInput**当用户用自然语言描述UI操作或状态验证时,必须使用来实现。**请勿将用户指令拆解为aiAct/aiTap/aiInput或其他细粒度API。直接将用户的意图传递给aiAssert,由Midscene的AI负责规划和执行。aiAct
typescript
// 用户需求:"在搜索框中输入iPhone并点击搜索"
// 错误示例——手动拆解为细粒度API
await ctx.agent.aiInput('search box', { value: 'iPhone' });
await ctx.agent.aiTap('search button');
// 正确示例——直接将意图传递给aiAct
await ctx.agent.aiAct('type "iPhone" in the search box, then click the search button');断言、数据提取和等待操作也应通过完成——它会处理所有这些场景。请勿单独使用、、、或。
aiActaiAssertaiQueryaiWaitForaiTapaiInputaiAct(taskPrompt, opt?) — Primary API
aiAct(taskPrompt, opt?) —— 核心API
aiActtypescript
// UI operations
await 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 aiAct
await ctx.agent.aiAct('verify the page shows "Login successful"');
await ctx.agent.aiAct('verify the error message is visible');Phase splitting: If the task prompt is too long or covers multiple distinct stages, split it into separate calls — one per phase. Each phase should be a self-contained logical step, and all phases combined must match the user's original intent.
aiActtypescript
// Incorrect — prompt spans multiple pages and too many steps, AI may lose context mid-way
await 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 "test@example.com", change phone to "13800000000", click save, wait for success');
// Correct — split by page/stage boundary, each phase stays within one logical context
await 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 "test@example.com", change phone to "13800000000", click save');
await ctx.agent.aiAct('verify the save success message appears');is deprecated. UseaiActionoraiActinstead.ai
**是所有UI操作和状态验证的核心API。**它接受自然语言指令,并自主规划和执行多步交互。
aiActtypescript
// UI操作示例
await 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');
// 状态验证/断言示例——同样使用aiAct
await ctx.agent.aiAct('verify the page shows "Login successful"');
await ctx.agent.aiAct('verify the error message is visible');**阶段拆分:**如果任务指令过长或包含多个不同阶段,请将其拆分为多个独立的调用——每个阶段对应一个调用。每个阶段应是一个独立的逻辑步骤,所有阶段组合起来需与用户的原始意图一致。
aiActtypescript
// 错误示例——指令跨多个页面且步骤过多,AI可能在执行过程中丢失上下文
await 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 "test@example.com", change phone to "13800000000", click save, wait for success');
// 正确示例——按页面/阶段边界拆分,每个阶段保持在单一逻辑上下文中
await 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 "test@example.com", change phone to "13800000000", click save');
await ctx.agent.aiAct('verify the save success message appears');已被弃用,请使用aiAction或aiAct替代。ai
Common Mistakes
常见错误
- Vague locators — is ambiguous; use
'button''the blue "Submit" button at the top of the page' - Deprecated — use
aiActioninsteadaiAct - Ambiguous multi-element targets — specify row/position:
'the delete button in the first product row'
- 模糊定位器 —— 表述模糊,应使用
'button'这类明确描述'页面顶部的蓝色“提交”按钮' - 使用已弃用的—— 请使用
aiAction替代aiAct - 多元素目标模糊 —— 请指定行/位置:
'第一行产品对应的删除按钮'
Agent Configuration — aiActionContext
aiActionContextAgent配置 —— aiActionContext
aiActionContextaiActionContexttypescript
// 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:
- — specific actions belong in
'Click the login button.', notaiAct()aiActionContext - — this is page description, not a system prompt
'The page is in Chinese.'
aiActionContexttypescript
// 在setup()中通过agentOptions设置
const ctx = WebTest.setup('https://example.com', {
agentOptions: {
aiActionContext: 'You are a Web UI testing expert.',
},
});优秀示例:
'You are a Web UI testing expert.''You are an Android app testing expert who is familiar with Chinese UI.'
错误示例:
- —— 具体操作应放在
'Click the login button.'中,而非aiAct()aiActionContext - —— 这是页面描述,不属于系统提示
'The page is in Chinese.'
How to Look Up More
如何查找更多信息
- In ,
node_modules/@midscene/web, andnode_modules/@midscene/android, find the type definitions for the agent classesnode_modules/@midscene/ios - If types are not enough, follow the source references in the files to read the implementation code in
.d.tsnode_modules - Download https://midscenejs.com/llms.txt, then use to search for the API or concept you need (the file is large, do not read it in full)
grep
- 在、
node_modules/@midscene/web和node_modules/@midscene/android中查找Agent类的类型定义node_modules/@midscene/ios - 如果类型定义不够详细,可通过文件中的源引用,查看
.d.ts中的实现代码node_modules - 下载https://midscenejs.com/llms.txt,使用`grep`搜索你需要的API或概念(文件较大,请勿完整阅读)