Cypress基础-02
1. beforeEach() hook
每次运行测试之前自动运行:
beforeEach(() => {
cy.visit("http://localhost:3000")
})
2. 自定义命令行custom Cypress commands
允许用户在不同的Cypress spec file中reuse code.
Eg. “get” data-test attributes more easily:
- 添加自定义命令到
cypress/support/commands.tsfile:/// <reference types="cypress" /> Cypress.Commands.add("getByData", (selector) => {//TypeScript? return cy.get(`[data-test=${selector}]`) })2. 无法检测自定义命令ERRORS解决
创建
cypress/support/index.ts并添加以下代码:/// <reference types="cypress" /> declare namespace Cypress { interface Chainable { getByData(dataTestAttribute: string): Chainable<JQuery<HTMLElement>> } }3. 使用:自定义的“getByData”命令,将允许pass任意data-test的值。
before: cy.get("[data-test='hero-heading']") after: cy.getByData("hero-heading")

浙公网安备 33010602011771号