- Cypress教程
- Cypress - 主页
- Cypress - 简介
- Cypress - 架构和环境设置
- Cypress - 测试运行者
- Cypress - 构建第一个测试
- Cypress - 支持的浏览器
- Cypress - 基本命令
- Cypress - 变量
- Cypress - 别名
- Cypress - 定位器
- Cypress - 断言
- Cypress - 文本验证
- Cypress - 异步Behave
- Cypress - 使用 XHR
- Cypress - jQuery
- Cypress - 复选框
- Cypress - 标签
- Cypress - 下拉菜单
- Cypress - 警报
- Cypress - 子窗口
- Cypress - 隐藏元素
- Cypress - 框架
- Cypress - 网络表
- Cypress - 鼠标操作
- Cypress - cookie
- Cypress - 获取和发布
- Cypress - 文件上传
- Cypress - 数据驱动测试
- Cypress - 提示弹窗
- Cypress - 仪表板
- Cypress - 屏幕截图和视频
- Cypress - 调试
- Cypress - 自定义命令
- Cypress - 装置
- Cypress - 环境变量
- Cypress - 挂钩
- Cypress - JSON 文件的配置
- Cypress - 报告
- Cypress - 插件
- Cypress - GitHub
- Cypress有用资源
- Cypress - 快速指南
- Cypress - 有用的资源
- Cypress - 讨论
Cypress - 挂钩
Cypress Hooks 用于在每次测试之前/之后执行某些操作。一些常见的 Hooks 如下 -
before - 一旦执行了描述块中的任何测试,就会执行它。
after - 一旦执行了描述块中的所有测试后,就会执行它。
beforeEach - 它在单个执行之前执行,它在描述块内阻塞。
afterEach - 它在个体执行后执行,它在描述块内阻塞。
执行
Cypress Hooks 命令的实现解释如下 -
describe('Tutorialspoint', function() {
before(function() {
// executes once prior all tests in it block
cy.log("Before hook")
})
after(function() {
// executes once post all tests in it block
cy.log("After hook")
})
beforeEach(function() {
// executes prior each test within it block
cy.log("BeforeEach hook")
})
afterEach(function() {
// executes post each test within it block
cy.log("AfterEac hook")
})
it('First Test', function() {
cy.log("First Test")
})
it('Second Test', function() {
cy.log("Second Test")
})
})
执行结果
输出如下 -
输出日志显示第一个执行的步骤是 BEFORE ALL。
最后执行的步骤是 AFTER ALL。两人都只跑过一次。
在 BEFORE EACH 下执行的步骤运行了两次(在每个 TEST BODY 之前)。
此外,在 AFTER EACH 下执行的步骤运行了两次(在每个测试主体之后)。
这两个 it 块都按它们实现的顺序执行。
标签
除了钩子之外,Cypress 还有标签 - .only 和 .skip。
.only 标签用于执行其所标记的 it 块,而 .skip 标记用于排除其所标记的 it 块。
使用 .only 实现
Cypress 中 .only 标签的实现如下 -
describe('Tutorialspoint', function()
//it block with tag .only
it.only('First Test', function() {
cy.log("First Test")
})
//it block with tag .only
It.only('Second Test', function() {
cy.log("Second Test")
})
it('Third Test', function() {
cy.log("Third Test")
})
})
执行结果
输出如下 -
输出日志显示带有 .only 标记的 it 块(第一次和第二次测试)仅被执行。
使用 .skip 实现
Cypress 中 .skip 标签的实现如下 -
describe('Tutorialspoint', function()
it('First Test', function() {
cy.log("First Test")
})
it('Second Test', function() {
cy.log("Second Test")
})
//it block with tag .skip
it.skip('Third Test', function() {
cy.log("Third Test")
})
})
执行结果
输出如下 -
输出日志显示带有 .skip 标记的 it 块(第三次测试)已从执行中跳过。