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. python通过字典实现购物车案例-用户端

    import os dict01 = { 'iphone' : { '5999' : { '总部位于美国' : '价格相对较贵', }, }, 'wahaha' : { '15' : { '总部位于中 ...

  2. 在Centos7下安装RabbitMQ

    1.背景 不用多说,这东西好用! 2.安装 步骤一:下载安装包 链接:https://pan.baidu.com/s/1PIYI60wX6L7BtVyVft-vSA 提取码:1234 复制这段内容后打 ...

  3. 详解 DNS 解析

    背景 前面讲了域名.IP,那么还缺少一个主角,就是 DNS 这些都是网络中最最最基础的,也是最最最重要的概念,很有必要深入学习下 所有素材均来自:https://www.bilibili.com/vi ...

  4. 在vue中下拉框切换事件中改新建表单中的一个值,页面不显示

    事件中改新建表单中的一个值,页面不显示,当另一个对象值发生改变时,这个页面上的值才会显示 由于新建表单是弹窗,在弹出时会重新给每个字段重新赋值,在赋值时没给这个字段赋值(常见新加功能时,加了一个字段, ...

  5. 仅使用JsonUtility && File类实现Json数据读写

    using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using S ...

  6. C语言:C99 中的37个关键字

    一.数据类型关键字(12个): 1.char [tʃɑ:]:声明字符型变量或函数 2.double [ˈdʌbəl] :声明双精度变量或函数 3.enum :声明枚举类型 4.float [fləut ...

  7. 详解C++中继承的基本内容

    有些类与类之间存在特殊的关系,有共性也有特性,比如动物类可以细分为猫,狗等.下级别的成员除了拥有上一级的共性,还有自己的特性,这个时候就可以考虑继承的技术,减少重复代码. 一.继承中的对象模型 1.1 ...

  8. Prometheus+Grafana企业监控系统

    Prometheus+Grafana企业监控系统 作者 刘畅 实验配置: 主机名称 Ip地址 controlnode 172.16.1.70/24 slavenode1 172.16.1.71/24 ...

  9. MySQL数据库快速入门与应用实战(阶段一)

    MySQL数据库快速入门与应用实战(阶段一) 作者 刘畅 时间 2020-09-02 实验环境说明: 系统:centos7.5 主机名 ip 配置 slavenode3 172.16.1.123 4核 ...

  10. 11、函数(def)

    11.1.函数: 函数即变量 函数的作用域只跟函数声明时定义的作用域有关,跟函数的调用位置无任何关系 1.函数格式: def test(x): ''' 2*x+1 :param x:整形数字 :ret ...