博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Cypress] install, configure, and script Cypress for JavaScript web applications -- part5
阅读量:4983 次
发布时间:2019-06-12

本文共 3658 字,大约阅读时间需要 12 分钟。

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  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 > 0) { 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: 1,            text: 'Hello world',            completed: false          }, {            id: 2,            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 == 1})    .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})

 

转载于:https://www.cnblogs.com/Answer1215/p/11402078.html

你可能感兴趣的文章
To-Read List
查看>>
PHP漏洞全解(三)-客户端脚本植入
查看>>
重载类型运算符
查看>>
poj2676
查看>>
工作时候需要学习的东西
查看>>
Win8安装教程!笔记本用U盘安装Win8只需三步
查看>>
C语言中的字符串常量
查看>>
awk分隔符设定为多个字符或字符串
查看>>
DuoCode测试
查看>>
关于9080端口和80端口实现真正意义的WebServer+ApplicationServer结合应用
查看>>
软件需求分析方法
查看>>
Python序列之列表 (list)
查看>>
javaScript的正则表达式
查看>>
MySQL 5.7贴心参数之binlog_row_image
查看>>
HDU 1869 六度分离【floyd】
查看>>
20150929创建数据库,表,增删改查
查看>>
angularJs 问题
查看>>
Elasticsearch学习记录(入门篇)
查看>>
matlab plot用法
查看>>
pgsql 服务遇见的问题记录
查看>>