Cypress基础入门

前言

Cypress 由一个免费的、开源的、本地安装的 Test Runner 和一个用于记录测试的 Dashboard 服务组成.
Cypress 不受与 Selenium 相同的限制.
cypress能够编写更快、更容易和更可靠的测试。
cypress可以测试任何在浏览器中运行的东西。

安装

  1. 桌面版安装:
    下载地址: http://download.cypress.io/desktop

  2. 命令行安装
    首先查看自己的npm版本是否是新版本,如果不是最新则进行升级

npm -v
npm install npm -g

npm install cypress --save-dev

安装可能会出现速度慢的问题,可以试一下下面的方法

方法1:cnpm命令代替npm

首先安装cnpm命令

npm install --global cnpm

方法2:使用国内淘宝链接下载

npm install cypress --registry=http://registry.npm.taobao.org

方法3:配置下载链接

npm config set registry http://registry.npm.taobao.org

检查配置是否成功

npm config get registry

启动

node_modules.bincypress open

启动cypress如果报文件找不到的错误:Can't start server EEXIST: file already exists, mkdir 'C:** ode_modules.bincypress'
可以将命令改为全局启动

node_modules.bincypress open --global

第一个测试用例

创建一个.js的测试文件

describe('我的第一个测试', () => {
    it('测试用例', () => {
        expect(true).to.equal(true);
    }
    )
}
)

打开编写的测试用例并运行

一个真实的测试用例

describe('我的第一个测试',function(){
    it('百度测试用例:',function(){
        cy.visit('http://www.baidu.com') //访问url
        cy.title().should('contain','百度一下,你就知道')   //验证页面 title 是否正确
        cy.get('#kw').type('python')       //根据 css 定位搜索输入框
        .should('have.value','python')  //验证关键字自动是否展示正确
        cy.get('#su').click()   //根据 css 定位搜索按钮并点击
        cy.url().should('include','wd=python')     //验证目标url 是否正确包含关键字
        cy.title().should('contain','python_百度搜索')  //验证页面 title 是否正确
        cy.get('[id="1"]').should('contain','python')    // 验证第一个结果中是否包含TesterHome
        cy.screenshot()
    })
})

原文地址:https://www.cnblogs.com/huny/p/14323168.html