postman接口关联

1、使用json提取器实现接口关联

var cook = pm.response.headers;//返回object类型所有响应header
var jsonData = JSON.stringify(cook);//转换成json格式
// console.log(jsonData);
var result = JSON.parse(jsonData);//解析json
var cookie="";
for(var i=0;i<result.length;i++){
 
    if(result[i].key===("Set-Cookie")){
//         console.log(result[i].value);
        cookie=cookie+result[i].value.split(";")[0]+";";
    }
    else{
        continue;
    } 
}
console.log(cookie);
pm.globals.set("Set-Cookie",cookie);
 
2、使用正则表达式
var result = responseBody.match(new RegExp('"access_token":"(.*?)"')
result[1]
 
3、内置动态参数以及自定义
{{$timestamp}}  生成当前时间戳
{{$randomInt}}  生成0-100之间的随机数
{{$guid}}  生成速记GUID字符串
自定义动态参数:
var time  = Date.now()
 
4、后置判断:
判断返回接口参数msg为0
pm.test("Test Result:",function(){
   var jsonData = pm.response.json();
   pm.expect(jsonData.msg).to.eql("ok")
 
});
判断接口code返回值为200
pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([200]);
});
判断接口status为200
pm.test("Status test", function () {
    pm.response.to.have.status(200);
});
 
5、postman使用文档:https://learning.postman.com/docs/sending-requests/requests/
6、cookie 通常包含两个数据:每个用户的唯一 ID 和站点名称。
 
posted @ 2022-09-05 14:30  陈文芝  阅读(386)  评论(0编辑  收藏  举报