Cypress 笔记

Cypress 的特点

1. 基于 node.js 环境的 E2E 前端自动化测试框架
2. 支持 Chrome 和 Firefox 浏览器,自动等待
3. 不用 web driver 驱动浏览器,运行速度快
4. 支持失败用例自动截屏
5. 对运行过程的自动录制视频
6. 时光穿梭看各步骤的 snapshoot
7. 支持 CI 和并发运行,收集测试结果
8. 支持 Junit+Allure 生成测试报告

安装和使用

安装 node.js 之后,运行命令安装 cypress
$ npm install cypress --save-dev

启动待测服务 / 应用
$ npm start & wait-on http://localhost:8080

启动 Cypress Test Runner
$ npx cypress open

运行指定用例,使用 Chrome 浏览器
$ npx cypress run --spec "cypress/integration/login.js" --browser chrome

演示用例 login.js

//用例集
describe('UI loginTest',function(){   
    //用例
    it('console root login',function(){   
        //打开页面
        cy.visit('http://localhost:8080/rootlogin')
        cy.url().should('contain','randomKey')
        //输入用户,密码,点击登录
        cy.get('[name=username]').type('root')
            .should('have.value','root')
        cy.get('[name=password]').type('password')
            .should('have.value','password')
        cy.get('.el-button').click()
        //登录后进入页面
        cy.title().should('contain','系统管理员')
        cy.wait(1000)
        //登出
        cy.contains('root').click()
        cy.contains("退出登录").click()
        cy.url().should('contain','randomKey')
    })
    
    it('console user login', function(){
        // user login test 
    })
})

元素定位方式

cy.get() - 按 CSS 或元素特定属性进行定位,累 JQuery selection

cy.contains() - 按特定字符串匹配进行元素定位

cy.xpath() - 按 XPATH 进行定位,需安装 cypress-xpath:

  1) npm install cypress-xpath --save-dev

  2) 在 cypress/support/index.js 中添加: require('cypress-xpath')

页面操作

HOOK 钩子示例

describe('Hooks', () => {
  before(() => {
      // runs once before all tests in the block
      cy.log("所有的用例之前只执行一次,测试准备工作")
  })
  after(() => {
      // runs once after all tests in the block
      cy.log("所有的用例之后只执行一次")
  })
  beforeEach(() => {
      // runs before each test in the block
      cy.log("每个用例之前都会执行")
  })
  afterEach(() => {
      // runs after each test in the block
      cy.log("每个用例之后都会执行")
  })
  it('test case 1', () => {
      cy.log("test case 1")
      expect(true).to.eq(true)
  })
  it('test case 2', () => {
      cy.log("test case 2")
      expect(true).to.eq(true)
  })
})

测试数据准备

describe('The Dashboard Page', () => {
  beforeEach(() => {
    // reset and seed the database prior to every test
    // seed a user in the DB that we can control from our tests
    cy.exec('npm run db:reset && npm run db:seed')
    cy.request('POST', '/test/seed/user', { username: 'jane.lane' })
      .its('body')
      .as('currentUser')
  })

it('logs in programmatically without using the UI', function () {
    // destructuring assignment of the this.currentUser object
    const { username, password } = this.currentUser
    // programmatically log us in without needing the UI
    cy.request('POST', '/login', {
      username,
      password
    })
    // now that we're logged in, we can visit
    // any kind of restricted route!
    cy.visit('/dashboard')
   // our auth cookie should be present
    cy.getCookie('your-session-cookie').should('exist')
   // UI should reflect this user being logged in
    cy.get('h1').should('contain', 'jane.lane')
  })
})

参数化

describe('参数化案例,输入不同的值', function() {
    // 定义测试数据
    var testdatas = ["北京", "上海", "南京"]
    // 前置-打开浏览器
    before(() => {
          cy.visit('https://www.baidu.com')
        })
    // 参数化
    testdatas.forEach((event) => {
        it("百度输入框功能", function () {
            cy.get('#kw').type(event)
                .should('have.value', event)
                .clear()
                .should('have.value', '')
        })
    })
})

