当前位置: 首页 > news >正文

使用Playwright MCP探索网站并编写测试

安装Playwright MCP

新建项目

mkdir playwright-mcp-test

使用Cursor打开playwright-mcp-test

打开终端输入命令

mkdir -p .cursor && touch .cursor/mcp.json
{"mcpServers": {"playwright": {"command": "npx","args": ["@playwright/mcp@latest"]}}
}

然后在Cursor Setting打开MCP开关
在这里插入图片描述

编写文档

执行命令

mkdir -p .github && touch .github/generate_tests.prompt.md
---
tools: ['playwright']
mode: 'agent'
---- You are a playwright test generator.
- You are given a scenario and you need to generate a playwright test for it.
- DO NOT generate test code based on the scenario alone. 
- DO run steps one by one using the tools provided by the Playwright MCP.
- When asked to explore a website:1. Navigate to the specified URL2. Explore 1 key functionality of the site and when finished close the browser.3. Implement a Playwright TypeScript test that uses @playwright/test based on message history using Playwright's best practices including role based locators, auto retrying assertions and with no added timeouts unless necessary as Playwright has built in retries and autowaiting if the correct locators and assertions are used.
- Save generated test file in the tests directory
- Execute the test file and iterate until the test passes
- Include appropriate assertions to verify the expected behavior
- Structure tests properly with descriptive test titles and comments

执行测试

在Cursor对话框输入

Explore https://debs-obrien.github.io/playwright-movies-app

在这里插入图片描述

可以看到Cursor使用Playwright MCP导航到网站并使用浏览器像真实用户一样探索应用。

然后继续生成代码进行测试
在这里插入图片描述

第1次执行测试报告
在这里插入图片描述

然后AI会自动修复测试失败的代码,然后生成总结和说明文档,并最终测试通过

在这里插入图片描述
在这里插入图片描述

生成的测试代码

import { test, expect } from '@playwright/test';test.describe('Movie App Search Functionality', () => {test('should display popular movies and handle search with no results', async ({ page }) => {// Navigate to the movie appawait page.goto('https://debs-obrien.github.io/playwright-movies-app');// Verify the page loads correctlyawait expect(page).toHaveTitle('Popular Movies');// Verify popular movies section is displayedawait expect(page.getByRole('heading', { name: 'Popular' })).toBeVisible();await expect(page.getByRole('heading', { name: 'movies' })).toBeVisible();// Verify movie grid is present with moviesconst moviesList = page.getByRole('list', { name: 'movies' });await expect(moviesList).toBeVisible();// Verify at least one movie is displayed (check for first movie)const firstMovie = moviesList.getByRole('listitem').first();await expect(firstMovie).toBeVisible();// Test search functionality - click on search area to activate inputconst searchArea = page.getByRole('search');await searchArea.click();const searchInput = page.getByRole('textbox', { name: 'Search Input' });// Search for a term that should have no resultsawait searchInput.fill('Batman');await searchInput.press('Enter');// Verify navigation to search results pageawait expect(page).toHaveURL(/.*\/search\?searchTerm=Batman&page=1/);// Verify "no results" message is displayedawait expect(page.getByRole('heading', { name: 'Sorry!' })).toBeVisible();await expect(page.getByRole('heading', { name: 'There were no results for Batman...' })).toBeVisible();// Verify "Not found!" image is displayedawait expect(page.getByRole('img', { name: 'Not found!' })).toBeVisible();// Verify Home button is present and functionalconst homeButton = page.getByRole('link', { name: 'Home' });await expect(homeButton).toBeVisible();// Click Home button to return to main pageawait homeButton.click();// Verify we're back on the popular movies pageawait expect(page).toHaveURL(/.*\?category=Popular&page=1/);await expect(page).toHaveTitle('Popular Movies');await expect(page.getByRole('heading', { name: 'Popular' })).toBeVisible();});test('should have functional UI components in header', async ({ page }) => {// Navigate to the movie appawait page.goto('https://debs-obrien.github.io/playwright-movies-app');// Verify header components are presentawait expect(page.getByRole('img', { name: 'movie ticket' })).toBeVisible();// Verify theme toggle components are presentawait expect(page.getByRole('button', { name: '☀' })).toBeVisible();await expect(page.getByRole('button', { name: '☾' })).toBeVisible();await expect(page.getByRole('checkbox', { name: 'Toggle Switch' })).toBeVisible();// Verify login button is presentawait expect(page.getByRole('button', { name: 'Log In' })).toBeVisible();// Verify search functionality is accessibleawait expect(page.getByRole('search')).toBeVisible();});
}); 

通过这种方式可以做到:探索—>生成—>执行—>审查这一完整循环。

http://www.dtcms.com/a/316313.html

相关文章:

  • 解锁n8n:开启自动化工作流的无限可能
  • 面试题:vue3使用proxy相较于vue2的优点在哪里
  • 03-基于深度学习的钢铁缺陷检测-yolo11-彩色版界面
  • postman接口测试实战
  • 鸿蒙组件装饰器深度解析:@Component vs @ComponentV2
  • 【实时Linux实战系列】基于实时Linux的智能交通系统设计
  • 数据结构---Makefile 文件(格式、文件变量、调用、伪目标)、gcc编译的四个步骤、双向链表(概念、作用、应用)
  • 若依vue前端处理日期数据的格式问题(只留下年月日,去掉时分秒)
  • 小易的yolo学习笔记2
  • AlexNet训练和测试FashionMNIST数据集
  • 系统启动项管理工具对美国服务器性能基线的验证标准
  • Rust进阶-part4-智能指针2
  • 真正的多模态上下文学习需要关注视觉上下文
  • 人工智能领域、图欧科技、IMYAI智能助手2025年1月更新月报
  • 【RabbitMQ】高级特性—消息确认详解
  • 轻量应用服务器Centos系统上安装jdk8和Jdk17教程(详细)
  • BFS 和 DFS 编程思想、框架、技巧及经典例题总结
  • 【Git学习】入门与基础
  • 达芬奇31-40
  • Java技术栈/面试题合集(8)-Redis篇
  • Python爬虫08_Requests聚焦批量爬取图片
  • 如何用分布式架构视角理解宇宙稳定性?从精细调参到微服务的类比思考
  • 微服务架构及常见微服务技术栈
  • PromQL(Prometheus 查询语言)语法详解
  • 2um 精度 + 130mm 深孔扫描:新启航激光频率梳方案重塑 3D 轮廓测量标准
  • 【拓扑序】P6286 [COCI 2016/2017 #1] Cezar|省选-
  • 前端遇到页面卡顿问题,如何排查和解决?
  • DM8日常运维命令总结(四)
  • 游戏画面总是卡顿怎么办 告别延迟畅玩游戏
  • Go语言 泛型