Despite the fact that Cypress is an application that runs natively on your machine, you can install it and add it as a dependency just like all other dependencies in your package.json file. So let's install Cypress and get our project ready to start end-to-end testing with Cypress.

install:

npm i -D cypress

Run:

npx cypress open

It will create some default tests and run the tests.

Config with eslint:

npm i -D eslint-plugin-cypress

Open .eslintrc.js:

const path = require('path')

module.exports = {
extends: [
'kentcdodds',
'kentcdodds/import',
'kentcdodds/webpack',
'kentcdodds/jest',
'kentcdodds/react',
],
plugins: ['eslint-plugin-cypress'],
env: {'cypress/globals': true
},
overrides: [
{
files: ['**/__tests__/**'],
settings: {
'import/resolver': {
jest: {
jestConfigFile: path.join(__dirname, './jest.config.js'),
}
}
}
}
]
}

Update .gitignore:

coverage
node_modules
dist
cypress/videos
cypress/screenshots

Write the first Cypress Test

Cypress starts out your project with a ton of tests, let’s remove those and add our first test that verifies our calculator can add two numbers together using the Cypress selector playground and Cypress commands.

describe('anonymous calculator', () => {
it('can make calculation', () => {
cy.visit('http://localhost:8080')
.get('._2S_Gj6clvtEi-dZqCLelKb > :nth-child(3)') // get 1
.click()
.get('._1yUJ9HTWYf2v-MMhAEVCAn > :nth-child(4)') // get +
.click()
.get('._2S_Gj6clvtEi-dZqCLelKb > :nth-child(5)') // get 3
.click()
.get('._1yUJ9HTWYf2v-MMhAEVCAn > :nth-child(5)') // get =
.click()
.get('.mNQM6vIr72uG0YPP56ow5')// get display
.should('have.text', '4');
})
})

Configure Cypress in cypress.json

Cypress is fairly simple to configure, but it’s important to optimize our experience in testing Cypress. We’ll configure the baseUrl to make it easier to visit our app and we’ll also move our test files to a folder called e2e to make it more clear what types of tests we want to write with Cypress. We’ll use the cypress.json file, but Cypress can also be configured via command line flags and environment variables.

// cypress.json

{
"baseUrl": "http://localhost:8080",
"integrationFolder": "cypress/e2e/",
"viewportHeight": 900,
"viewportWidth": 400,
"defaultCommandTimeout": 5000
}

We have config the 'baseUrl', so in the test, we can use:

describe('anonymous calculator', () => {
it('can make calculation', () => {
cy.visit('/')
.get('._2S_Gj6clvtEi-dZqCLelKb > :nth-child(3)')
.click()
.get('._1yUJ9HTWYf2v-MMhAEVCAn > :nth-child(4)')
.click()
.get('._2S_Gj6clvtEi-dZqCLelKb > :nth-child(5)')
.click()
.get('._1yUJ9HTWYf2v-MMhAEVCAn > :nth-child(5)')
.click()
.get('.mNQM6vIr72uG0YPP56ow5')
.should('have.text', '4');
})
})

Installing cypress-testing-library

Our selectors are pretty ridiculous, mostly because we’re using CSS-in-JS and css modules for our react application. But even if we weren’t, using class names as selectors for our application testing is pretty brittle and not really testing in the way a user would. So instead let’s use cypress-testing-library to improve our selectors and make it easier to read our tests.

The following part of code is really hard to read and maintain because those wired selector

.get('._2S_Gj6clvtEi-dZqCLelKb > :nth-child(3)')
.click()
.get('._1yUJ9HTWYf2v-MMhAEVCAn > :nth-child(4)')
.click()
.get('._2S_Gj6clvtEi-dZqCLelKb > :nth-child(5)')
.click()
.get('._1yUJ9HTWYf2v-MMhAEVCAn > :nth-child(5)')
.click()
.get('.mNQM6vIr72uG0YPP56ow5')

To solve the problem, let's install:

npm i -D cypress-testing-library

Inside cypress/support:

// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

import 'cypress-testing-library/add-commands'
// Import commands.js using ES2015 syntax:
import './commands' // Alternatively you can use CommonJS syntax:
// require('./commands')

Update the tests:

      cy.visit('/')
.getByText(/^1$/)
.click()
.getByText(/^\+$/)
.click()
.getByText(/^3$/)
.click()
.getByText(/^\=$/)
.click()

For the display, we need to use 'data-testid':

