[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 ...
随机推荐
- ACM_Ruin of Titanic(简单贪心)
Ruin of Titanic Time Limit: 2000/1000ms (Java/Others) Problem Description: 看完Titanic后,小G做了一个梦.梦见当泰坦尼 ...
- [转]linux之awk命令
转自:http://blog.chinaunix.net/uid-23302288-id-3785105.html awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓 ...
- Kibana里No Marvel Data Found问题解决(图文详解)
问题详情 http://192.168.80.145:5601/app/marvel#/no-data?_g=(refreshInterval:(display:'10%20seconds',paus ...
- debug时红点消失
问题描述:debug时红色断点和黄色小箭头不见,而用行代码高亮的形式时. 解决办法:可以用设置 工具 => 选项 => 文本编辑器 => 指示器边距 勾上选项
- java实例1
1.满天星星 import java.awt.*; public class xinxin { public static void main(String[] args) { Frame w = n ...
- shouldComponentUpdate不能直接比较object
凡是参阅过react官方英文文档的童鞋大体上都能知道对于一个组件来说,其state的改变(调用this.setState()方法)以及从父组件接受的props发生变化时,会导致组件重渲染,正所谓&qu ...
- CAD插入非等比例的图块
主要用到函数说明: _DMxDrawX::InsertBlock 向控件数据库中插入一个图块,不用它插入匿名块.详细说明如下: 参数 说明 BSTR pszDwgFileName 图块定义的dwg 文 ...
- BZOJ 2894: 世界线 广义后缀自动机
Code: #include<bits/stdc++.h> #define maxn 300000 #define ll long long using namespace std; ve ...
- js 包管理工具
环境 Windows10 + node 12.x + Webstorm 2019.1.3 工具 npm cnpm yarn npm/cnpm Webstorm 中第一次安装包一定几率卡死,很烦 不使用 ...
- react 父组件给子组件传值
父组件 import React from 'react'import '../page1/header.css'import { Table } from 'antd'import Child fr ...