[Cypress] install, configure, and script Cypress for JavaScript web applications -- part4
Load Data from Test Fixtures in Cypress
When creating integration tests with Cypress, we’ll often want to stub network requests that respond with large datasets. All of this mock data can lead to test code that is hard to read. In this lesson, we’ll see how to use fixtures to keep sample data in files and easily load it on demand in your tests.
If we load test data from the 'it', it is not a clean way, we should do it in fixtures:
// NOT
describe('App initialization', () => {
it('Displays todos from API on load', () => {
cy.server()
cy.route('GET', '/api/todos', [
{id: , name: 'One', iscomplete: false},
{id: , name: 'Two', iscomplete: false},
{id: , name: 'Three', iscomplete: false},
{id: , name: 'Four', iscomplete: false},
])
cy.visit('/')
cy.get('.todo-list li').should('have.length', )
})
})
In fixtures folder, to createa new json file called: todos.json:
[
{"id": , "name": "One", "iscomplete": false},
{"id": , "name": "Two", "iscomplete": false},
{"id": , "name": "Three", "iscomplete": false},
{"id": , "name": "Four", "iscomplete": false},
]
Use it:
describe('App initialization', () => {
it('Displays todos from API on load', () => {
cy.server()
cy.route('GET', '/api/todos', 'fixture:todos')
cy.visit('/')
cy.get('.todo-list li').should('have.length', )
})
})
Wait for XHR Responses in a Cypress Test
When testing interactions that require asynchronous calls, we’ll need to wait on responses to make sure we’re asserting about the application state at the right time. With Cypress, we don’t have to use arbitrary time periods to wait. In this lesson, we’ll see how to use an alias for a network request and wait for it to complete without having to wait longer than required or guess at the duration.
describe('App initialization', () => {
it('Displays todos from API on load', () => {
cy.server()
cy.route('GET', '/api/todos', 'fixture:todos').as('load')
cy.visit('/')
cy.wait('@load')
cy.get('.todo-list li').should('have.length', )
})
})
Interact with Hidden Elements in a Cypress Test
We often only show UI elements as a result of some user interaction. Cypress detects visibility and by default won’t allow your test to interact with an element that isn’t visible. In this lesson, we’ll work with a button that is shown on hover and see how you can either bypass the visibility restriction or use Cypress to update the state of your application, making items visible prior to interacting with them
cy.get('.todo-list li')
.first()
.find('.destroy')
.invoke('show')
.click()
})
Create Aliases for DOM Elements in Cypress Tests
We’ll often need to access the same DOM elements multiple times in one test. Your first instinct might be to use cy.get and assign the result to a variable for reuse. This might appear to work fine at first, but can lead to trouble. Everything in Cypress is done asynchronously and you’re interacting with an application’s DOM, which is changing as your tests interact with it. In this lesson, we’ll see how we can reference DOM elements in multiple places with the alias feature in Cypress.
describe('List Item Behavior', () => {
it('Deletes an item', () => {
cy.server()
cy...
cy.seedAndVisit()
cy.get('.todo-list li')
.first()
.find('.destroy')
.invoke('show')
.click()
cy.wait('@delete')
cy.get('.todo-list li')
.should('have.length', )
})
})
We be DRY, we can create alias for DOM element:
cy.get('.todo-list li')
.as('list')
cy.get('@list')
.first()
.find('.destroy')
.invoke('show')
.click()
cy.wait('@delete')
cy.get('@list')
.should('have.length', )
Test Variations of a Feature in Cypress with a data-driven Test
Many applications have features that can be used with slight variations. Instead of maintaining multiple tests with nearly identical code, we can take advantage of the JavaScript runtime and use normal data structures and plain old JavaScript to test and make assertions for multiple interactions in a single test.
describe('Footer', () => {
it('Filters todos', () => {
const filters = [
{link: 'Active', expectedLength: },
{link: 'Completed', expectedLength: },
{link: 'All', expectedLength: }
]
cy.seedAndVisit('fixture:mixed_todos')
cy.wrap(filters)
.each(filters => {
cy.contains(filter.link).click()
cy.get('.todo-list li').should('have.length', filter.expectedLength)
})
})
})
[Cypress] install, configure, and script Cypress for JavaScript web applications -- part4的更多相关文章
- [Cypress] install, configure, and script Cypress for JavaScript web applications -- part1
Despite the fact that Cypress is an application that runs natively on your machine, you can install ...
- [Cypress] install, configure, and script Cypress for JavaScript web applications -- part3
Use custom Cypress command for reusable assertions We’re duplicating quite a few commands between th ...
- [Cypress] install, configure, and script Cypress for JavaScript web applications -- part2
Use Cypress to test user registration Let’s write a test to fill out our registration form. Because ...
- [Cypress] install, configure, and script Cypress for JavaScript web applications -- part5
Use the Most Robust Selector for Cypress Tests Which selectors your choose for your tests matter, a ...
- Cypress系列(3)- Cypress 的初次体验
如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 前言 这里的栗子项目时 Cypress ...
- Cypress系列(41)- Cypress 的测试报告
如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 注意 51 testting 有一篇文章 ...
- document.write('<script type=\"text/javascript\"><\/script>')
document.write('<script type=\"text/javascript\"><\/script>')
- <script language = "javascript">, <script type = "text/javascript">和<script language = "application/javascript">(转)
application/javascript是服务器端处理js文件的mime类型,text/javascript是浏览器处理js的mime类型,后者兼容性更好(虽然application/ ...
- 2.1 <script>元素【JavaScript高级程序设计第三版】
向 HTML 页面中插入 JavaScript 的主要方法,就是使用<script>元素.这个元素由 Netscape 创造并在 Netscape Navigator 2 中首先实现.后来 ...
随机推荐
- [转帖]crontab每小时运行一次
crontab每小时运行一次 先给出crontab的语法格式 对于网上很多给出的每小时定时任务写法,可以说绝大多数都是错误的!比如对于下面的这种写法: 00 * * * * #每隔一小时执行一 ...
- Word 固定行间距公式图片显示不全、Word Eculid 字体导致行间距过大、Word 行间距过大
1. 前言 1.有些文章行间距要求是固定值,比如,固定值15磅,但是这样会导致有些公式.图片显示不全.例如下图: 2.Euclid这个字体很容易导致行间距超大. 2. 解决方案 1.把固定值15磅改为 ...
- 开始使用 Manjaro(添加源+字体渲染去模糊+软件安装+优化配置+常见错误)(30)
1. 添加 archlinux 镜像源 1. 步骤一 向 /etc/pacman.d/mirrorlist 中添加国内镜像地址 1.1 方法1:自动添加 1. 输入如下命令查看国内镜像源,并按质量排序 ...
- STM32F103芯片SPI控制NRF24L012.4G无线模块交互通信实验
1.NRF24L01模块的资料百度搜索一下就有很多.这里我就不做介绍本文主要侧重于应用层面实验介绍与分享. 2.先看下原理图. 根据原理图:写出NRF24L01 C语言驱动文件如下: #includ ...
- 关于python的一次性能调优过程
问题 这两天在公司帮老大写一个程序功能,要求抓取从elasticsearch和kibana服务器上返回的数据,统计所有hits的数据字段ret_code为0的hit,并计算其占有率等一些功能. 功能倒 ...
- 『Python基础』第8节:格式化输出
现在有一个需求, 询问用户的姓名, 年龄, 工作, 爱好, 然后打印成以下格式 ************ info of Conan ************ name: Conan age: 23 ...
- Hadoop 完全分布式搭建
搭建环境 https://www.cnblogs.com/YuanWeiBlogger/p/11456623.html 修改主机名------------------- 1./etc/hostname ...
- 异常:[vue/no-parsing-error] Parsing error:x-invalid-end-tag
- NetLink通信原理研究、Netlink底层源码分析、以及基于Netlink_Connector套接字监控系统进程行为技术研究
1. Netlink简介 0x1:基本概念 Netlink是一个灵活,高效的”内核-用户态“.”内核-内核“.”用户态-用户态“通信机制.通过将复杂的消息拷贝和消息通知机制封装在统一的socket a ...
- 怎样安装ipython
ipython 是一个python的交互式shell, 比默认的python shell更好用, 支持自动补全 / 上下翻等功能. 下面是按照方法: # 通用安装方法 pip install ipy ...