前端analysis | 3w & 1h

《自动化测试》 - Cypress介绍

2020-11-09

Cypress介绍

Cypress

优点

  • 1.随时可截屏 - Cypress captures snapshots at the time of test execution.
  • 2.不需要编写等待命令 - One doesn’t need to add explicit or implicit wait commands in test scripts,
  • 3.可以使用spy - Developers or QAs can use Spies, Stubs, and Clocks to verify and control the behavior of server responses, functions, or timers.
  • 4.元素一定会先存着- The automatic scrolling operation ensures that an element is in view before performing any action (for example Clicking on a button)
  • 5.在真实浏览器中运行,运行速度更快

缺点

  • 1.不能并发- cannot use Cypress to drive two browsers at the same time
  • 2.不能多tab- It doesn’t provide support for multi-tabs
  • 3.仅支持js - only supports JavaScript for creating test cases
  • 4.不支持chrome外浏览器 - doesn’t provide support for browsers like Safari and IE at the moment.
    1. Limited support for iFrames

Selenium

优点

  • 1.语言不限定,浏览器也不限定
  • 2.Compatible with multiple OS like Windows, Linux, Unix, Mac
    1. browsers like Safari, Chrome, Firefox, etc.
  • 4.Provides Concise APIs

缺点

  • 1.没有内置命令,产生结果 - automatic generation of test results,
  • 2.文档体验差
  • 3.界面加载操作麻烦 - Handling page load or element load is difficult
  • 4.Limited support for testing images
  • 5.Creating test cases is time-consuming
  • 6.环境配置麻烦 - Difficult to set up test environment as compared to Cypress

如何选择

1.需要在IE,Firefox,以及SauceLabs,BrowserStack运行,就需要选择Selenium
2.如果应用基于Angular,React,Vue开发,那么可以选择Cypress

环境搭建

安装

1
2
3
4
5
6
7
8
9
# 本地安装
npm install cypress -D

# 全局安装
npm i -g cypress

# yarn
yarn add cypress --dev

启动

1
2
3
4
5
npx cypress open 

$(npm/.bin)/cypress open

yarn run cypress open

原理

webpack打包管理所有测试代码到一个bundle
然后处于iframe中运行,进而操作dom

使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