Postman新旧版本的代码脚本、断言、获取请求、响应体区别

目录

版本更新前:例子代码

断言区别 :

版本更新前:

版本更新后:

获取响应体区别: 

版本更新前:

版本更新后:


版本更新前:例子代码

1接口

//后置
var data = JSON.parse(responseBody);

tests["the search status"] = data.status === 0
tests["the search msg"] = data.msg === "success"

// console.info(request.data)

pm.environment.set("tripType", JSON.parse(request.data).tripType)
pm.environment.set("adultNumber",JSON.parse(request.data).adultNumber)
pm.environment.set("childNumber",JSON.parse(request.data).childNumber)

pm.environment.set("search_shoppingResultList",JSON.stringify([].concat(data.shoppingResultList)))
pm.environment.set("search_flightList",JSON.stringify([].concat(data.flightList)))

2接口 

//前置
var routing_data = "HJUK001-2509343459224a2beab70d0|33";


/** 根据routing_data 查找航段数据作为请求参数  */
var search_shoppingResultList = JSON.parse(pm.environment.get("search_shoppingResultList"));
var search_flightList = JSON.parse(pm.environment.get("search_flightList"));

var routing_search_fromSegment = []
var routing_search_retSegments = []

for(var i in search_shoppingResultList){
    // 找到指定data
    if(routing_data == search_shoppingResultList[i].data){
        // 定位到指定data的flightRefList
        var rl =search_shoppingResultList[i].flightRefList
        // 找到flightRefList内的段数
        for(var j in rl){
            for(var k in search_flightList){
                // 找到指定data中search_flightList中的flightRefNum对应的数据--去程
                if(rl[j].flightRefNum == search_flightList[k].flightRefNum && rl[j].segmentNo==1){
                    // 将rl[j].seatClass的值赋给search_flightList[k].seatClass这个变量
                    search_flightList[k].seatClass=rl[j].seatClass;
                    // 将search_flightList[k]放到routing_search_fromSegment中,作为请求参数
                    routing_search_fromSegment.push(search_flightList[k])
                }else if(rl[j].flightRefNum == search_flightList[k].flightRefNum && rl[j].segmentNo==2){
                    search_flightList[k].seatClass=rl[j].seatClass;
                    routing_search_retSegments.push(search_flightList[k])
                }
            } 
        }
    }
}


pm.environment.set("routing_search_fromSegment",JSON.stringify(routing_search_fromSegment))
console.log(routing_search_fromSegment)
pm.environment.set("routing_search_retSegments",JSON.stringify(routing_search_retSegments))
pm.environment.set("routing_data",routing_data)

//后置
var data = JSON.parse(responseBody);

tests["the search status"] = data.status === 0
tests["the search msg"] = data.msg === "成功"

pm.environment.set("sessionId",data.sessionId);
pm.environment.set("routing_verify_fromSegments",JSON.stringify(data.routing.fromSegments) )
pm.environment.set("routing_verify_retSegments",JSON.stringify(data.routing.retSegments) )
pm.environment.set("routing_data", data.routing.data)

 other:

pm.environment.set("order_content",responseBody)
pm.environment.set("order",responseBody)
var data = JSON.parse(responseBody);

tests["Status code is 0"] = data.status === 0
tests["The order msg is 成功"] = data.msg === "成功"

pm.environment.set("pnrCode",data.pnrCode);

console.log(""+data.pnrCode)

断言区别 :

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

版本更新前:
tests["the search status"] = data.status === 0
tests["the search msg"] = data.msg === "success"
版本更新后:
//断言响应体内的内容
pm.test("the search status", function() {
    // var jsonData = pm.response.json();
    // equal or eql
    pm.expect(data.status).to.equal(0)
});
pm.test("the search msg", function() {
    pm.expect(data.msg).to.eql("success")
});


pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});


// 断言系统响应的
// pm.test("Status code is 200", function () {
//     pm.response.to.have.status(200);
// });

获取响应体区别: 

版本更新前:
//获取请求数据
var requestData = request.data


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


//获取响应数据(JSON格式)
var data = pm.response.json()

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在Postman中进行断言获取响应头,可以使用以下方法: 首先,使用`pm.response.headers`对象来获取所有的响应头。该对象包含了响应中的所有头部信息。 为了断言获取响应头,可以使用`pm.test`函数结合`pm.response.headers`对象进行断言。 例如,如果想要断言响应头中的Content-Type头部是否存在,可以使用以下代码: `pm.test("Content-Type header exists", function () { pm.expect(pm.response.headers.has("Content-Type")).to.be.true; });` 在该代码中,`pm.response.headers.has("Content-Type")`用于检查响应头中是否存在Content-Type头部,如果存在则返回true,否则返回false。断言语句`pm.expect(pm.response.headers.has("Content-Type")).to.be.true`用于判断结果是否为true。 如果想要断言响应头中的某个头部的值是否等于指定的值,可以使用类似的方法。 例如,如果想要断言响应头中的Content-Type头部的值等于"application/json",可以使用以下代码: `pm.test("Content-Type header value is correct", function () { pm.expect(pm.response.headers.get("Content-Type")).to.equal("application/json"); });` 在该代码中,`pm.response.headers.get("Content-Type")`用于获取Content-Type头部的值,断言语句`pm.expect(pm.response.headers.get("Content-Type")).to.equal("application/json")`用于判断值是否等于"application/json"。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [postman断言详解](https://blog.csdn.net/m0_58026506/article/details/126485664)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值