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. mysql select 操作优先级

    单表查询操作 select filed1,filed2... form table where ... group by ... having .... order by ... limit ... ...

  2. 绑定树tree 的后台方法

    #region 获取部门列表树集合         /// <summary>         /// 获取部门列表树集合         /// </summary>     ...

  3. Spring Boot (24) 使用Spring Cache集成Redis

    Spring 3.1引入了基于注解(annotation)的缓存(cache)技术,它本质不是一个具体的缓存实现方案,而是一个对缓存使用的抽象,通过在既有代码中添加少量它定义的个助攻annotatio ...

  4. 研磨JavaScript系列(五):奇妙的对象

    在JavaScript中,只有object和function两种东西有对象化的能力.我们先来说说函数的对象化能力. 任何一个函数都可以为其动态地添加或去除属性,这些属性可以是简单类型,可以是对象,也可 ...

  5. 研磨JavaScript系列(四):代码的时空

    对于过程式编程来说,代码执行的时间与数据标识的空间是密不可分的.我们只有把指令执行的具体时刻与标识映射的具体地址结合起来,才能确定程序在执行瞬间的上下文状态.于是,代码时刻与数据标识的结构,就形成了作 ...

  6. BFS小结

    其实bfs本身不难,甚至不需要去学习,只要知道它的特性就可以写出来了.往往,bfs都是用递归做的.递归比循环更容易timeout.所以这次遇到一题bfs,卡时间的就悲剧了. PAT1076 #incl ...

  7. SQL server 2005中无法新建作用(Job)的问题

    1.在使用sqlserver2005创建作业时,创建不了,提示 无法将类型为“Microsoft.SqlServer.Management.Smo.SimpleObjectKey”的对象强制转换为类型 ...

  8. Linux Shell ssh登录脚本

    Linux 登陆服务器敲命令太多,某时候确实不便,所以就用shell写了一个  我的blog地址: http://www.cnblogs.com/caoguo 一.说明 支持秘密和密钥两种格式 用户名 ...

  9. (转)OGNL与值栈

    http://blog.csdn.net/yerenyuan_pku/article/details/67709693 OGNL的概述 什么是OGNL 据度娘所说: OGNL是Object-Graph ...

  10. tidyverse生态链

    一套完整的数据分析流程 , 如下图所示 从图中可以看到,整个流程包括读取数据,整洁数据,数据探索和交流部分.经过前两部分, 我们可以得到一个整理好的数据,它的每一行都是一个样本 , 每一列是一个变量. ...