Cypress 学习指南
Cypress 学习指南
# Cypress 简介
# 什么是 Cypress
一个前端测试工具
- Cypress 能测试什么
- E2E 测试
- 集成测试
- 单元测试(因为内嵌了 Mocha)
- 任何在浏览器中运行的内容
- Cypress 提供的一些功能
- 时间旅行
- 自动等待(类似 Jest 中的 wait)
- 以同步风格的代码完成异步操作
- 网络流量控制
- 截屏
- 持续集成
- Cypress 语法设计(及内置对象)
- jQuery + 链式调用
- Promise(Bluebird)
- Mocha + Chai
# 为什么要用 Cypress
- 当你需要 E2E 测试
- 在 2019 年 JavaScript 明星项目 (opens new window) 的测试分类中位于第二名
- 比其他同类 E2E 测试工具更简单、灵活、健壮
# 学习 Cypress
# 概览
- 耗时:从入门到熟悉基本概念,大约需要 8~16 小时
- 难点:充分利用需要花一定时间学习和配置(报告、覆盖率、CI 等)
- 工具:cypress (opens new window)
# 学习路线
- 前置学习
- 学习 Cypress
- 了解 Cypress 的组成,全面阅读文档
- 掌握基本使用
- 实战
- 在业务中编写测试
- 进一步熟悉各 API 的大量细节
- 为项目调整 Cypress 配置,使用高级功能
- 在业务中编写测试
# 资料
# 自学教材
# 实战
# Cypress 知识体系
# Cypress 概览
# 安装/初始化
# 在已有项目中安装 cypress
npm install cypress --save-dev
# 接着运行这个命令,cypress 将初始化并生成一堆用例
npx cypress open
1
2
3
4
5
2
3
4
5
# 文件结构 (opens new window)
- /cypress
- /fixtures (mock 数据)
- example.json
- /integration (测试文件)
- /examples
- example.spec.js (一般格式为
*.spec.js)
- example.spec.js (一般格式为
- /examples
- /plugins (用于配置安装的 插件 (opens new window),task 系统)
- index.js
- /support (用于调整 自定义选项 (opens new window))
- commands.js
- index.js
- /screenshots (默认截屏文件夹)
- /fixtures (mock 数据)
- cypress.json
# 测试文件 典型代码
/// <reference types="cypress" />
describe('描述', () => {
before(() => console.log('---- Test Start! ----'));
beforeEach(() => cy.visit('https://witch.url'));
afterEach(() => cy.clearCookies());
it('测试用户交互', () => {
cy.get('#app').children('.intro').click();
cy.contains('Welcome').should('be.exist');
});
it('测试显示文本', () => {
cy.get('div').should('have.text', 'Hello');
// * 另一种风格
cy.get('div').should(($div) => {
const text = $div.text();
expect(text).to.match(/hello/i);
});
});
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
大致分为几个部分
- TypeScript 自动完成支持(第一行的注释)
- 运行器和生命周期(
describe、it、before等) - 元素查找和操作(
cy相关命令) - 断言/测试(
should、expect、assert多种风格)
# Cypress 对象
Cypress 和 cy 的区别 (opens new window)
Cypress:全局对象,影响所有测试- 内置工具 (opens new window)
Cypress._(opens new window):Lodash (opens new window)Cypress.$(opens new window):jQuery (opens new window)Cypress.Blob(opens new window):blob-utli (opens new window)Cypress.minimatch()(opens new window):minimatch (opens new window)new Cypress.Promise(fn)(opens new window):Bluebird (opens new window)
- 全局 API (opens new window)
- 内置工具 (opens new window)
cy:在每个测试中相互独立
# 测试/断言
Cypress 中内置的断言 (opens new window) 包含了几种类型:
- Chai:断言
expect('test').to.be.a('string'):BDD 风格assert.equal(3, 3, 'vals equal'):TDD 风格
- Chai jQuery:关于 DOM 的断言
expect(\$el).to.have.attr('foo', 'bar')
- Sinon-Chai:关于函数调用情况的断言
expect(spy).to.be.called
.should():在 Cypress 中封装了以上所有可用断言cy.get('li.selected').should('have.length', 3):shouldcy.get('div').should(($div) => { expect($div)... }):BDD
注意到 Cypress 使用 Mocha BDD 风格的生命周期 (opens new window),
不同测试的命名风格:
| Mocha BDD | Mocha TDD | Jest |
|---|---|---|
describe/context | suite | describe |
specify/it | test | test/it |
before | setup | beforeAll |
after | teardown | afterAll |
beforeEach | suiteSetup | before |
afterEach | suiteTeardown | after |
# cy 命令
用来编写测试
- 测试
should:断言then:类似 Promise 的 theneach:遍历执行(对于数组)spread:then 的 each 版
- 查询
contains、getchildren、closest、findeq、filter、notfirst、lastnext、nextAll、nextUntilparent、parents、parentsUntilprev、prevAll、prevUntilsiblings
window、document、titleits:取得对象中的字段,如cy.get('ul li').its('length')root:当前上下文的根元素节点within:设定上下文元素(类似 JS 中的 with)
- 操作
- 用户操作
click、dblclick、rightclickblur、focus、focused:不支持hovertrigger:触发事件
- 表单/输入框
check、uncheck、selectclear:清除文本框type:输入文本框submit
scrollIntoView、scrollToinvoke:调用对象中的函数,如cy.get('div').invoke('show')
- 用户操作
- 浏览器
viewport:设置应用窗口大小clearCookie、clearCookies、getCookie、getCookies、setCookieclearLocalStorage
- 网络请求
visit、reload:访问hash、location、url:获取go:历史跳转,相当于window.history.gorequest:HTTP 请求server:启动一个服务route:跳转路由
- 功能性
- 任务
log、debug、pauseexec:执行 shell 命令readFile、writeFilescreenshot:截屏到/screenshotsfixture:读取/fixtures中文件内容task:执行/plugins中声明的事件
- 语法糖
as:设置为别名and:进行多个测试end:截断当前测试(后续链式调用将重新计算)wrap:包装一个对象(以便支持 cy 命令)
- 调用监听
spy:监听对象中的函数stub:替换对象中的函数(用于监听)
- Timer
clock:覆写原生时钟(将会影响 setTimeout 等原生函数)tick:跳过时间,加快测试速度(需要先cy.clock())wait:显式等待(不推荐使用)
- 任务
# Cypress API
包含定制选项方法,或公共静态方法
- 定制
Commands:添加自定义命令Cookies:测试时的 Cookie 行为控制Screenshot:截屏参数配置SelectorPlayground:调整选择器规则Server:调整cy.server()默认参数config:修改 Cypress 的 配置选项 (opens new window)env:管理自定义全局变量log:配置 log 参数
- 辅助
dom:一组 dom 相关方法- 如
Cypress.dom.isHidden($el)
- 如
isCy:是否是 cy 对象
- 环境信息
arch:获取 CPU 架构,来源于 Nodeos.arch()browser:获取浏览器信息platform:获取操作系统名字spec:当前测试文件信息version:版本号
# 事件
事件绑定机制是 Node Events (opens new window),
用法如:Cypress.on/cy.on
- 应用(页面)事件
uncaught:exceptionwindow:confirm、window:alert、window:before:load、window:load、window:before:unload、window:unloadurl:changed
- Cypress 事件
failviewport:changedscrolledcommand:enqueued、command:start、command:end、command:retrylog:added、log:changedtest:before:run、test:after:run
# Cypress 典型代码
上次更新: Jan 29, 2022 6:01 PM