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 高级用法系列 一的更多相关文章

  1. EF5+MVC4系列(12) 在主视图中直接用RenderAction调用子Action,并返回视图(Return View)或者分部视图(Return PartialView); 从主Action传值到子Action使用TempData传值;TempData高级用法

    结论: ViewData 适用于 在一次请求中 传递数据  . 比如我们从 主Action 到 主视图, 然后在 主视图中  用 RenderAction 请求子Action的时候,就是算作 一次请求 ...

  2. Solr学习总结(六)SolrNet的高级用法(复杂查询,分页,高亮,Facet查询)

    上一篇,讲到了SolrNet的基本用法及CURD,这个算是SolrNet 的入门知识介绍吧,昨天写完之后,有朋友评论说,这些感觉都被写烂了.没错,这些基本的用法,在网上百度,资料肯定一大堆,有一些写的 ...

  3. Jenkins高级用法 - Jenkinsfile 介绍及实战经验

    系列目录 1.Jenkins 安装 2.Jenkins 集群 3.Jenkins 持续集成 - ASP.NET Core 持续集成(Docker&自由风格&Jenkinsfile) 4 ...

  4. Python进阶:切片的误区与高级用法

    2018-12-31 更新声明:切片系列文章本是分三篇写成,现已合并成一篇.合并后,修正了一些严重的错误(如自定义序列切片的部分),还对行文结构与章节衔接做了大量改动.原系列的单篇就不删除了,毕竟也是 ...

  5. Web Scraper 高级用法——抓取属性信息 | 简易数据分析 16

    这是简易数据分析系列的第 16 篇文章. 这期课程我们讲一个用的较少的 Web Scraper 功能--抓取属性信息. 网页在展示信息的时候,除了我们看到的内容,其实还有很多隐藏的信息.我们拿豆瓣电影 ...

  6. 多年经验,教你写出最惊艳的 Markdown 高级用法

    点赞再看,养成习惯,微信搜索[高级前端进阶]关注我. 本文 GitHub https://github.com/yygmind 已收录,有一线大厂面试完整考点和系列文章,欢迎 Star. 最近在学习的 ...

  7. 多年经验总结,写出最惊艳的 Markdown 高级用法

    点赞再看,养成习惯,微信搜索[高级前端进阶]关注我. 本文 GitHub https://github.com/yygmind 已收录,有一线大厂面试完整考点和系列文章,欢迎 Star. 最近在学习的 ...

  8. Visual Studio 宏的高级用法

    因为自 Visual Studio 2012 开始,微软已经取消了对宏的支持,所以本篇文章所述内容只适用于 Visual Studio 2010 或更早期版本的 VS. 在上一篇中,我已经介绍了如何编 ...

  9. SolrNet高级用法(分页、Facet查询、任意分组)

    前言 如果你在系统中用到了Solr的话,那么肯定会碰到从Solr中反推数据的需求,基于数据库数据生产索引后,那么Solr索引的数据相对准确,在电商需求中经常会碰到菜单.导航分类(比如电脑.PC的话会有 ...

随机推荐

  1. 如何基于 String 实现同步锁?

    如何基于String实现同步锁? 在某些时候,我们可能想基于字符串做一些事情,比如:针对同一用户的并发同步操作,使用锁字符串的方式实现比较合理. 因为只有在相同字符串的情况下,并发操作才是不被允许的. ...

  2. Java基础之(一)——从synchronized优化看Java锁概念

    一.悲观锁和乐观锁概念 悲观锁和乐观锁是一种广义的锁概念,Java中没有哪个Lock实现类就叫PessimisticLock或OptimisticLock,而是在数据并发情况下的两种不同处理策略. 针 ...

  3. .NET Core如何全局获取用户信息?

    前言 在增删改查中的增和改操作中,我们经常需要更新数据流的创建人和修改人,无论我们项目是基于DDD,抑或是简单仅有服务层,此时我们都需要获取用户信息,那么我们只能将用户标识从控制器层层传递到服务或仓储 ...

  4. POJ 1050 To the Max 枚举+dp

    大致题意: 求最大子矩阵和 分析: 一开始想复杂了,推出了一个状态方程:d[i][j]=max(d[i][j-1]+-,d[i-1][j]+-).写着写着发现上式省略的部分记录起来很麻烦. 后来发现n ...

  5. QQ邮箱获取授权码方法

    1.登录QQ邮箱,点击"设置" 2.点击"账户" 3.开启POP3/SMTP服务 4.点击"生成授权码" 5.完成验证后,即可生成授权码 P ...

  6. jenkins报错The goal you specified requires a project to execute but there is no POM inthis directory

    报错截图及详细:   15:30:29[ERROR]The goal you specified requires a project to execute but there is no POM i ...

  7. Pytest学习笔记12-配置文件pytest.ini

    前言 pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行. 常用的配置项 marks 作用:测试用例中添加了自定义标记( ...

  8. Python如何设计面向对象的类(下)

    本文将在上篇文章二维向量Vector2d类的基础上,定义表示多维向量的Vector类. 第1版:兼容Vector2d类 代码如下: from array import array import rep ...

  9. python pip install matplotlib安装模块

    python pip install matplotlib安装模块,可附带安装相关的模块 程序运行提示: from . import _imaging as coreImportError: DLL ...

  10. Vue全局弹窗:一次注册,全局可弹

    Vue全局弹窗 今天来搞一个全局弹窗,不用每个文件都引入,只在main.js里作为全局原型引入就好了 先新建弹窗组件 toast.vue <template></template&g ...