← 返回案例库
CUSTOMER STORY · 企业知识库怎么搭

Playwright MCP + Claude Code:读取真实页面写测试并自行修复

Write Playwright tests with Playwright MCP and Claude Code

Tools · 使用工具Claude Code
Source · 来源原作者 · DEV
Zaowuyun · 可参考能力资产库 + 上下文管理
01

谁在用

Who

一位测试工程师撰写的教程。

02

遇到什么问题

Challenge

手写端到端测试和定位器费时,且常因页面变化失效。

03

怎么用 AI 解决

Solution
  1. 把 Playwright MCP 服务器注册到 Claude Code,通过 storage state 载入已登录会话。
  2. 三段式提示词:环境信息、通俗场景描述、固定规则。
  3. 智能体通过无障碍树探索真实应用,写测试、运行并自行修复失败;人工审阅代码和场景含义。
04

结果如何

Results据原作者自述
  • 生成可运行测试,定位器来自真实页面,还自行修复了严格模式定位器错误
  • 长流程、变化数据、深层条件和 OAuth 仍困难;提交前把 .auth/ 加入 .gitignore

以上内容均由原作者自行报告,未经独立核实,也不代表造物云产品的效果。

05

提示词

Prompt

参考提示词(由 agentusecases.ai 整理为模板,非原帖原文)

中文

在本项目中配置 Playwright MCP,打开【应用网址】探索【用户流程,如注册和结账】。用稳定定位器写覆盖该流程的 Playwright 测试,运行直到通过。最后给我看测试文件和通过的运行输出。

ENGLISH

Set up Playwright MCP in this project, then open [app URL] and explore the flow for [user journey, e.g. sign up and checkout]. Write Playwright tests that cover it using stable locators, and run them until they pass. Finish by showing me the test files and the passing run output.