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. 【NX二次开发】Block UI 双精度表

    属性说明 常规         类型 描述     BlockID     String 控件ID     Enable     Logical 是否可操作     Group     Logical ...

  2. 【SQLite】知识点概述

    1.SQLite不与诸如MySQL,Oracle,PostgreSQL或SQL Server之类的客户端/服务器SQL数据库引擎竞争,SQLite与fopen()竞争,读写快35%.2.SQLite数 ...

  3. 带你了解Java的序列化与反序列化

    什么是序列化 序列化:将 Java 对象转换成字节流的过程. 什么是反序列化 反序列化:将字节流转换成 Java 对象的过程. 序列化的实现 当 Java 对象需要在网络上传输 或者 持久化存储到文件 ...

  4. 理解Spring:IOC的原理及手动实现

    Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架.也是几乎所有Java工作者必须要掌握的框架之一,其优秀的设计思想以及其代码实现上的艺术也是我们需要掌握的. ...

  5. 【题解】codeforces 467C George and Job dp

    题目描述 新款手机 iTone6 近期上市,George 很想买一只.不幸地,George 没有足够的钱,所以 George 打算当一名程序猿去打工.现在George遇到了一个问题. 给出一组有 n ...

  6. Django(69)最好用的过滤器插件Django-filter

    前言   如果需要满足前端各种筛选条件查询,我们使用drf自带的会比较麻烦,比如查询书名中包含"国"字,日期大于"2020-1-1"等等诸如此类的请求,Djan ...

  7. Jenkins+Github+Nginx实现前端项目自动部署

    前言 最近在搭建一个自己的网站,网站框架搭好了要把项目放到服务器运行,但是每次更新网站内容就要手动部署一次,实在很麻烦,于是就想搭建一套自动化部署的服务.看了一些案例最后选用现在比较主流的Jenkin ...

  8. js笔记18

    1.面向对象 (1)单例模式 (2)工厂模式 (3)构造函数 a.类  js天生自带的类 基类   object 子类   Function  Array Number  Math  Boolean ...

  9. 数据备份[APIO/CTSC 2007]题解

    题目描述 你在一家IT公司为大型写字楼或办公楼的计算机数据做备份. 然而数据备份的工作是枯燥乏味的,因此你想设计一个系统让不同的办公楼彼此之间互相备份,而你则坐在家中尽享计算机游戏的乐趣. 已知办公楼 ...

  10. C++ nullptr 和 NULL 的使用区别

    1. 为什么会有nullptr的出现 目的:nullptr的出现主要是为了替代NULL. 那么,为什么要替代NULL呢? 在NULL的定义中存在会有2种方式,有的编译器会将NULL定义成0,有的编译器 ...