当前位置:   article > 正文

Postman新旧版本的代码脚本、断言、获取请求、响应体区别_postman 断言老式写法和新式写法

postman 断言老式写法和新式写法

目录

版本更新前:例子代码

断言区别 :

版本更新前:

版本更新后:

获取响应体区别: 

版本更新前:

版本更新后:


版本更新前:例子代码

1接口

  1. //后置
  2. var data = JSON.parse(responseBody);
  3. tests["the search status"] = data.status === 0
  4. tests["the search msg"] = data.msg === "success"
  5. // console.info(request.data)
  6. pm.environment.set("tripType", JSON.parse(request.data).tripType)
  7. pm.environment.set("adultNumber",JSON.parse(request.data).adultNumber)
  8. pm.environment.set("childNumber",JSON.parse(request.data).childNumber)
  9. pm.environment.set("search_shoppingResultList",JSON.stringify([].concat(data.shoppingResultList)))
  10. pm.environment.set("search_flightList",JSON.stringify([].concat(data.flightList)))

2接口 

  1. //前置
  2. var routing_data = "HJUK001-2509343459224a2beab70d0|33";
  3. /** 根据routing_data 查找航段数据作为请求参数 */
  4. var search_shoppingResultList = JSON.parse(pm.environment.get("search_shoppingResultList"));
  5. var search_flightList = JSON.parse(pm.environment.get("search_flightList"));
  6. var routing_search_fromSegment = []
  7. var routing_search_retSegments = []
  8. for(var i in search_shoppingResultList){
  9. // 找到指定data
  10. if(routing_data == search_shoppingResultList[i].data){
  11. // 定位到指定data的flightRefList
  12. var rl =search_shoppingResultList[i].flightRefList
  13. // 找到flightRefList内的段数
  14. for(var j in rl){
  15. for(var k in search_flightList){
  16. // 找到指定data中search_flightList中的flightRefNum对应的数据--去程
  17. if(rl[j].flightRefNum == search_flightList[k].flightRefNum && rl[j].segmentNo==1){
  18. // 将rl[j].seatClass的值赋给search_flightList[k].seatClass这个变量
  19. search_flightList[k].seatClass=rl[j].seatClass;
  20. // 将search_flightList[k]放到routing_search_fromSegment中,作为请求参数
  21. routing_search_fromSegment.push(search_flightList[k])
  22. }else if(rl[j].flightRefNum == search_flightList[k].flightRefNum && rl[j].segmentNo==2){
  23. search_flightList[k].seatClass=rl[j].seatClass;
  24. routing_search_retSegments.push(search_flightList[k])
  25. }
  26. }
  27. }
  28. }
  29. }
  30. pm.environment.set("routing_search_fromSegment",JSON.stringify(routing_search_fromSegment))
  31. console.log(routing_search_fromSegment)
  32. pm.environment.set("routing_search_retSegments",JSON.stringify(routing_search_retSegments))
  33. pm.environment.set("routing_data",routing_data)
  1. //后置
  2. var data = JSON.parse(responseBody);
  3. tests["the search status"] = data.status === 0
  4. tests["the search msg"] = data.msg === "成功"
  5. pm.environment.set("sessionId",data.sessionId);
  6. pm.environment.set("routing_verify_fromSegments",JSON.stringify(data.routing.fromSegments) )
  7. pm.environment.set("routing_verify_retSegments",JSON.stringify(data.routing.retSegments) )
  8. pm.environment.set("routing_data", data.routing.data)

 other:

  1. pm.environment.set("order_content",responseBody)
  2. pm.environment.set("order",responseBody)
  3. var data = JSON.parse(responseBody);
  4. tests["Status code is 0"] = data.status === 0
  5. tests["The order msg is 成功"] = data.msg === "成功"
  6. pm.environment.set("pnrCode",data.pnrCode);
  7. console.log(""+data.pnrCode)

断言区别 :

更多断言方式可以去看BDD chains(我是看不太懂)

版本更新前:
  1. tests["the search status"] = data.status === 0
  2. tests["the search msg"] = data.msg === "success"
版本更新后:
  1. //断言响应体内的内容
  2. pm.test("the search status", function() {
  3. // var jsonData = pm.response.json();
  4. // equal or eql
  5. pm.expect(data.status).to.equal(0)
  6. });
  7. pm.test("the search msg", function() {
  8. pm.expect(data.msg).to.eql("success")
  9. });
  10. pm.test("Response time is less than 200ms", function () {
  11. pm.expect(pm.response.responseTime).to.be.below(200);
  12. });
  13. // 断言系统响应的
  14. // pm.test("Status code is 200", function () {
  15. // pm.response.to.have.status(200);
  16. // });

获取响应体区别: 

版本更新前:
  1. //获取请求数据
  2. var requestData = request.data
  3. //获取响应数据(JSON格式)
  4. var data = JSON.parse(responseBody);
版本更新后:
  1. //获取请求数据
  2. var requestData = pm.request.body.raw
  3. //获取响应数据(JSON格式)
  4. var data = pm.response.json()

还是习惯了更新前的...

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/681433
推荐阅读
相关标签
  

闽ICP备14008679号