We’ll often need to access the same DOM elements multiple times in one test. Your first instinct might be to use cy.getand assign the result to a variable for reuse. This might appear to work fine at first, but can lead to trouble. Everything in Cypress is done asynchronously and you’re interacting with an application’s DOM, which is changing as your tests interact with it. In this lesson, we’ll see how we can reference DOM elements in multiple places with the alias feature in Cypress.

  1. it('should Delete an item', function () {
  2. cy.server();
  3. cy.route({
  4. method: 'DELETE',
  5. url: '/api/todos/*',
  6. response: {}
  7. }).as('delete');
  8.  
  9. cy.seedAndVisit();
  10.  
  11. cy.get('.todo-list li')
  12. .first()
  13. .find('.destroy')
  14. .invoke('show') // Make the hidden button appear
  15. .click();
  16.  
  17. cy.wait('@delete');
  18.  
  19. cy.get('.todo-list li')
  20. .should('have.length', 3);
  21. });

The code above, we have use 'cy.get('.todo-list li')' in two places.

We can use alias for DOM element as well to reduce duplication:

  1. it('Using alias for the DOM element', function () {
  2. cy.server();
  3. cy.route({
  4. method: 'DELETE',
  5. url: '/api/todos/*',
  6. response: {}
  7. }).as('delete');
  8.  
  9. cy.seedAndVisit();
  10.  
  11. cy.get('.todo-list li')
  12. .as('list'); // alias the DOM element
  13.  
  14. cy.get('@list')
  15. .first()
  16. .find('.destroy')
  17. .invoke('show')
  18. .click();
  19.  
  20. cy.wait('@delete');
  21.  
  22. cy.get('@list')
  23. .should('have.length', 3);
  24. });

[Cypress] Create Aliases for DOM Elements in Cypress Tests的更多相关文章

  1. [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 ...

  2. [D3] Create DOM Elements with D3 v4

    Change is good, but creating from scratch is even better. This lesson shows you how to create DOM el ...

  3. Adding DOM elements to document

    1.JavaScript 添加DOM Element 执行效率比较: 抄自:http://wildbit.com/blog/2006/11/21/javascript-optimization-add ...

  4. [D3] Select DOM Elements with D3 v4

    Before you can create dazzling data driven documents, you need to know how D3 accesses the DOM. This ...

  5. [D3] Modify DOM Elements with D3 v4

    Once you can get hold of DOM elements you’re ready to start changing them. Whether it’s changing col ...

  6. ReactDOM & DOM Elements

    一.ReactDOM 1.1 render() ReactDOM.render(element,container,[callback]) 在container中渲染一个React元素,然后返回组件一 ...

  7. [Cypress] Create True end-to-end Tests with Cypress (Smoke test)

    Integration tests let us keep our tests fast and reliable. They also allow us to test scenarios that ...

  8. [Javascript] Create scrollable DOM elements with Greensock

    In this lesson, we will look at Greensock's Draggable API. We will implement a scrollable <div> ...

  9. [Cypress] Test React’s Controlled Input with Cypress Selector Playground

    React based applications often use controlled inputs, meaning the input event leads to the applicati ...

随机推荐

  1. BZOJ 2592 随机化(伪)

    思路: 放yousiki大爷题解 http://yousiki.net/index.php/archives/82/ 我写的是随机化 既然gzz证了最终答案的上界是O(N)的 那么我们可以n^2枚举所 ...

  2. UNIX环境高级编程--3

    文件IO 函数lseek: 每个打开文件都有一个与其相关联的“当前文件偏移量”,用来度量从文件开始处计算的字节数.除非指定O_APPEND选项,否则该偏移量被置为0.如果文件描述符指向的是一个管道.F ...

  3. .net Jquery动态显示当前时间

    <span id="Timer"></span> <script type="text/javascript"> $(fun ...

  4. Jquery EasyUI环境下设置下拉框选中指定选项

    前提: 要求点击某个按钮就将所有的下拉框都设置为选中第一个选项,因此,指定索引是最好的做法 尝试过的做法: html代码如下(easyui 的写法) <select class="ea ...

  5. swift单例创建的几种方法

    //单例方法1 class SingleTonOne{ static var sharedInstanceOne:SingleTonOne{ struct SingleTonStruct { stat ...

  6. RabbitMQ 创建用户和创建Virtual host

    https://www.bilibili.com/video/av18997807/?p=3 Virtual host 就是类似数据库吧.

  7. Python 时间处理---------笔记

    时区处理&格式化 import pytz from datetime import datetime # 设置时区 timezone = pytz.timezone('Asia/Shangha ...

  8. java设计模式02观察者模式

    观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己. 这里主要讲一下学习内置观察者的记录,在JA ...

  9. 【技术累积】【点】【java】【27】@JSONField

    @JSONField 该注解隶属于阿里fastjson,方便fastjson处理对象时的一些操作 源码 @Retention(RetentionPolicy.RUNTIME) @Target({ El ...

  10. Web 服务器与应用服务器的区别是什么?

    不太严谨的说法:web服务器就是负责接收用户的Request,然后响应html等给客户浏览器.应用服务器处理一些业务逻辑等. 作者:luo链接:https://www.zhihu.com/questi ...