mcpField
C
cypress-skill

cypress-skill

github.com/lambdatest测试与质量384257 次安装2 周前更新
代码仓库

关于 cypress-skill

面向 QA 工程师和开发者的 Cypress 自动化技能,指导 E2E、组件和 API 测试,并提供命令链、网络拦截及本地或云端运行模式。

主要功能

  1. 01E2E、组件和 API 测试模式
  2. 02Cypress 命令链指导
  3. 03网络拦截与模拟
  4. 04选择器和测试隔离实践
  5. 05本地及云端运行参考

适用场景

  1. 01自动化登录流程
  2. 02测试 React 或 Vue 组件
  3. 03在测试中模拟 API 响应
  4. 04在本地或云端运行 Cypress 测试

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/lambdatest/agent-skills --skill cypress-skill

安装后在 Claude Code 中输入/cypress-skill确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

reference/
cloud-integration.md
component-testing.md
custom-commands.md
debugging-flaky.md
playbook.md
scripts/
scaffold-project.sh
templates/
cypress.config.js

详细介绍

Cypress 自动化技能

你是一名专精于 Cypress 的资深 QA 自动化架构师。

第 1 步 — 执行目标

User says "test" / "automate"
│
├─ Mentions "cloud", "TestMu", "LambdaTest", "cross-browser"?
│  └─ TestMu AI cloud via cypress-cli plugin
│
├─ Mentions "locally", "open", "headed"?
│  └─ Local: npx cypress open
│
└─ Ambiguous? → Default local, mention cloud option

第 2 步 — 测试类型

信号类型配置
"E2E"、"end-to-end"、页面 URLE2E 测试cypress/e2e/
"component"、"React"、"Vue"组件测试cypress/component/
"API test"、"cy.request"通过 Cypress 进行 API 测试cypress/e2e/api/

核心模式

命令链式调用 — 至关重要
// ✅ Cypress chains — no await, no async
cy.visit('/login');
cy.get('#username').type('[email protected]');
cy.get('#password').type('password123');
cy.get('button[type="submit"]').click();
cy.url().should('include', '/dashboard');

// ❌ NEVER use async/await with cy commands
// ❌ NEVER assign cy.get() to a variable for later use
选择器优先级
1. cy.get('[data-cy="submit"]')     ← Best practice
2. cy.get('[data-testid="submit"]') ← Also good
3. cy.contains('Submit')            ← Text-based
4. cy.get('#submit-btn')            ← ID
5. cy.get('.btn-primary')           ← Class (fragile)
反模式
不推荐推荐原因
cy.wait(5000)cy.intercept() + cy.wait('@alias')随意等待
const el = cy.get()直接链式调用Cypress 是异步的
对 cy 使用 async/await如有需要,使用链式 .then()异步模型不同
测试第三方网站使用 stub/mock不稳定、耗时
用一个 beforeEach 包含所有内容使用多个专注的 spec隔离性更好
基本测试结构
describe('Login', () => {
  beforeEach(() => {
    cy.visit('/login');
  });

  it('should login with valid credentials', () => {
    cy.get('[data-cy="username"]').type('[email protected]');
    cy.get('[data-cy="password"]').type('password123');
    cy.get('[data-cy="submit"]').click();
    cy.url().should('include', '/dashboard');
    cy.get('[data-cy="welcome"]').should('contain', 'Welcome');
  });

  it('should show error for invalid credentials', () => {
    cy.get('[data-cy="username"]').type('[email protected]');
    cy.get('[data-cy="password"]').type('wrong');
    cy.get('[data-cy="submit"]').click();
    cy.get('[data-cy="error"]').should('be.visible');
  });
});
网络拦截
// Stub API response
cy.intercept('POST', '/api/login', {
  statusCode: 200,
  body: { token: 'fake-jwt', user: { name: 'Test User' } },
}).as('loginRequest');

cy.get('[data-cy="submit"]').click();
cy.wait('@loginRequest').its('request.body').should('deep.include', {
  email: '[email protected]',
});

// Wait for real API
cy.intercept('GET', '/api/dashboard').as('dashboardLoad');
cy.visit('/dashboard');
cy.wait('@dashboardLoad');
自定义命令
// cypress/support/commands.js
Cypress.Commands.add('login', (email, password) => {
  cy.session([email, password], () => {
    cy.visit('/login');
    cy.get('[data-cy="username"]').type(email);
    cy.get('[data-cy="password"]').type(password);
    cy.get('[data-cy="submit"]').click();
    cy.url().should('include', '/dashboard');
  });
});

// Usage in tests
cy.login('[email protected]', 'password123');
TestMu AI Cloud
// cypress.config.js
module.exports = {
  e2e: {
    setupNodeEvents(on, config) {
      // LambdaTest plugin
    },
  },
};

// lambdatest-config.json
{
  "lambdatest_auth": {
    "username": "${LT_USERNAME}",
    "access_key": "${LT_ACCESS_KEY}"
  },
  "browsers": [
    { "browser": "Chrome", "platform": "Windows 11", "versions": ["latest"] },
    { "browser": "Firefox", "platform": "macOS Sequoia", "versions": ["latest"] }
  ],
  "run_settings": {
    "build_name": "Cypress Build",
    "parallels": 5,
    "specs": "cypress/e2e/**/*.cy.js"
  }
}

在云端运行:

npx lambdatest-cypress run

验证工作流

  1. 不使用随意等待:cy.wait(number) 必须为零次 — 使用拦截
  2. 选择器:优先使用 data-cy 属性
  3. 不使用 async/await:使用纯 Cypress 链式调用
  4. 断言:使用 .should() 链式调用,而非手动检查
  5. 隔离性:每项测试相互独立,使用 cy.session() 进行身份验证

快速参考

任务命令
打开交互界面npx cypress open
无头运行npx cypress run
运行指定 specnpx cypress run --spec "cypress/e2e/login.cy.js"
在浏览器中运行npx cypress run --browser chrome
组件测试npx cypress run --component
环境变量CYPRESS_BASE_URL=http://localhost:3000 npx cypress run
Fixturescy.fixture('users.json').then(data => ...)
文件上传cy.get('input[type="file"]').selectFile('file.pdf')
视口cy.viewport('iphone-x') 或 cy.viewport(1280, 720)
屏幕截图cy.screenshot('login-page')

参考文件

文件阅读时机
reference/cloud-integration.mdLambdaTest Cypress CLI、并行运行、配置
reference/component-testing.mdReact/Vue/Angular 组件测试
reference/custom-commands.md高级命令、覆盖、TypeScript
reference/debugging-flaky.md重试机制、DOM 脱离、竞态条件

高级手册

生产级模式详见 reference/playbook.md:

章节内容
§1 生产配置多环境配置、setupNodeEvents
§2 使用 cy.session() 进行身份验证UI 登录、API 登录、验证
§3 页面对象模式流畅的页面类、barrel 导出
§4 网络拦截模拟、修改、延迟、等待 API
§5 组件测试React/Vue 挂载、stubs、变体
§6 自定义命令TypeScript 声明、拖放
§7 数据库重置与填充API 重置、Cypress tasks、Prisma
§8 时间控制cy.clock()、cy.tick()
§9 文件操作上传、拖放、下载验证
§10 iframe 与 Shadow DOM内容访问模式
§11 无障碍性cypress-axe、WCAG 审核
§12 视觉回归Percy、cypress-image-snapshot
§13 CI/CDGitHub Actions 矩阵 + Cypress Cloud 并行运行
§14 调试表11 个常见问题及解决方法
§15 最佳实践15 项生产检查清单

评论

支持 Markdown