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.

    it('should Delete an item', function () {
cy.server();
cy.route({
method: 'DELETE',
url: '/api/todos/*',
response: {}
}).as('delete'); cy.seedAndVisit(); cy.get('.todo-list li')
.first()
.find('.destroy')
.invoke('show') // Make the hidden button appear
.click(); cy.wait('@delete'); cy.get('.todo-list li')
.should('have.length', 3);
});

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:

    it('Using alias for the DOM element', function () {
cy.server();
cy.route({
method: 'DELETE',
url: '/api/todos/*',
response: {}
}).as('delete'); cy.seedAndVisit(); cy.get('.todo-list li')
.as('list'); // alias the DOM element cy.get('@list')
.first()
.find('.destroy')
.invoke('show')
.click(); cy.wait('@delete'); cy.get('@list')
.should('have.length', 3);
});

[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. 取消安卓listview,scrollview,gridview滑动时候边缘模糊问题

    只需在xml文件里面声明: android:faddingEdge = "none" android:faddingEdgelenth = "0dp" andr ...

  2. day03_12/13/2016_bean属性的设置之构造器方式注入

  3. Docker学习系列(一):windows下安装docker(转载)

    本文目录如下: windows按照docker的基本要求 具体安装步骤 开始使用 安装远程连接工具连接docker 安装中遇到的问题 Docker的更新 Docker中的jupyter windows ...

  4. Oracle 10g RAC的负载均衡配置[转载]

    Oracle 10g RAC的负载均衡配置 负载均衡是指连接的负载均衡.RAC的负载均衡主要是指新会话连接到RAC数据库时,如何判定这个新的连接要连到哪个节点进行工作.在RAC中,负载均衡分为两种,一 ...

  5. RabbitMQ~一些术语和最消息的生产

    学习一种技术需要先了解它,而想要学好一种技术,需要更多的了解它的组成,原理和实现机制! RabbitMQ安装介绍 RabbitMQ是由erlang语言开发的,所以必须先有安装erlang,类似java ...

  6. MVC系列学习(十一)-客户端的验证

    1.通过一个实例,来了解MVC中强大的验证功能 1.1新建一个 [基本] 的mvc项目,因为要用到验证的js,然后在一个视图中写上一下代码,以及Model中的代码如下 [注]在调用html.EditF ...

  7. ios的认识

    刚进了ios兴趣班,第一次使用苹果电脑,因为苹果电脑和windows电脑使用的区别很大.所以老师教我们苹果电脑的基本使用,以及关于苹果产品的一些认识.我听得热血沸腾,对苹果开发越来越感兴趣,相信下次上 ...

  8. [ USACO 2007 OPEN ] Dining

    \(\\\) \(Description\) 有\(N\)头牛,\(F\)种食物,\(D\)种饮料,每种食物和饮料只有一份. 现在已知每头牛可以吃哪些食物,可以喝哪些饮料,问最多可以让多少头牛可以同时 ...

  9. [ NOIP 2002 ] TG

    \(\\\) \(\#A\) 均分纸牌 有\(N\)堆纸牌,每堆有若干张,但纸牌总数必为\(N\)的倍数.可以在任一堆上取若干张纸牌,然后移动给其左右任意一侧的纸牌堆,求将所有的牌堆牌数都变为平均值最 ...

  10. Python初学1

    windows版python下载: https://pan.baidu.com/s/1dsAPp0C9PJUF73kFDdAzXQ 安装时勾选pip和Add python.exe to Path. w ...