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. 分享两篇关于ActionBar样式设置的博客

    http://www.open-open.com/lib/view/open1373981182669.html http://blog.csdn.net/xyz_lmn/article/detail ...

  2. C# 利用反射进行类型转换

    /// <summary> /// 父类转子类 /// </summary> /// <typeparam name="TParent">< ...

  3. Unity相机平滑跟随

    简介 unity中经常会用到固定视角的相机跟随,然后百度发现大家都是自己写的,然后偶也写咯一个,分享一下 PS: 由于刚学C#不久,才发现delegate这个东东,也不知道对性能影响大不大,但是看MS ...

  4. Java_注解之一

    注解可以替换复杂的hbm.xml文件,使得程序的开发大大简化 @Override    :子类重写父类方法 @Test :junit测试 @Before :测试之前执行 @SuppressWarnin ...

  5. 研磨JavaScript系列(二):没有类

    object就是对象的类型.在JavaScript中不管多么复杂的数据和代码.都可以组织成object形式的对象. 但JavaScript没有"类"概念. 看下面这段JavaScr ...

  6. Html常用标签及全称

    <!-- 块标签 divsion --><div></div> <!--br 换行    break--> <br /> <!--分割 ...

  7. 使用Dreamweaver在一张图片上添加多个热点链接

    所有代码: <html> <head> <meta charset="utf-8"> <title>无标题文档</title& ...

  8. 使用jquery animate实现锚点慢慢平滑滚动效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Zabbix自带的mysql监控模块

    Zabbix自带的mysql监控模块 [root@Cagios zabbix-]# cp conf/zabbix_agentd/userparameter_mysql.conf /usr/local/ ...

  10. python 模块学习——time模块

    Python语言中与时间有关的模块主要是:time,datetime,calendar time模块中的大多数函数是调用了所在平台C library的同名函数, 所以要特别注意有些函数是平台相关的,可 ...