[Cypress] Create a Single Custom Cypress Command from Multiple Commands
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- Cypress系列(41)- Cypress 的测试报告
如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 注意 51 testting 有一篇文章 ...
- 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 ...
- Cypress系列(3)- Cypress 的初次体验
如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 前言 这里的栗子项目时 Cypress ...
- Cypress系列(90)- Cypress.Cookies 命令详解以及如何跨测试用例共享 Cookies
如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html Cypress.Cookies 共有三个 ...
- [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 ...
随机推荐
- Java 删除List元素的正确方式
方式一:使用Iterator的remove()方法 public class Test { public static void main(String[] args) { List<Strin ...
- 巴什博弈----hdu2147-----较难
kiki's game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 40000/10000 K (Java/Others)Total ...
- 【洛谷3648/BZOJ3675】[APIO2014]序列分割(斜率优化DP)
题目: 洛谷3648 注:这道题洛谷3648有SPJ,要求输出方案.BZOJ3675数据组数较多但不要求输出方案. 分析: 这可能是我第三次重学斜率优化了--好菜啊 这道题首先一看就是个DP.稍微推一 ...
- linux如何更改yum源
更改linux yum源方法:第一步:进入yum配置文件目录:cd /etc/yum.repos.d/第二步:备份配置文件(如果后续出现了问题就可以恢复):mv CentOS-Base.repo Ce ...
- 学习c语言的感想
其实个人认为无论学习什么语言,最重要的是掌握习编程思想,然而C语言一种学习编程思想的基础语言.所以,C语言的重要性不言而喻. 一.课本 无论用的是什么书,要学好C语言,把书上的每一个例题.习题的代码读 ...
- wordpress登录账号之后才能查看页面,实例
函数: <?php auth_redirect(); ?> 例子: 要求用户登录才能查看页面 if(!is_user_logged_in()){ auth_redirect(); } 源文 ...
- AIDL跨进程通信报Intent must be explicit
在Android5.0机子上采用隐式启动来调试AIDL时,会出现Intent must be explicit的错误,原因是5.0的机子不允许使用隐式启动方式,解决的方法是:在启动intent时添加i ...
- cmd 切换目录和配置环境变量
记录一下: 在用cmd进行切换盘符的时候, 如果是从 C盘切换到其他盘的话: D:直接回车就行了. 如果是在同一个盘符内切换文件夹的话,cd D:\ComputerSoft\curl\curl-7.6 ...
- JS高级——Function原型链
基本概念 1.函数可以通过Function new出来,那么Function可以被称作构造函数,被new出来的函数可以被称为一个对象 2.Function既然是构造函数,那么肯定也有原型,它的原型是一 ...
- C语言保留字
数值变量相关: int float double char long short unsigned signed 储存说明符 const 用于声明常量 static用于限制变量/函数的作用范围等等 e ...