Cypress接口自动化5-接口驱动化数据保存json中

2021-09-28 14:00:00
wencheng
转贴:
微信公众号
115

前言


自动化测试里面参数化思想是非常重要的,同一类型的测试,只需维护不同的数据就可以了。

数据通过json 文件管理

/**
* Create by dell on 2021/4/9
* Author :wencheng
* describe :
* */
// 导入数据文件 example.json,并保存在 testData 变量中
import testDatas from '../data/example.json'
describe('接口数据驱动化', function () {
   beforeEach("获取token", function () {
       cy.token()
  })
   //遍历case
   for (const data in testDatas) {
       it(`${testDatas[data].casename}`, function () {
           //获取token
           var token = sessionStorage.getItem("Token")
           var url = testDatas[data].url
           var method = testDatas[data].method
           var header = testDatas[data].headers
           var body = testDatas[data].body
           var status_code = testDatas[data].status_code
           var header = JSON.stringify(header)
           var body = JSON.stringify(body)
           //发送请求
           cy.requests2(url, method, header, body, token, status_code)
      });
  }
})
二次封装请求
//封装http请求
Cypress.Commands.add("requests2", function (url, method, header, body, token, status_code) {
   cy.log('测试地址: ' + url)
   cy.log('请求参数: ' + body)
   cy.log('请求方法: ' + method)
   cy.log('请求头部: ' + header)
   var headers = JSON.parse(header.replace('token', token))
   if (method.toUpperCase() == "GET") {
       cy.request({
           url: url, method: method, headers: headers, body: body
      }).then(function (resp) {
           cy.log("返回结果:" + JSON.stringify(resp.body))
           expect(resp.status).to.eq(status_code)
      })
  } else if (method.toUpperCase() == "POST") {
       var randomName = require("chinese-random-name");
       var randomname = randomName.names.get3()
       var bodys = body.replace('randomname', randomname)
       var body = JSON.parse(bodys)
       cy.request({
           url: url, method: method, headers: headers, body: body
      }).then(function (resp) {
           cy.log("返回结果:" + JSON.stringify(resp.body))
           expect(resp.status).to.eq(status_code)
      })
  }
})
json 数据
[
{
   "casename": "获取配置列表信息",
   "url": "/configures/",
   "method": "GET",
   "headers": {
     "Content-Type": "application/json",
     "authorization": "JWT token"
  },
   "body": "",
   "status_code": 200
},
{
   "casename": "获取项目(多个)列表数据",
   "url": "/projects/",
   "method": "GET",
   "headers": {
     "Content-Type": "application/json",
     "authorization": "JWT token"
  },
   "body": "",
   "status_code": 200
},
{
   "casename": "返回所有项目ID和名称",
   "url": "/projects/names/",
   "method": "GET",
   "headers": {
     "Content-Type": "application/json",
     "authorization": "JWT token"
  },
   "body": "",
   "status_code": 200
},
{
   "casename": "创建项目",
   "url": "/projects/",
   "method": "POST",
   "headers": {
     "Content-Type": "application/json",
     "authorization": "JWT token"
  },
   "body": {"name": "randomname", "leader": "张三", "tester": "李四","programmer": "王五", "publish_app": "测测测", "desc": "randomdesc"},
   "status_code": 201
}
]
运行结果

发表评论
评论通过审核后显示。
联系我们
  • 联系人:郑女士
  • 联系方式: 13792883250
  • 邮箱:zhengqiaoyin@cnezsoft.com
  • 地址:青岛市黄岛区井冈山路157号中南金石国际广场A座3202室