1.1postman常用函数

随机数

Math.random()
Math.random()*10  //0.0-10之前的数字,小数 9.465413246541

Math.floor(Math.random()*10)  //直接舍弃小数部分 0,1,2,3,4,5,6,7,8,9
Math.floor(9.232323123313213)=9

Math.ceil()  //进1法,整数部分加1,
Math.floor(9.232323233231121)=10

Math.round() //去整数,但是四舍五入
案例
案例1:随机生成一个用户名 规定jason001,jason002.jason003
var num="";

//循环生成3位数
for(var i=0;i<3;i++){
    num +=Math.floor(Math.random()*10);

}
//生成用户名
var username = "jason"+num;
//打印username
console.log(username)



案例2,:随机生成一个电话号码
#1,前三位,135,138,156,181,199........
var list=["135","138","156","181","199"];
//list[下标]   取列表中的值,下边是随机变化的(0-list.length()之间的整数)
//求列表的长度 list.lenght
//随机获取一个下标
var index= Math.floor(Math.random()*list.length)
var pre_phone3=list[index];
//后8位,随机的8位数字
var back_phone8 ="";
for(var i=0;i<8;i++){
    back_phone8 +=Math.floor(Math.random()*10);
}
//电话号码
var phonenum = pre_phone3+back_phone8
//打印电话号码
console.log(phonenum)
//设置为本地变量,请求直接{{phonenum}}调用
pm.variables.set('phonenum',phonenum);

提取响应数据(提取json格式的数据)

方法1:
console.log(pm.response.code)   //提取状态码 200
console.log(pm.response.reason())  //提取响应信息 OK
console.log(pm.response.headers)  //提取响应头
console.log(pm.response.responseTime)  //提取响应时间
console.log(pm.response.text())  //提取响应body,不管body是什么格式的,都可以采用这个函数来提取
console.log(pm.response.json())  //提取json格式的响应数据,并转化为json对象(字典)

方法2:
var code=responseCode.code;        //获取响应中的状态码200
var codename=responseCode.name;   //提取响应信息 OK
var code=responseTime;            //提取响应时间
var headers=postman.getResponseHeader('Content=Type');  //提取响应头中的Content=Type
var coookies=postman.getResponseCookie('').value;       //获取响应cookies中的value值,括弧啊中填写cookies的name值
var body=getResponseBody;                              //获取响应正文(是字符串,如果是json格式,那就是json字符串)
var jsonData= json.parse(body)

断言响应信息

状态码:
方法1:
pm.test("Status code is 200", function () {
     pm.response.to.have.status(200);
});

方法2:
var code=pm.response.code
pm.test("状态码是否为200",function(){
    pm.expect(code).to.equal(200);
});

方法3:
var code =pm.response.code
if(code=200){
    tests['状态码一致!']=true;
}else{
    tests['状态码不一致']=false;
}

方法4:
tests['状态码的检验']=pm.response.code===200;

pm.test("状态信息为OK",function(){
     pm.expect(pm.response.reason()).to.equal('OK')
})

响应body:
判断返回的json对象用的具体的某个关键的键所对应的值。
这个比较适合于json数据比较大,只需要检查其中比较关键的键值对就行了
方法1:response等于xxx
pm.test("Body matches string", function () {
     var jsonData =pm.response.json();
     pm.expect(pm.jsonData.status).to.equal(2);
     pm.expect(pm.jsonData.status).to.equal("xxxxxxxxxxx")
});

方法2:response包含于xxx
var jsonData=pm.response.json();
var jsonStringData= JSON.stringify(jsonData)
pm.test("Body matches string",function(){
     pm.expect(jsonStringData).to.include('xxxxxxxxxxx')
     pm.expect(jsonStringData).to.include('xxxxxxxxxxx')
})

方法3:判断整个json格式的数据是否一致
pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData).to.eql({
    "msgid": "",
    "errcode": 0,
    "errmsg": "",
    "data": {
        "user_id": 1,
        "user_name": "admin",
        "user_name_type": 0,
        "password": "",
        "access_token": "50ba393c-1b58-4e3c-bcff-577a83b57c2d",
        "no_password": 0,
        "parent_id": 0,
        "grade": 0,
        "role_id": 1,
        "permission1": 0,
        "permission2": 0,
        "permission3": 0,
        "permission4": 0,
        "staff_id": 1,
        "status": 1,
        "manager_type": 0,
        "create_time": 18042xxx55850,
        "modify_time": 0,
        "expired_time": 0
    },
    "notices": null,
    "auth": null
});
});

方法4:判断jsondata中第1个data的real_name
pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.data.data[1].real_name).to.eql("曾某某");
});

案例
//案例1:当json格式的数量比较大,不能全量匹配整个json格式
//案例2:返回的json格式的数据是变化的,这种一般检查(关键的键是够存在?对应的值是否正确?)
pm.test('判断返回的body是够为json格式',function(){
    pm.response.to.have.jsonBody({});
    var shema1={
     Type:"object",      //类型为对象类型
     properties:{        //内部应该包含那些属性
          "error_code":{Type:"number"},
          "errmsg":{Type:"string"},
        }
        // require:["data"]
    }
    pm.response.to.have.jsonSchema(shema1);
});

//案例3:如果返回的json格式的Body数据是动态变化的怎么办?
1,首先检查是够为json格式
2,可以只检查body中是否存在期望的键,主要检查哪些核心的键
3,可以检查返回json结构是否正确
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容