postman assertion of commonly used functions

1, set the global variables and environment variables

postman on the right function

Setting an environment variable:pm.environment.set("variable_key", "variable_value");
Set a global variable:pm.globals.set("variable_key", "variable_value");

 

jsonData the JSON.parse = var (the responseBody); // Get the return value json format 
pm.globals.set ( "totalCountBeforeMerged", jsonData.data.totalCountBeforeMerged); // the format of a field value of the json to the global variable 
pm. environment.set ( "totalPageNum", jsonData.data.totalPageNum); // will json format a field value to the environment variable

 

2, reference variable - bis {} {} for a reference variable

 

 

 

postman comes with three global variables Description:

$ guid:
generating a globally unique identification code at runtime. Can be used to generate a unique number will not be repeated.

$ randomInt:
generates a random integer 0-1000 at runtime.

$ timestamp:
generate a timestamp of the current time at runtime. The number of seconds January 1, 1970 00:00:00 began.

3、接口返回状态检查以及body里字符串检查

 

 

pm.test("返回200", function () {
    pm.response.to.have.status(200);
});
pm.test("返回值包含postman", function () {
    pm.expect(pm.response.text()).to.include("postman");
});

//源码
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("postman");
});

 

4、boss直聘搜索

https://www.zhipin.com/job_detail/?query=软件测试&city=101280600&industry&position

// header参数

host: www.zhipin.com
referer:http://www.zhipin.com/
cache-control: max-age=0
connection: keep-alive
Upgrade-Insecure-Requests: 1
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36\
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8

 

pm.globals.get("query");
pm.globals.get("scity");
pm.test("有这个公司的招聘记录", function () {
    pm.expect(pm.response.text()).to.include("软通动力");
});

 

5、响应时间校验

pm.test("有这个公司的招聘记录", function () {
    pm.expect(pm.response.text()).to.include("软通动力");
});

tests["有这个公司的招聘记录"]=responseBody.has("软通动力");


pm.test("响应时间小于 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

6、主要函数参数汇总

  获取json数据对象:var data =JSON.parse(responseBody);

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

  查看json的programs中有几组数据:data.programs.length=== 5

  获取响应返回值:varCode= responseCode.code

 获取相应时间:var time=responseTime

 判断响应数据中是否包含某个值:responseBody.has("true")

 检查点(断言):tests["Body matches string"]=responseBody.has("响应数据json的任意值 ");

 7、Json格式获取与断言

var result = JSON.parse(responseBody);
 if(result.retCode=='0'){
        tests["测试通过")"]=true;
    }else{
        tests["测试失败"]=false;
   }

 

Guess you like

Origin www.cnblogs.com/shishibuwan/p/11284855.html