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. STL源码剖析——空间配置器Allocator#2 一/二级空间配置器

    上节学习了内存配置后的对象构造行为和内存释放前的对象析构行为,在这一节来学习内存的配置与释放. C++的内存配置基本操作是::operator new(),而释放基本操作是::operator del ...

  2. Python对象的引用、可变性和垃圾回收

    1.标识.相等性和别名 别名的例子 >>> charles = {'name': 'Charles L. Dodgson', 'born': 1832} >>> l ...

  3. Jmeter-后置处理器--json提取器

    Token提取: 将token放入全局变量: 将token值设为全局变量,${__setProperty(newtoken,${token},)}  添加请求头部管理器作为全局使用,将变量token使 ...

  4. C++打印乘法表

    #include <iostream> #include <Windows.h> #include <iomanip> using namespace std; i ...

  5. c语言实现串

    串  (string)是由零个或者多个字符组成的有限序列,又称字符串 一般表示为 S=“ a1 a2 a3 a4  . . . . . an”    其中S 是串名,双引号串起来的是串值,(有些书用单 ...

  6. php获取当前月的天数

    <?php //php获取当前月份的所有天数 $start_day = date('Ym01', time()); $end_day = date('Ymd', strtotime(" ...

  7. 全栈项目|小书架|服务器开发-Koa全局路由实现

    什么是路由 路由就是具体的访问路径,指向特定的功能模块.一个api接口是由ip(域名)+端口号+路径组成,例如 :https://www.npmjs.com/package/koa-router就是一 ...

  8. linux 基础指令

    df -h 查看磁盘空间 root@ubuntu:/etc# df -h Filesystem Size Used Avail Use% Mounted on udev 970M 0 970M 0% ...

  9. 1+X证书学习日志——盒模型

    ##   padding的作用:                 控制子元素和父元素之间的位置关系                              padding设置方法:          ...

  10. javascript--清除表单缓存

    表单缓存是指当用户在表单输入之后再次回到该表单或者刷新页面的时候,浏览器会直接显示用户之前的输入,即表单缓存下来了.多数情况下这正是我们想要的,但也有些情况我们希望表单能够刷新,特别是根据后台的数据刷 ...