Fixture 文件读取测试数据

把账号和密码放 login.json 文件,

{
  "username": "admin",
  "password": "password",
  "include": "/test/my/",
  "text": "测试"
}

用 login.json 中的 fixture 数据登录网页

describe('登陆网站', function() {
    beforeEach(() => {
          cy.visit('http://ip:8080/login');
          cy.fixture('login.json').as('login')
        })

    it("登陆案例", function (){
        cy.log("读取login.json文件账号:"+this.login.username)
        cy.log("读取login.json文件密码:"+this.login.password)
        // let 定义变量
        let username = this.login.username
        let password = this.login.password
        let include = this.login.include
        let text = this.login.text

        //输入用户名
        cy.get('#account').type(username)
              .should('have.value', username)
        // 输入密码
        cy.get('[name="password"]').type(password)
              .should('have.value', password)
        // 提交表单
        cy.get('#submit').click()
        // 判断页面跳转
        cy.url().should('include', include)
        cy.get('body').should('contain', text)
    })
})

多个用例共享 Cookie

describe('Dashboard', () => {
    before (() => {
        cy.login()     // log in only once before any of the tests run.
    })
    
    beforeEach (() => {
        Cypress.Cookies.preserveOnce('session_id', 'remember_token')
    })
    
    it('displays stats', () => {
        // ...
    })
    
    it('can do something', () => {
        // ...
    })
})

JUnit/Allure 产生测试报告

在 cypress.json 中添加配置, [hash] 使得每个测试集产生自己的测试结果 xml 文件(否则会被覆盖):

{
  "reporter": "junit",
  "reporterOptions": {
    "mochaFile": "results/test_report_[hash].xml", 
    "toConsole": true
  }
}

执行用例

$ npx cypress run

产生报告

$ allure generate ./result/ -o ./report/ --clean

关闭 Chrome 跨域检查

在 cypress.json 中加入如下配置:

{
    "chromeWebSecurity": false,  
}

重跑失败用例的插件

Cypress Test Runner 上单跑或跳过某个用例
https://github.com/bahmutov/cypress-skip-and-only-ui

重跑失败的用例
https://github.com/Bkucera/cypress-plugin-retries

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Cypress是一个JavaScript端到端测试框架,它提供了一组API来编写端到端测试,可以在浏览器中运行这些测试。以下是Cypress的一些基本概念和用法: 1. 安装Cypress ```shell npm install cypress --save-dev ``` 2. 运行Cypress ```shell npx cypress open ``` 运行上述命令后,Cypress Test Runner会启动,你可以在其中运行测试用例。 3. 编写测试用例 在Cypress中,测试用例被称为“规范”(spec)。规范是一个JavaScript文件,其中包含一组测试用例。以下是一个简单的规范示例: ```javascript describe('My First Test', () => { it('Does not do much!', () => { expect(true).to.equal(true) }) }) ``` 在上述示例中,我们定义了一个规范,其中包含一个测试用例。测试用例使用`it`函数定义,其中包含一些断言,这些断言用于验证测试用例的正确性。 4. 运行特定的规范文件 如果你只想运行特定的规范文件,可以使用以下命令: ```shell npx cypress run --spec path/to/spec.js ``` 在上述命令中,`path/to/spec.js`是规范文件的路径。 5. 使用cy.fixture()加载测试数据 Cypress提供了一个`cy.fixture()`函数,用于加载测试数据。你可以将测试数据存储在JSON文件中,并使用`cy.fixture()`函数加载它们。以下是一个示例: ```javascript describe('My First Test', () => { beforeEach(() => { cy.fixture('example.json').as('example') }) it('loads example data', function () { cy.visit('https://example.com') cy.get('#username').type(this.example.username) cy.get('#password').type(this.example.password) cy.get('#submit').click() }) }) ``` 在上述示例中,我们使用`cy.fixture()`函数加载了一个名为`example.json`的JSON文件,并将其存储为别名`example`。在测试用例中,我们使用`this.example`访问加载的测试数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据搜集者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值