vitest-midscene-e2e
web-infra-dev/midscene-skills
通过 Midscene 增强 Vitest,支持在 Web(Playwright)、Android(ADB)和 iOS(WDA)平台上进行基于 AI 的 UI 测试。可为新项目生成模板、转换现有项目,并利用自然语言 UI 交互来创建、更新、调试和运行端到端(E2E)测试。 触发词:写测试、添加测试、创建测试、更新测试、修复测试、调试测试、运行测试、端到端测试、Midscene测试、新建项目、转换项目、初始化项目、写测试、 加测试、创建测试、更新测试、修复测试、调试测试、运行测试、新建工程、转换工程。
...展开全部关于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`。
需要哪些准备工作?
使用提供的脚本克隆模板代码,安装依赖项,并配置 .env 文件(从 .env.example 复制而来),其中需包含必要的变量,包括 Midscene 的 AI 模型凭据。您还需要相应的平台工具链(Playwright、ADB/scrcpy 或 WebDriverAgent)。
如何安全地处理文件上传?
当 aiAct 提示符上传文件时,请将 fileChooserAllowedDir 设置为包含该测试 fixture 的最小目录。该技能明确规定不得使用项目根目录或主目录。
如果一个提示涉及多个步骤该怎么办?
请按页面或阶段边界将其拆分为独立的 aiAct 调用,以确保 AI 在流程中不会丢失上下文,同时保证所有阶段组合起来符合原始意图。故障排除参考文档涵盖了错误调试的相关内容。
所有文件
3个文件SKILL.md7.0KB查看scripts/clone-boilerplate.sh1.2KB查看references/troubleshooting.md2.2 KB查看Modules
| Module | Role |
|---|---|
| Vitest | TypeScript test framework. Provides describe/it/expect/hooks for test organization, assertions, and lifecycle. |
| Midscene | AI-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.agentonly - iOS —
IOSTest(WebDriverAgent):ctx.agentonly
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:
PlaywrightAgentfrom@midscene/web/playwright - Android:
AndroidAgentfrom@midscene/android - iOS:
IOSAgentfrom@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
aiActto implement it. Do NOT decompose user instructions intoaiTap/aiInput/aiAssertor other fine-grained APIs. Pass the user's intent directly toaiActand 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');
aiActionis deprecated. UseaiActoraiinstead.
Common Mistakes
- Vague locators —
'button'is ambiguous; use'the blue "Submit" button at the top of the page' - Deprecated
aiAction— useaiActinstead - 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 inaiAct(), notaiActionContext'The page is in Chinese.'— this is page description, not a system prompt
How to Look Up More
- In
node_modules/@midscene/web,node_modules/@midscene/android, andnode_modules/@midscene/ios, find the type definitions for the agent classes - If types are not enough, follow the source references in the
.d.tsfiles to read the implementation code innode_modules - Download https://midscenejs.com/llms.txt, then use
grepto search for the API or concept you need (the file is large, do not read it in full)





首页
