基于
playwright和midscene.js自动化测试项目,给Playwright插上AI的翅膀,目前可以落地的AI自动化测试项目。
技术栈:
-
plywright Web UI自动化测试工具。
-
midscene.js 提供AI定位断言能力。
- 克隆项目到本地:
git clone https://github.com/autotestclass/playwright-mind- 安装依赖
cd playwright-mind
npm install- 安装运行浏览器
npx playwright install- 配置大模型
本项目默认使用
qwen-vl-max-latest模型, 经过验证可用,关键是免费。如果想其他模型请参考midscenejs官方配置。
阿里云百练:https://bailian.console.aliyun.com/
使用其他模型:https://midscenejs.com/zh/model-provider.html
在 .env 文件中配置环境变量:
export OPENAI_API_KEY="sk-your-key"
export OPENAI_BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1"
export MIDSCENE_MODEL_NAME="qwen-vl-max-latest"在项目的test目录,附带了bing-search-ai-example.spec.ts例子。
示例代码
import { expect } from "@playwright/test";
import { test } from "./fixture/fixture";
test.beforeEach(async ({ page }) => {
await page.goto("https://cn.bing.com");
});
test('search keyword on bing', async ({ page, ai, aiQuery, aiAssert }) => {
// 👀 输入关键字,执行搜索
await ai('搜索输入框输入"playwright"关键字,并回车');
await page.waitForTimeout(3000);
// 👀 找到列表里耳机相关的信息
const items = await aiQuery(
'string[], 搜索结果列表中包含"playwright"相关的标题'
);
console.log("search result", items);
console.log("search result number", items?.length);
// 断言大于 1 条搜索结果
expect(items?.length).toBeGreaterThan(1);
// 👀 用 AI 断言
await aiAssert('检查搜索结果列表第一条标题是否包含"playwright"字符串');
});三种关键方法:交互(.ai, .aiAction), 提取 (.aiQuery), 断言 (.aiAssert)。
.ai方法描述步骤并执行交互.aiQuery从 UI 中“理解”并提取数据,返回值是 JSON 格式,你可以尽情描述想要的数据结构.aiAssert来执行断言
运行测试
npx playwright test --headed tests/bing-search-ai-example.spec.ts
Running 1 test using 1 worker
✓ 1 [chromium] › baidu-search-ai-example.spec.ts:9:5 › search headphone on bing (52.1s)
search result [ 'Playwright 中文网', '快速入门Playwright框架:从零到自动化测试的第一 ...' ]
search result number 2
Midscene - report file updated: /Users/fnngj/zhpro/github/playwright-mind/midscene_run/report/playwright-merged-2025-01-10_00-44-50-464.html
Slow test file: [chromium] › baidu-search-ai-example.spec.ts (52.1s)
Consider splitting slow test files to speed up parallel execution
1 passed (55.3s)
Midscene - report file updated: /Users/fnngj/zhpro/github/playwright-mind/midscene_run/report/playwright-merged-2025-01-10_00-44-50-464.html测试报告
