Cypress 高级用法系列 一
1. Multiple Assertions
cy
.get('[data-cy=task]')
.then( item => {
expect(item[0]).to.contain.text('bread')
expect(item[1]).to.contain.text('milk')
})
- 用 then()就不会retry, 即使页面发生变化, expect assertion也不会retry.
cy
.get('[data-cy=task]')
.should( item => {
if (item.length !== 3) {
throw new Error('Not enough elements!')
}
expect(item[0]).to.contain.text('bread')
expect(item[1]).to.contain.text('milk')
})
- 用should(), 如果页面上元素发生变化, assert前面的一个命令就会retry。
- 可以加一些logic决定要不要做assertion。
2. Invisible element - changing the DOM
当Dom里面有一些invisible的element, 我们需要用.click(force:true) 来click到。更好的办法是让这个element显示出来。
cy
.get('[data-cy=star]') // invisible element, only display when mouse over the 'board-item'.
.invoke('show')
.should('be.visible')
.click()
同时我们也可以通过trigger()的办法来测试元素什么时候display。当鼠标靠近时display, 鼠标离开时disappear.
cy
.get('[data-cy="board-item"]')
.trigger('mouseover') cy
.get('[data-cy=star]')
.should('be.visible') cy
.get('[data-cy="board-item"]')
.trigger('mouseout') cy
.get('[data-cy=star]')
.should('not.be.visible')
- Cypress checks actionability of elements
- .invoke() function can change attributes of DOM elements.
- Trigger different type of events. Event listeners can be triggererd by .trigger() command.
3. Cookies
保存token在cookies里面,这样执行测试时候不用每个case都需要login一次。
方法一: 保存cookies 里面的token在support file>index.js里面,所有的tests都会share这个token。
Cypress.Cookies.defaults({
preserve: 'trello_token'
})
方法二: 只是单个文件share这个token, 则可以放在 berore each里面:
beforeEach(() => { Cypress.Cookies.preserveOnce('trello_token') cy
.visit('/') }) it('test #1', () => { cy
.setCookie('trello_token', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImZpbGlwQGV4YW1wbGUuY29tIiwiaWF0IjoxNjE1OTg5MTkyLCJleHAiOjE2MTU5OTI3OTIsInN1YiI6IjIifQ.c3HqS_RRCJp4IPYvwbUkxWPwBx4VXJa_0ArzKq7qx_M') cy
.reload() }); it('test #2', () => { });
- Cypress deletes cookies and other storage in between tests
- Cypress.Cookies API enables you to change cookies rules.
4. Intercepting Network Requests
- use .intercept() to match any http request our app makes.
- intercepted http requests can be waited for.
- Intercepted http requests can be tested.
it('Intercept requests', () => { cy
.intercept({
method: 'POST',
url: '/api/boards'
}).as('createBoard') cy
.visit('/') cy
.get('[data-cy=create-board]')
.click() cy
.get('[data-cy=new-board-input]')
.type('launching a rocket{enter}') cy
.wait('@createBoard')
.then( (board) => {
expect(board.response.statusCode).to.eq(201)
expect(board.request.body.name).to.eq('launching a rocket')
}) });
5. Stubbing Responses
stubbing response, 举例 如果get response stubbing empty.
cy
.intercept({
method: 'GET',
url: '/api/boards'
}, {
body: []
}).as('boardList')
can use fixture to load a json file as request reponse body:
cy
.intercept({
method: 'GET',
url: '/api/boards'
}, {
fixture: 'threeBoards'
}).as('boardList')
stubbing network error:
cy
.intercept({
method: 'POST',
url: '/api/boards'
}, {
forceNetworkError: true
}).as('boardList') cy.visit('/') cy.get('[data-cy-create-board]')
.click() cy.get('[data-cy=new-board-input]')
.type('new board{enter}') cy.get('#errorMessge')
.should('be.visible')
- Second argument modifies intercepted request
- We can dynamically change just a part of response data.
it('Stubbing response', () => { cy
.intercept({
method: 'GET',
url: '/api/boards'
}, (req) => { req.reply( (res) => {
res.body[0].starred = true return res
})
}).as('boardList') cy
.visit('/') });
6. Creating a custom command
- Create .d.ts definmitions file to get autocomplete of custom command.
- JSDoc adds documentation to custom comands.
create a custom comman in the test file or under the support folder > commands.js.(notes: can add the command in a index to load type definitions that come tish cypress module.E.g. command.d.ts or index.d.ts check in cypress documents. )
Cypress.Commands.add('addBoard', (input) => {
cy
.get('[data-cy="create-board"]')
.click(); cy
.get('[data-cy=new-board-input]')
.type(input + '{enter}');
}) it('Custom commands', () => { cy
.visit('/'); cy
.addBoard('groceries'); });
More powerful custom command using prevSubject: true or prevSubject: 'optional' command to call a child command only.
- custom commands can be parent, child or dual.
- https://testautomationu.applitools.com/advanced-cypress-tutorial/chapter9.html
Cypress.Commands.add('take', {prevSubject: 'optional'}, (subject, input) => { if (subject) { cy
.wrap(subject)
.find(`[data-cy=${input}]`) } else { cy
.get(`[data-cy=${input}]`) } }) it('Custom commands', () => { cy
.visit('/board/77787127477'); cy
.take('list')
.eq(0)
.take('task') });
7. Install Plugins
8. Running a Task
We can run a task in the background , wait for it to finish, and then continue on with our test.
Cypress 高级用法系列 一的更多相关文章
- EF5+MVC4系列(12) 在主视图中直接用RenderAction调用子Action,并返回视图(Return View)或者分部视图(Return PartialView); 从主Action传值到子Action使用TempData传值;TempData高级用法
结论: ViewData 适用于 在一次请求中 传递数据 . 比如我们从 主Action 到 主视图, 然后在 主视图中 用 RenderAction 请求子Action的时候,就是算作 一次请求 ...
- Solr学习总结(六)SolrNet的高级用法(复杂查询,分页,高亮,Facet查询)
上一篇,讲到了SolrNet的基本用法及CURD,这个算是SolrNet 的入门知识介绍吧,昨天写完之后,有朋友评论说,这些感觉都被写烂了.没错,这些基本的用法,在网上百度,资料肯定一大堆,有一些写的 ...
- Jenkins高级用法 - Jenkinsfile 介绍及实战经验
系列目录 1.Jenkins 安装 2.Jenkins 集群 3.Jenkins 持续集成 - ASP.NET Core 持续集成(Docker&自由风格&Jenkinsfile) 4 ...
- Python进阶:切片的误区与高级用法
2018-12-31 更新声明:切片系列文章本是分三篇写成,现已合并成一篇.合并后,修正了一些严重的错误(如自定义序列切片的部分),还对行文结构与章节衔接做了大量改动.原系列的单篇就不删除了,毕竟也是 ...
- Web Scraper 高级用法——抓取属性信息 | 简易数据分析 16
这是简易数据分析系列的第 16 篇文章. 这期课程我们讲一个用的较少的 Web Scraper 功能--抓取属性信息. 网页在展示信息的时候,除了我们看到的内容,其实还有很多隐藏的信息.我们拿豆瓣电影 ...
- 多年经验,教你写出最惊艳的 Markdown 高级用法
点赞再看,养成习惯,微信搜索[高级前端进阶]关注我. 本文 GitHub https://github.com/yygmind 已收录,有一线大厂面试完整考点和系列文章,欢迎 Star. 最近在学习的 ...
- 多年经验总结,写出最惊艳的 Markdown 高级用法
点赞再看,养成习惯,微信搜索[高级前端进阶]关注我. 本文 GitHub https://github.com/yygmind 已收录,有一线大厂面试完整考点和系列文章,欢迎 Star. 最近在学习的 ...
- Visual Studio 宏的高级用法
因为自 Visual Studio 2012 开始,微软已经取消了对宏的支持,所以本篇文章所述内容只适用于 Visual Studio 2010 或更早期版本的 VS. 在上一篇中,我已经介绍了如何编 ...
- SolrNet高级用法(分页、Facet查询、任意分组)
前言 如果你在系统中用到了Solr的话,那么肯定会碰到从Solr中反推数据的需求,基于数据库数据生产索引后,那么Solr索引的数据相对准确,在电商需求中经常会碰到菜单.导航分类(比如电脑.PC的话会有 ...
随机推荐
- Python 机器学习实战 —— 监督学习(上)
前言 近年来AI人工智能成为社会发展趋势,在IT行业引起一波热潮,有关机器学习.深度学习.神经网络等文章多不胜数.从智能家居.自动驾驶.无人机.智能机器人到人造卫星.安防军备,无论是国家级军事设备还是 ...
- Manacher(马拉车)————O(n)回文子串
Manacher 一.背景 1975年,Manacher发明了Manacher算法(中文名:马拉车算法),是一个可以在O(n)的复杂度中返回字符串s中最长回文子串长度的算法,十分巧妙. 让我们举个栗子 ...
- Windows内核开发-Windows内部概述-2-
Windows内部概述-2- 线程: 执行代码的实体是线程.一个线程的包含在进程里面的,线程使用进程提供的资源来运行代码. 一个线程拥有以下的内容: 1:明确的运行模式,用户态或者内核态. 2:执行的 ...
- top命令信息详解
top详解 [root@localhost ~]# top top - 09:36:38 up 17:59, 3 users, load average: 0.00, 0.03, 0.00 Tasks ...
- 为你的Go应用创建轻量级Docker镜像?
缩小Go二进制文件大小 环境 youmen@youmendeMacBook-Pro % gcc -dumpversion 12.0.5 youmen@youmendeMacBook-Pro % go ...
- C# 位图BitArray 小试牛刀
前面聊了布隆过滤器,回归认识一下位图BitMap,阅读前文的同学应该发现了布隆过滤器本身就是基于位图,是位图的一种改进. 位图 先看一个问题, 假如有1千万个整数,整数范围在1到1亿之间,如何快速确定 ...
- 重新点亮linux 命令树————帮助命令[一]
前言 重新整理一下linux的命令. 正文 这里首先介绍帮助命令. 帮助命令常用的有三个: man help info 那么就来看下这三个. man 第一个man,man不是男人的意思,而是manua ...
- Excel的布尔值运算
TRUE被当做1进行加减乘除,例:1-TRUE=0. FALSE被当做0进行加减乘除,例:1-FALSE=1.(当做除数时,会因为分母为0返回错误值)
- Swagger/OpenAPI By Swashbuckle在NetCore 3.1中较NetCore 2.2使用的注意事项及入门
方案选择 使用Web API时,了解其各种方法对开发人员来说可能是一项挑战. Swagger也称为OpenAPI(Open Application Programming Interface,开放应用 ...
- 14 Nginx访问日志自动按天切割
#!/bin/bash export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin LOG_DIR=/usr/loc ...