[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 中首先实现.后来 ...
随机推荐
- [C++] 习题 2.15 实现简单环形队列
目录 前置技能 环形队列 具体实现 设计一个环形队列,用front和rear分别作为队头和队尾指针,另外用一个tag表示队列是空 ( 0 ) 还是不空 ( 1 ),这样就可以用front==rear作 ...
- Java正则表达式获取中括号之间的内容
参考: 求一个正则表达式提取中括号里的内容 [问题点数:80分]CSDN论坛 > Java > Web 开发 正则表达式 - 菜鸟教程 不包含中括号 正则表达式如下: \\[(.*?)] ...
- 在uboot里面添加环境变量使用run来执行
在uboot里面添加环境变量使用run来执行 本文链接:https://blog.csdn.net/u010979030/article/details/41038259 Author:杨正 Dat ...
- 记一次纯sqlite数据库的小项目开发经历
sqlite有哪些坑 1.支持的数据量级:根据SQLite的官方提示:http://www.sqlite.org/limits.htmlSQLIte数据库最大支持128TiB(140 terabyte ...
- Redis—简介
1.Redis是什么? 是一个速度非常快的非关系型数据库,即NoSql数据库(non-relational database) 可以将存储在内存的Key-Value数据持久化到硬盘,可以使用复制特性来 ...
- vue多页面项目搭建(vue-cli 4.0)
1.创建vue项目 cmd命令执行 vue create app (app 自定义的项目名) 一般都会选择后者,自己配置一下自己需要的选项(空格为选中) 这是我个人需要的一些选项,路由Router.状 ...
- 低功耗蓝牙UUID三种格式转换
熟悉BLE技术同学应该对UUID不陌生,服务.特征值.描述都是有UUID格式定义. 蓝牙广播中对服务UUID格式定义都有三种16 bit UUID.32 bit UUID.128 bit UUID. ...
- 3.JUC之volatile
原文链接:http://blog.csdn.net/zteny/article/details/54888629 一.简介 volatile是Java语言的关键字,用来修饰可变变量(即该变量不能被fi ...
- Vue指令之`v-model`和`双向数据绑定
v-bind 只能实现数据的单向绑定,从 M 自动绑定到 V, 无法实现数据的双向绑定 <input type="text" v-bind:value="msg& ...
- Linux建立虚拟ip的方法
文章来源 运维公会:Linux建立虚拟ip的方法 1.虚拟ip的介绍 虚拟IP地址(VIP) 是一个不与特定计算机或一个计算机中的网络接口卡(NIC)相连的IP地址.数据包被发送到这个VIP地址, ...