逐步指南,使用MCP Playwright服务器与MCP客户端windsurf/VS Code,支持UI和API测试
UI: nextjs.org
API: https://fakerestapi.azurewebsites.net/
创建一个名为playwright-mcp的空项目/文件夹
使用任意编辑器(windsurf/VS Code)打开,并通过运行以下命令初始化Playwright项目
配置MCP Playwright服务器
{
"mcpServers": {
"playwright": {
"command": "npx",
"args": [
"@playwright/mcp@latest"
]
}
}
}
使用可用工具导航到nextjs.org网站
分析内容并为nextjs网站的页脚链接创建一套测试
将测试放置在@testsfolder中
注意:生成的测试套件会根据所选LLM的不同而变化,相同的提示可能会产生不同的输出
接下来,需要在提示完成生成测试后运行测试
npx playwright test tests/nextjs-footer.spec.ts
npx playwright test tests/footer-links-validation.spec.ts
npx playwright test tests/footer-comprehensive.spec.ts
# 仅运行结构验证测试
npx playwright test tests/nextjs-footer.spec.ts --grep "should display all expected footer sections"
# 仅运行链接验证测试
npx playwright test tests/footer-links-validation.spec.ts --grep "should validate"
# 运行综合测试
npx playwright test tests/footer-comprehensive.spec.ts
npx playwright test --reporter=html
注意:生成的测试套件会根据所选LLM的不同而变化,相同的提示可能会产生不同的输出
npx playwright test tests/api/fakerest.spec.ts