Cypress provides a straightforward API that allows you to define custom commands. In this lesson, we’ll take a series of Cypress commands and wrap them up in a single custom command so we can easily repeat these steps in multiple specs.

We have this partten in the code:

    it('should have four initial todos and waiting loaded', function () {
cy.server(); // open server
cy.route('GET', '/api/todos', 'fixture:todos') // tell the endpoint, give fixture as data
.as('loadingTodos'); // mark it as loading
cy.visit('/'); // visit the page
cy.wait('@loadingTodos'); // wait until the loading is finished cy.get('.todo-list > li')
.should('have.length', 4);
});

Those code which has comments actually can be reused in many places.

Cypress enalbes us to create custom commands to use, so that we can reuse part of code everytime.

Open cypress/support/commands.js, add following code:

Cypress.Commands.add('seedAndVisit', (seedData = 'fixture:todos') => { // using 'fixture:todos' if seedData is undefined
cy.server()
cy.route('GET', '/api/todos', seedData).as('load') cy.visit('/') cy.wait('@load')
});

To use the commands:

    it('should use Cypress commands to simplify the code: using default value', function () {
cy.seedAndVisit();
cy.get('.todo-list > li')
.should('have.length', 4);
}); it('should use Cypress commands to simplify the code: using defined value', function () {
cy.seedAndVisit([]);
cy.get('.todo-list > li')
.should('have.length', 0);
});

[Cypress] Create a Single Custom Cypress Command from Multiple Commands的更多相关文章

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

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

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

  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系列(41)- Cypress 的测试报告

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

  6. Linux / UNIX create soft link with ln command

    How to: Linux / UNIX create soft link with ln command by NIXCRAFT on SEPTEMBER 25, 2007 · 42 COMMENT ...

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

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

  8. Cypress系列(90)- Cypress.Cookies 命令详解以及如何跨测试用例共享 Cookies

    如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html Cypress.Cookies 共有三个 ...

  9. [Cypress] 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 mig ...

随机推荐

  1. Sql批量插入方法

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  2. BZOJ 2592 随机化(伪)

    思路: 放yousiki大爷题解 http://yousiki.net/index.php/archives/82/ 我写的是随机化 既然gzz证了最终答案的上界是O(N)的 那么我们可以n^2枚举所 ...

  3. 【Codeforces576E_CF576E】Painting Edges(可撤销并查集+线段树分治)

    题目 CF576E 分析: 从前天早上肝到明天早上qwq其实颓了一上午MC ,自己瞎yy然后1A,写篇博客庆祝一下. 首先做这题之前推荐一道很相似的题:[BZOJ4025]二分图(可撤销并查集+线段树 ...

  4. 慕课网中网页定位导航中js相关问题总结

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...

  5. JS高级——闭包练习

    从上篇文章我们知道与浏览器的交互操作如鼠标点击,都会被放入任务队列中,而放入到任务队列中是必须等到主线程的任务都执行完之后才能执行,故而我们有时利用for循环给dom注册事件时候,难以获取for循环中 ...

  6. SQL Server建库-建表-建约束

    ----------------------------------------SQL Server建库-建表-建约束创建School数据库------------------------------ ...

  7. block要用copy修饰,还是用strong

    栈区与堆区 block本身是像对象一样可以retain,和release.但是,block在创建的时候,它的内存是分配在栈(stack)上,而不是在堆(heap)上.他本身的作于域是属于创建时候的作用 ...

  8. 程序员不可不知的Linux性能工具

    前言 在实际开发中,有时候会收到一些服务的监控报警,比如CPU飙高,内存飙高等,这个时候,我们会登录到服务器上进行排查.本篇博客将涵盖这方面的知识:Linux性能工具. 一次线上问题排查模拟 背景:服 ...

  9. JAVA通用工具类

    1.异常验证框架 框架1:com.google.common.base.Preconditions 框架2:org.apache.commons.lang.Validate 框架3:org.apach ...

  10. react typescript 子组件调用父组件

    //父组件 import * as React from 'react'import { Input } from 'antd'const Search = Input.Searchimport &q ...