cypress 前端自动化测试实践二(编写示例)

一、首先自己搭建一个vue+elmentui+mockjs的前端项目

参照这篇文章

https://blog.csdn.net/qq_39046786/article/details/107689358

搭建完成后得到如图所示页面

二、编写登陆页面自动化的示例

首先删除这里面的官方示例,新建一个文件夹,命名文login,表示这是前端登陆页面的所有自动化测试用例

内容入下

describe('The Login Page', () => { // 这个相当于测试套件
  const userLogin = Cypress.env('userLogin') // 把环境变量的值赋给userLogin
  const { username, password} = userLogin// 把userlogin的值赋给 username和password

  it('输入账号错误', () => { // 这个相当于测试用例的描述
    cy.visit('/login')
    cy.get('input[placeholder=账号]').type(username)// 定位账号输入框
    cy.get('input[placeholder=密码]').type(password)// 定位密码输入框
    cy.get('span').contains('login').click()// 定位提交登陆请求的按钮,并点击
    cy.get('body').should('contain', '账号错误')// 断言
  })
})

 

上文中的环境变量在cypressjson里面设置

{ "baseUrl": "http://localhost:8080",
  "integrationFolder": "cypress/integration",
  "testFiles": "**/*.spec.js",
  "video": false,
  "viewportHeight": 800,
  "viewportWidth": 1024,
  "chromeWebSecurity": false,
  "reporter": "junit",
  "reporterOptions": {
    "mochaFile": "cypress/reports/my-test-output[hash].xml",
    "toConsole": true
  },
  "env": {
    "userLogin": {
      "username": "zzz",
      "password": "zzz"
    }
  }
}

 

然后运行cypress

npm run cypress:open

注意这里需要另启一个控制台输入此命令,因为此时还没有讲用一个命令同时运行前端和cypress,这里提一下用的是

start-server-and-test,下一篇文章会讲到

此时cypress就启动起来了

点击运行得到结果

至此一个简单的登陆示例就完成了,下一篇讲保存登陆状态在其他页面使用

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值