Skip to content

Cypress 速查表

面向 Web 应用的端到端测试框架,调试体验出色。

01

快速上手

基础测试

Cypress 直接运行在浏览器中。

cypress
describe("Home", () => {
  it("loads", () => {
    cy.visit("/");
    cy.contains("Welcome");
  });
});

交互操作

可链式调用的命令,模拟用户交互。

cypress
cy.get("input").type("hello");
cy.get("button").click();
cy.get(".result").should("contain", "hello");

断言

使用 should() 编写断言。

cypress
cy.get("#count").should("have.text", "5");
cy.get("li").should("have.length", 3);
cy.url().should("include", "/dashboard");
02

核心特性

测试固件(Fixtures)

用 fixture 管理测试数据。

cypress
cy.fixture("user.json").then(user => {
  cy.get("input").type(user.name);
});

自定义命令

自定义命令,复用常用测试步骤。

cypress
Cypress.Commands.add("login", (email, pw) => {
  cy.get("#email").type(email);
  cy.get("#pw").type(pw);
  cy.get("button").click();
});

cy.login("[email protected]", "secret");

这篇内容对您有帮助吗?