cypress 是目前 e2e 很火的一个测试组件,内部绑定了 macha、chai、chai-jquery 之类的断言,为了让代码代码
更有说服力,减小提交测试错误,进行 e2e 测试显然是很是有必要的。html
官网 \
GitHub
借鉴官网一句话来讲:前端
Cypress is a next generation front end testing tool built for the modern web. We address the key
pain points developers and QA engineers face when testing modern applications.
<!-- more -->vue
node v9.5\ npm v5.5
e2e 测试端对端测试的简称, e2e 即为end to end
,
指任意一我的的社交、交易、休闲均可以直接与另外任意一我的产生关系,去中心化、渠道化.node
作前端怎么少的多的了 npm 呢react
$ npm i -D cypress
而后为了方便起见,我们在package.json
中写入下面脚本:jquery
{ "scripts": { "e2e:open": "cypress open", "e2e:run": "cypress run" } }
运行npm run e2e:open
,启动一个 cypress 的服务器,以下:git
以下图这就完成了一个启动一个 cypress。第一次点开时候,cypress 会帮你建立一个初始化配置目录,这是
cypress 推荐的目录的结构,固然也能够本身建立。github
点击 example_spec.js 文件,而后能够看到以下界面,cypress 开始测试:web
上面就看到 cypress 的运行过程了。下面看看 example_spec.js(文件的位置
:projectName/cypress/integration)文件中写了啥:npm
describe('Kitchen Sink', function() { it('.should() - assert that <title> is correct', function() { // ... } })
这是主要结构的,下面大部分都是在一个it
函数内部,是测试里面的回调函数。详细能够查看 TDD 和 BDD 测试
框架,cypress 绑定了这些测试框架。
这是 cypress 里面一个很重要的方法,能够访问一个连接,列入 example.js 文件以下:
beforeEach(function() { // Visiting our app before each test removes any state build up from // previous tests. Visiting acts as if we closed a tab and opened a fresh one cy.visit('https://example.cypress.io/commands/querying') })
这里就是在前置钩子函数里面访问了https://...../querying
这个连接。若是代码须要浏览器调试,好比用户交
互点击,用户输入之类的。第一步就是访问:cy.visit
仍是从 example_spec.js 问中提及:
it('cy.get() - query DOM elements', function() { // https://on.cypress.io/get // Get DOM elements by id cy.get('#query-btn').should('contain', 'Button') // Get DOM elements by class cy.get('.query-btn').should('contain', 'Button') cy.get('#querying .well>button:first').should('contain', 'Button') // ↲ // Use CSS selectors just like jQuery })
这里定义了一个测试单元,在这个里面作了啥呢?第一步获取 id 为 query-btn 这个按钮。接下来 should 操做
,奉上一张表自行查看:
cy.get 还有一个玩法就是 cy.get('@app')这种,意思说以前你已经cy.get('.app').as('app')
,不须要再次获
取了,直接使用别名就行了
从官网截图的表格,详
细jquery-chai 文档表格
这里看到cy.get()
和jquery.$
是否是很像,在官网这里说了这样一句话:
The querying behavior of this command matches exactly how $(…) works in jQuery.
因此能够将 cy.get()当$同样来用便可,不过这里返回的不过 jquery 对象罢了,这里返回的事经过 cypress 包
装过的对象能够在控制台看到这样的东西,见下图:
是一个用于 cypress 全部方法的对象。而后能够操做他的 api 了。
第一部分,主要是查询,查询页面元素是否按照咱们开发想要的存在,下面看第二部分:
context('Actions', function() { beforeEach(function() { cy.visit('https://example.cypress.io/commands/actions') }) // Let's perform some actions on DOM elements // https://on.cypress.io/interacting-with-elements it('.type() - type into a DOM element', function() { // https://on.cypress.io/type cy .get('.action-email') .type('fake@email.com') .should('have.value', 'fake@email.com') // .type() with special character sequences .type('{leftarrow}{rightarrow}{uparrow}{downarrow}') .type('{del}{selectall}{backspace}') // .type() with key modifiers .type('{alt}{option}') //these are equivalent .type('{ctrl}{control}') //these are equivalent .type('{meta}{command}{cmd}') //these are equivalent .type('{shift}') // Delay each keypress by 0.1 sec .type('slow.typing@email.com', { delay: 100 }) .should('have.value', 'slow.typing@email.com') cy .get('.action-disabled') // Ignore error checking prior to type // like whether the input is visible or disabled .type('disabled error checking', { force: true }) .should('have.value', 'disabled error checking') }) })
这一部分主要是进行获取元素交互, 下面来讲交互是如何搞得。 与 cy.get 类似还有:
既然要交互确定须要点击输入滚动,能够还存在拖拽等等。我们就暂时从输入开始提及啦
这不是一个能够直接使用的方法,要配合cy.get
使用的,做用是给空间进行输入。例如:
cy.get('input').type('hello world')
<div class="el" tabIndex="1"> This is TabIndex div. </div>
cy.get('.el').type('laldkadaljdkljasf') // 这个里面是随机字符串
cy.get('input[type=date]').type('2008-8-9')
下面直接是对 input 进行组合键盘操做
cy.get('input').type('{shift}{alt}Q')
按住键盘操做
cy.get('input').type('{alt}这里是按了一下alt后输入的内容')
还有长按键盘之类的操做,详细就看官网了这里之类奉上链
接https://docs.cypress.io/api/commands/type.html#Key-Combinations
这里就是关于键盘的组合操做。
这些就只须要利用点击事件便可,以下:
cy .get('input[type=radio]') .as('radio') .click() cy.get('@radio').should('be.checked')
下面是等待 1s
cy.wait(1000)
本身的代码:
var seconds = 0 setInterval(() => { $('#seconds-elapsed').text(++seconds + ' seconds') }, 1000)
测试代码
cy.clock() cy.visit('/index.html') cy.tick(1000) cy.get('#seconds-elapsed').should('have.text', '1 seconds') cy.tick(1000) cy.get('#seconds-elapsed').should('have.text', '2 seconds')
这里就会出现关于 clock 和 tick
的用法,更多用法看文档,我也有部分迷惑的。待后来再解决。老规矩文档地址:
地址
先复制一段出来:
{ "baseUrl": "http://localhost:8080", "pageLoadTimeout": 3000, "viewportHeight": 667, "viewportWidth": 375 }
这是一个很是精简的配置了:
viewport 两个属性
上面是 cypress 的基本用法,cypress 是基于 electron 的一个测试框架,提供 web 环境进行点对点的测试,在
programer 思惟下,进行自动化的交互操做,必要点检测说明,这是一个很是棒的用处。例如以后拥有数据埋点,
能够在固定的位置检测是否有埋点。测试想要的地方是否匹配的数据。模拟用户的点击操做,这都是很是棒的。在
jquery 操做年代,各类 id 和 class 奇怪命名下,这些均可以容易找到,在 vue 和 react 大行其道的年代,但
是却能够经过文本寻找节点。这也是很是棒的体验,更多秘密须要去体验,奉上官方地址
:官网 cypress