Fast, easy and reliable testing for anything that runs in a browser.
50.1k stars | TypeScript | MIT | 2012 | https://cypress.io
端对端测试框架,覆盖 E2E、组件测试和 API 测试,内置调试界面和时间回溯功能。
核心定位
Cypress 是专为 Web 应用设计的测试框架,与 Selenium 等工具不同,它直接运行在浏览器内部(不是通过 WebDriver),因此拥有更强的调试能力和更稳定的测试执行。
核心特性
- E2E 测试:模拟用户行为,完整流程测试
- 组件测试:对 React/Vue/Angular 等框架的组件进行隔离测试
- 时间回溯调试:每步操作都有快照,可回溯查看
- 自动等待:不需要手动 sleep,自动等待元素/请求就绪
- 实时重载:测试代码修改后实时重跑
- 网络拦截:可拦截/模拟 HTTP 请求(
cy.intercept()) - Cypress Cloud:云端并行执行、录像、截图
安装与使用
npm install cypress --save-dev
# 或
yarn add cypress --dev// 基本用法
describe('Login', () => {
it('should login successfully', () => {
cy.visit('/login')
cy.get('[data-cy=email]').type('user@example.com')
cy.get('[data-cy=password]').type('password')
cy.get('[data-cy=submit]').click()
cy.url().should('include', '/dashboard')
})
})与 Playwright/Puppeteer 对比
| 维度 | Cypress | Playwright | Puppeteer |
|---|---|---|---|
| 定位 | E2E + 组件测试 | 跨浏览器自动化 | Chrome/Firefox 自动化 |
| 架构 | 运行在浏览器内 | 运行在浏览器外 | 运行在浏览器外 |
| 调试体验 | 最佳(时间回溯) | 良好 | 基础 |
| 多浏览器 | 支持但有限 | 完整支持 | Chrome/Firefox |
| AI Agent 集成 | 无 | 有(playwright-mcp) | 有(puppeteer-mcp) |