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-cy
  • data-test
  • data-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的更多相关文章

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

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

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

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

  5. Cypress系列(3)- Cypress 的初次体验

    如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 前言 这里的栗子项目时 Cypress ...

  6. Cypress系列(41)- Cypress 的测试报告

    如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 注意 51 testting 有一篇文章 ...

  7. document.write('<script type=\"text/javascript\"><\/script>')

    document.write('<script type=\"text/javascript\"><\/script>')

  8. <script language = "javascript">, <script type = "text/javascript">和<script language = "application/javascript">(转)

          application/javascript是服务器端处理js文件的mime类型,text/javascript是浏览器处理js的mime类型,后者兼容性更好(虽然application/ ...

  9. 2.1 <script>元素【JavaScript高级程序设计第三版】

    向 HTML 页面中插入 JavaScript 的主要方法,就是使用<script>元素.这个元素由 Netscape 创造并在 Netscape Navigator 2 中首先实现.后来 ...

随机推荐

  1. Python 实现微信小程序的用户登录

    小程序可以通过官方提供的登录能力来获取用户身份的标示,具体文档可以参考 这里,通过流程时序可以看到,对于需要和前端配合的服务端开发,主要实现的就是通过小程序提供的 code 换取用户的 openid ...

  2. QT release版QAudioDeviceInfo获取不到音频设备,而debug版可以获取到

    新添加了两个模块:QCharts和Multimedia 但自己没有重新打包更新里面的库文件什么的... 坑爹... 害我找了这么久... 解决办法: 方法一: 将Qt安装目录下的plugins文件夹中 ...

  3. python学习-58 configparse模块

    configparse模块 1.生成文件 import configparser # 配置解析模块 config = configparser.ConfigParser() # config = { ...

  4. JDK8源码解析 -- HashMap(一)

    最近一直在忙于项目开发的事情,没有时间去学习一些新知识,但用忙里偷闲的时间把jdk8的hashMap源码看完了,也做了详细的笔记,我会把一些重要知识点分享给大家.大家都知道,HashMap类型也是面试 ...

  5. 浅谈(IOC)依赖注入与控制反转(DI)

    前言:参考了百度文献和https://www.cnblogs.com/liuqifeng/p/11077592.html以及http://www.cnblogs.com/leoo2sk/archive ...

  6. interface Part2(定义接口)

    一. 在 C# 语言中,类之间的继承关系仅支持单重继承,而接口是为了实现多重继承关系设计的. 二. 一个类能同时实现多个接口,还能在实现接口的同时再继承其他类,并且接口之间也可以继承. 三. 无论是表 ...

  7. git clone github上的项目失败 RPC failed

    error: RPC failed; curl 18 transfer closed with outstanding read data remainingfatal: the remote end ...

  8. @app.route源码流程分析

    @app.route(), 是调用了flask.app.py文件里面的Flask类的route方法,route方法所做的事情和add_url_rule类似,是用来为一个URL注册一个视图函数,但是我们 ...

  9. 【转载】使用Response.WriteFile输出文件以及图片

    Response对象是Asp.Net应用程序中非常重要的一个内置对象,其作用为负责将服务器执行好的信息输出给客户端,可以使用Response.WriteFile方法来像客户端输出文件或者图片,输出图片 ...

  10. Scrapy 安装与使用

    Scrapy的安装: 当前环境win10,python_3.6.4,64bit.在命令提示符窗口运行pip install Scrapy,出现以下结果: building 'twisted.test. ...