首页 > 其他分享 >postman:tests模块

postman:tests模块

时间:2022-08-26 17:46:04浏览次数:32  
标签:body tests postman json 模块 var pm

设置环境变量

postman.setEnvironmentVariable("key","value");

设置全局变量

postman.setGlobalVariable("key", "value");

变量引用

注:定义的变量在脚本中引用使用pm.globals.get获取,在其他地方要用双大扩号{{}} ,如:变量名为:test-global,在脚本中使用:pm.globals.get("test-global"),在请求时使用:{{test-global}};

postman.setGlobalVariable("test-global", "global环境变量值2222");
var ret = pm.globals.get("test-global");
tests[ret]= true;

获取json数据对象

var jsonData = pm.response.json();

获取json中的某个值

var value =data.key (注:value中有多组可以用data.keys[0].key1获取keys中第一组的key1的值)

查看数据的组数list长度

var num = jsonData.keys.length;

把xml的对象转化为json对象

varjsonObject=xml2Json(responseBody);

获取Header中的值

var value=postman.getResponseHeader("Content-Type");

确认Header中的是否有某字段

responseHeaders.hasOwnProperty("Content-Type")  结果:true or  false

获取响应返回值

varCode= responseCode.code


获取响应时间

var time=responseTime


判断响应数据中是否包含某个值

responseBody.has("true")

 

检查点(断言)

tests["Body matches string"]=responseBody.has("响应数据json的任意值 ");

tests["日志信息"]=true(等号后跟boolean值表示接口是否通过)

var jsonData = pm.response.json();
var num = jsonData.debug_requests.length;
var debug = new Array(num);

for(i=0;i<num;i++){
    debug[i] = jsonData.debug_requests[i];
    var url = debug[i].url;
    if(url.includes("blender")){

    tests["Body matches string1"]=url.has("/aaaa");
    tests["Body matches string2"]=url.has("bbbb&");
    break;
            }
}

 

 

返回结果的列表中字段值的校验

var jsonData = pm.response.json();
var num = jsonData.debug_requests.length;
var debug = new Array(num);

for(i=0;i<num;i++){
    debug[i] = jsonData.debug_requests[i];
    var url = debug[i].url;
    if(url.includes("目标请求")){
    pm.test("目标请求验证", function () {
            pm.expect(url).to.include("/AAAA");
            });
    pm.test("目标请求验证:bbbb", function () {
            pm.expect(url).to.include("bbbb=cccc&");
            });
            break;
            }
}

postman在Test Results中输出多个变量值

var header=postman.getResponseHeader("Content-Type");
var header2=responseHeaders.hasOwnProperty("Content-Type");

var Code= responseCode.code;
var time=responseTime;

tests[(header+"\t\t"+header2+"\t\t"+Code+"\t\t"+time)]= true;

 

 

 

在postman中“===“三个等号代表全等

 

 获取body中的值

var body = pm.request.body.raw;//取请求body值
var test1 = JSON.parse(body);//解析body
var temp = test1.id;//取body中id的值
var data = pm.request.body.urlencoded;//取x-www-form-urlencoded中的值

 


var data = JSON.parse(responseBody);

tests["program's lenght"] = data.programs.length === 5;


16、https://www.bbsmax.com/A/o75N3P9PdW/

 

标签:body,tests,postman,json,模块,var,pm
From: https://www.cnblogs.com/jxba/p/16628376.html

相关文章