[Cypress] Create a Single Custom Cypress Command from Multiple Commands
from create Command multiple Custom single Cypress commands
2023-09-14 08:59:17 时间
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); });
相关文章
- leetcode 之Remove Nth Node From End of List(19)
- [Typescript] Create Type From Any Given Data in TypeScript
- [Spring boot] Read values from external properties file
- [Ramda] Create a Query String from an Object using Ramda's toPairs function
- [Angular 2] Using a Value from the Store in a Reducer
- mysql DATE_FORMAT FROM_UNIXTIME 的区别
- error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status
- Get ip address from hostname in C using Linux sockets
- (LeetCode 82)Remove Duplicates from Sorted List II
- [AWS] Create MySQL database in RDS and connect from local / EC2
- [Typescript] Using 'Pick' to create a sub-type from original type
- [Docker] Converting from Docker Compose to Kubernetes
- [Tools] Batch Create Markdown Files from a Template with Node.js and Mustache
- [Ramda] Create an Array From a Seed Value with Ramda's unfold
- [Redux] Implementing combineReducers() from Scratch
- [LeetCode] Find Median from Data Stream
- sql中去除重复的数据 select distinct * from table
- 已解决ImportError: cannot import name COMMON_SAFE_ASCII_CHARACTERS‘ from charset-normalizerconstant‘
- 已解决(pandas读取DataFrame列报错)raise KeyError(key) from err KeyError: (‘name‘, ‘age‘)
- 【Linux 内核 内存管理】物理分配页 ⑤ ( get_page_from_freelist 快速路径调用函数源码分析 | 遍历备用区域列表 | 启用 cpuset 检查判定 | 判定脏页数量 )
- cannot import name 'UMAP' from 'umap'
- Ubuntu系统中如何更新conda到最新版本 conda-env: error: invalid choice: ‘rename‘ (choose from ‘attach‘, ‘create‘,