Update the component:

    return (
<div
className={styles.autoScalingText}
style={{transform: `scale(${scale},${scale})`}}
ref={this.node}
data-testid="total"
>
{this.props.children}
</div>

Now the test looks like:

describe('anonymous calculator', () => {
it('can make calculation', () => {
cy.visit('/')
.getByText(/^1$/)
.click()
.getByText(/^\+$/)
.click()
.getByText(/^3$/)
.click()
.getByText(/^\=$/)
.click()
.getByTestId('total')
.should('have.text', '4');
})
})

Scripting Cypress for local development and Continuous Integration

We want to make starting cypress as simple as possible for local development and we need to be able to run a single script that exits when the tests are complete for continuous integration. So let’s add a few scripts to get ourselves setup for this development experience.

Install:

npm install --save-dev start-server-and-test

When we run e2e test, check if it is CI, when we run:

cypress run // not open the broswer

Otherwise:

cypress open

Then we want to run our server first, after get response from server, then we want to run cypress automaticlly:

start-server-and-test start http://localhost:8080 cy:run  // run 'start' first, it will start the server wait response from 'http://localhost:8080', if get the response, then run cy:run' command

Scripts:

"scripts": {
"test": "is-ci \"test:coverage\" \"test:watch\"",
"test:coverage": "jest --coverage",
"test:watch": "jest --watch",
"test:debug": "node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand --watch",
"cy:run": "cypress run",
"cy:open": "cypress open",
"pretest:e2e:run": "npm run build",
"test:e2e": "is-ci \"test:e2e:run\" \"test:e2e:dev",
"test:e2e:run": "start-server-and-test start http://localhost:8080 cy:run",
"test:e2e:dev": "start-server-and-test dev http://localhost:8080 cy:open",
"dev": "webpack-serve",
"build": "webpack --mode=production",
"precommit": "lint-staged",
"postbuild": "cp ./public/index.html ./dist/index.html",
"start": "serve --no-clipboard --listen 8080 dist",
"lint": "jest --config test/jest-lint.js",
"format": "prettier \"**/*.js\" --write",
"validate": "npm run test && && npm run test:e2e:run",
"setup": "npm run setup && npm run validate"
},

Debug a test with Cypress

One of the greatest features of Cypress is how awesome the experience of debugging tests is. You do have to get used to the asynchronous nature of E2E testing, but once you get the hang of that debugging is pretty straightforward, and the ability to debug your own source code is awesome. The fact that your test and source code are executed in the same context also gives you some powerful advantages as well. You can potentially even expose some internal state to your Cypress app.

The way to add a debugger in the Cypress:

describe('anonymous calculator', () => {
it('can make calculation', () => {
cy.visit('/')
.getByText(/^1$/)
.click()
.getByText(/^\+$/)
.click()
.getByText(/^3$/)
.click()
.getByText(/^\=$/)
.then(subject => {
debugger
return
subject;
})

.click()
.getByTestId('total')
.should('have.text', '4');
})
})

or

.getByText(/^\=$/)
.debug()
.click()

And

we can add cy.log('xxx'):

cy.log('get by text')
cy.getByText(/^\=$/)
.debug()
.click()

In the component, you can also put a debugger:

ReactDOM.render(
<Component initialState={{}}>
{({state, setState}) => {
debugger
if(window.Cypress) {
window.appState = state
window.setAppState = setState
}

[Cypress] install, configure, and script Cypress for JavaScript web applications -- part1的更多相关文章

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

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

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

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

  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. 【php】 php能做什么

    来源:php官方文档 网站和 web 应用程序(服务器端脚本) 命令行脚本 桌面(GUI)应用程序 相信大多数人都不知道第三种,用php竟然可以写GUI,当然是基于PHP-GTK扩展写的

  2. "javac不是内部或外部命令"的解决办法

    “javac不是内部或外部命令”,而此时的java环境是好用的: 1.先检查 JAVA_HOME  =   C:\Program Files\Java\jdk1.7.0_45 classpath   ...

  3. leetcode刷题——动态规划

    知识点 专题-B-动态规划 题目 斐波那契数列 矩阵路径 数组区间 分割整数 最长递增子序列 最大连续子序列和 最长公共子序列 最长回文子序列 最长公共子串 最长回文子串 背包 题解 CS-Notes ...

  4. go的相关用法

    1. have gone to和have been to的区别 have gone to和have been to的区别如下: 1.have gone to,第三人称时用 has gone to ha ...

  5. Leetcode 402.移掉k位数字

    移调k位数字 给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小. 注意: num 的长度小于 10002 且 ≥ k. num 不会包含任何前导零. 示例 1 : ...

  6. 九度oj 题目1108:堆栈的使用

    题目描述: 堆栈是一种基本的数据结构.堆栈具有两种基本操作方式,push 和 pop.Push一个值会将其压入栈顶,而 pop 则会将栈顶的值弹出.现在我们就来验证一下堆栈的使用. 输入: 对于每组测 ...

  7. nginx的正则

    ~      为区分大小写的匹配. ~*     不区分大小写的匹配(匹配firefox的正则同时匹配FireFox). !~     不匹配的 !~*    不匹配的 .     匹配除换行符以外的 ...

  8. 虚拟机安装centos6.5

    最近想搞一下代码覆盖率的jacoco,需要在linux环境下部署一套jenkins.故需要装一个centos的虚拟机. 一.安装虚拟机. 下载后安装一个虚拟机,我选择的是VMware虚拟机 二.安装c ...

  9. BZOJ 1185 [HNOI2007]最小矩形覆盖 ——计算几何

    程序写的太垃圾,卡不过去. GG,甘拜下风. #include <map> #include <cmath> #include <queue> #include & ...

  10. Mychael原创题 洛谷T23923 Mychaelの水题 【题解】

    原题链接 题目大意: 有来自三个地区的人各a,b,c位,他们排成了一排.请问有多少种不同类型的排法,使得相邻的人都来自不同的地区 \(a,b,c<=200\) 答案取模 题解 弱弱的标程解法 设 ...