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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. Cypress系列(3)- Cypress 的初次体验

    如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 前言 这里的栗子项目时 Cypress ...

  6. Cypress系列(41)- Cypress 的测试报告

    如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 注意 51 testting 有一篇文章 ...

  7. document.write('<script type=\"text/javascript\"><\/script>')

    document.write('<script type=\"text/javascript\"><\/script>')

  8. <script language = "javascript">, <script type = "text/javascript">和<script language = "application/javascript">(转)

          application/javascript是服务器端处理js文件的mime类型,text/javascript是浏览器处理js的mime类型,后者兼容性更好(虽然application/ ...

  9. 2.1 <script>元素【JavaScript高级程序设计第三版】

    向 HTML 页面中插入 JavaScript 的主要方法,就是使用<script>元素.这个元素由 Netscape 创造并在 Netscape Navigator 2 中首先实现.后来 ...

随机推荐

  1. [转帖]hadoop学习笔记:hadoop文件系统浅析

    hadoop学习笔记:hadoop文件系统浅析 https://www.cnblogs.com/sharpxiajun/archive/2013/06/15/3137765.html 1.什么是分布式 ...

  2. vc编译器对 除法的优化

    基本知识,7/2 和 6/2 在计算机中的商都为3.C语言的除法不等同于数学意义中的除法. C语言的除法.采用向零取整的方法. -______________0_______________+ 只有在 ...

  3. 剑指offer59:按之字形顺序打印二叉树:[[1], [3,2], [4,5,6,7]]

    1 题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. 2 思路和方法 先给定一个二叉树的样式: ...

  4. WebClient 下载文件

    WebClient用法小结(转载)   如果只想从特定的URI请求文件,则使用WebClient,它是最简单的.NET类,它只用一两条命令执行基本操作,.NET FRAMEWORK目前支持以http: ...

  5. solr的命令

    Start the Server If you didn’t start Solr after installing it, you can start it by running bin/solr  ...

  6. iOS - 反射机制: objc_property_t的使用

    iOS属性反射:说白了,就是将两个对象的所有属性,用动态的方式取出来,并根据属性名,自动绑值.(注意:对象的类,如果是派生类,就得靠其他方式来实现了,因为得到不该基类的属性.) 常用的反射方式,有如下 ...

  7. shim和polyfill 区别解释

    polyfill 是 shim 的一种.shim 是将不同 api 封装成一种,比如 jQuery 的 $.ajax 封装了 XMLHttpRequest 和 IE 用 ActiveXObject 方 ...

  8. web框架(1)-搭建开发环境

    一.python安装 首先,确认系统安装的Python版本 $ python3 -V Python 3.6.3 未安装python,请转至:python安装 二.安装第三方依赖库 1.异步框架aioh ...

  9. py网络编程学习笔记

    一.异常处理 异常就是程序运行时发生错误的信号(在程序出现错误时,则会产生一个异常,若程序没有处理它,则会抛出该异常,程序的运行也随之终止),在python中,错误触发的异常如下 而错误分为两种: 1 ...

  10. Linux安装java环境和maven

    安装OpenJDK软件包: apt-get install openjdk-8-jdk 查看版本信息java -version 则代表安装成功 安装maven可以使用自己本机下载好的mavan使用Xf ...