[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 lot. In this lesson, we'll see the recommended Cypress best practices for selectors, and why we should prefer the data-cy attribute.
The Selector Playground automatically follows these best practices.
When determining an unique selector it will automatically prefer elements with:
data-cydata-testdata-testid
Assert on Your Redux Store with Cypress
Cypress doesn't provide out-of-the-box ability to make assertions on our frontend stores, so let's expose the store to the tests, and assert on it. We'll use our knowledge of Cypress's asynchronous programming style to access properties and functions on the store using cy.its and cy.invoke.
Inside applicatioin:
if(window.Cypress) {
window.store = store
}
Inside test:
cy.window().then(($window) => {console.log($window.store)})
or
cy.window().its('store')
What we want is to be able to make assertions against the state of the store. In order to get the state of the store, we would normally call, getState which is a function, not a property like store. In order to do this, we can call, .invoke.
cy.window().its('store').invoke('getState').then(($state) => { console.log($state)})
Create Custom Cypress Commands with better logs
Do you need to reuse complex Cypress calls often (like when accessing the store)?
You can turn them into custom Cypress commands, and even customize their output in the time-traveling debugger, so it's easy to see a snapshot at the point your command ran!
commands:
Cypress.Commands.add("store", (stateName = '') => {
let log = Cypress.log({name: 'store'})
const cb = (state) => {
log.set({
message: JSON.stringify(state),
consoleProps: () => {
return state
}
})
return state
}
return cy.window({log: false}).then(($window) => { return $window.store.getState() }).then((state) => {
if (stateName.length > ) {
return cy.wrap(state, {log: false}).its(stateName).then(cb)
} else {
return cy.wrap(state, {log: false}).then(cb)
}
})
})
Test:
cy.store('todos').should('deep.equal', [{
id: ,
text: 'Hello world',
completed: false
}, {
id: ,
text: 'Goodnight moon',
completed: true
}])
// or
cy.store('example.test.first')
Wrap External Libraries with Cypress
External libraries tend to be synchronous, so how do we integrate other powerful tools into the Cypress framework? This lesson walks us through merging in the Lodash library to Cypress to allow us to slice and dice data to more accurately assert on just the pieces of data we care about.
commands.js
const _ = require('lodash')
let loMethods = _.functions(_).map((fn) => { return 'lo_${fn}'})
loMethods.forEach((loFn) => {
let loName = loFn.replace(/lo_/, '')
Cypress.Commands.add(loFn, {prevSubject: true}, (subject, fn, ...args) => {
let result = _[loName](subject, fn, ...args)
Cypress.log({
name: 'lo_filter',
message: JSON.stringify(result),
consoleProps: () => { return result }
})
return result
})
Use:
cy.store('todos')
.lo_find((todo) => { return todo.id == })
.lo_pick('text')
.should('deep.equal', [
{
text: '1st Todo',
},
...
])
Find Unstubbed Cypress Requests with Force 404
Requests that aren't stubbed will hit our real backend. To ensure we've stubbed all our routes, we can use the force404 method to send 404s from any unstubbed routes.
cy.server({force404: true})
[Cypress] install, configure, and script Cypress for JavaScript web applications -- part5的更多相关文章
- [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 -- 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 -- part4
Load Data from Test Fixtures in Cypress When creating integration tests with Cypress, we’ll often wa ...
- Cypress系列(3)- Cypress 的初次体验
如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 前言 这里的栗子项目时 Cypress ...
- Cypress系列(41)- Cypress 的测试报告
如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 注意 51 testting 有一篇文章 ...
- document.write('<script type=\"text/javascript\"><\/script>')
document.write('<script type=\"text/javascript\"><\/script>')
- <script language = "javascript">, <script type = "text/javascript">和<script language = "application/javascript">(转)
application/javascript是服务器端处理js文件的mime类型,text/javascript是浏览器处理js的mime类型,后者兼容性更好(虽然application/ ...
- 2.1 <script>元素【JavaScript高级程序设计第三版】
向 HTML 页面中插入 JavaScript 的主要方法,就是使用<script>元素.这个元素由 Netscape 创造并在 Netscape Navigator 2 中首先实现.后来 ...
随机推荐
- [转帖]时序数据库技术体系 – InfluxDB TSM存储引擎之数据写入
时序数据库技术体系 – InfluxDB TSM存储引擎之数据写入 http://hbasefly.com/2018/03/27/timeseries-database-6/ 2018年3月27日 ...
- [WCF] - Odata Service 访问失败,查看具体错误信息的方法
Issue 解决 为 Data Service 配置属性如下:[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = ...
- google test 打印派生类对象
在Unison中使用google test时,发现EXPECT_EQ在fail时,不能打印Unison Test Language中定义的派生类的对象.于是写了个纯C++的示例,发现在只定义基类的op ...
- 第四章 函数之lambda 表达式和内置函数
4.5 lambda 表达式 用于表示简单的函数. # 三元运算,为了解决简单的if else的情况,如:if 1 == 1: a = 123else: a = 456# 相当于a = 1 ...
- Arm-Linux 移植 ssh
背景: 自己拥有一块开发板,但是苦于上面没有ssh,比较不方便.正好趁这个机会,移植ssh.我们使用的ssh是openssh. host平台 :Ubuntu 18.04 arm平台 : S5P6818 ...
- 网页修改<title ></title >标签内容
document.title = 'xxxxxx';
- java-websocket客户端 断线重连 注入Service问题
java版客户端: 使用开源项目java-websocket, github地址: https://github.com/TooTallNate/Java-WebSocket github上有很多示例 ...
- 翻译-在10行代码之内创建容器化的.net core应用
本文翻译自Hans Kilian的文章 Creating a containerized .NET core application in less than 10 lines of code htt ...
- VBA Exit For语句
当想要根据特定标准退出For循环时,就可以使用Exit For语句.当执行Exit For时,控件会立即跳转到For循环之后的下一个语句. 语法 以下是在VBA中Exit For语句的语法. Exit ...
- K2 BPM_当K2遇上医药,用流程打通企业的任督二脉_业务流程管理系统
据调查,如今仍有60%的医药企业,存在合规经营和利润下降的困扰,在“研”.“产”.“供”.“销”的运营过程中,时时伴随着严苛的管理政策和法规.如何加强企业跨部门.跨组织.跨业务线的执行能力,始终是管理 ...