http://www.cnblogs.com/zanjiahaoge666/p/6388434.html
postman常用js生成变量
pre-request script
- 生成一个字符串作为随机用户名:
postman.setEnvironmentVariable("random_username", ("0000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4)); - 在发起请求之前获取当前的时间戳放在参数里:
postman.setEnvironmentVariable("unixtime_now", Math.round(new Date().getTime()/1000)); - 生产随机数(0,1)postman.setEnvironmentVariable("random_cardno", Math.round(Math.random()*9999999));
tests
- 验证返回相应中的字段值:
var jsonData = JSON.parse(responseBody);
tests["Check respose value err_code"] = jsonData.err_code === 0;
- 响应时间小于500ms
tests['Response time is less than 500ms'] = responseTime < 500;
3.验证响应报文的JSON格式:
//最简单的格式验证
var schema = {
properties: {
err_code: {type: 'int'},
err_msg: {type: 'string'}
}
};
tests["Valid data schema"] = tv4.validate(responseBody, schema);
- 验证相应报文中是否包含某字段:
tests["body has status"] = jsonData.status = true;
- 先判断某字段是否存在,若存在则设置环境变量,若不存在则不设置:
if (jsonData.status) {
tests["body has status"] = true;
postman.setEnvironmentVariable("status", jsonData.status);
}
else {
tests["body has status"] = false;
}
var jsonData = JSON.parse(responseBody);
var jsonpo=JSON.stringify(jsonData["data"]);
console.log(jsonpo);
srcpo=jsonData["srcFileName"];
console.log(srcpo);
newpo=jsonData["newFileName"];
console.log(newpo);
typepo=jsonData["type"];
console.log(typepo);
// console.log(jsonData["data"][0].GlobalBusinessUnit);
postman.setEnvironmentVariable('jsonpo',jsonpo);
postman.setEnvironmentVariable('newpo',newpo);
postman.setEnvironmentVariable('srcpo',srcpo);
postman.setEnvironmentVariable('typepo',typepo);
var jsonPO=jsonData["data"][0];
var GlobalBusinessUnit=jsonPO['GlobalBusinessUnit'];
var OrderNo=jsonPO['OrderNo'];
var Department=jsonPO['Department'];
var Year=jsonPO['Year'];
var PlanningSsn=jsonPO['PlanningSsn'];
var VendorCode=jsonPO['VendorCode'];
var Vendor=jsonPO['Vendor'];
var ManagementFactoryCode=jsonPO['ManagementFactoryCode'];
var ManagementFactory=jsonPO['ManagementFactory'];
var BranchFactoryCode=jsonPO['BranchFactoryCode'];
var BranchFactory=jsonPO['BranchFactory'];
var TotalNumberLines=jsonData["data"].length;
// var TotalQty=jsonData["data"][i]['OrderQty'];
var TotalQty = 0;
jsonData['data'].map(x => TotalQty += x['OrderQty']);
// var TotalQty1=TotalQty*range
// console.log(TotalQty1);
postman.setEnvironmentVariable('GlobalBusinessUnit',GlobalBusinessUnit);
http://www.jianshu.com/p/dd0db1b13cfc
1. 清除一个全局变量
Clear a global variable
对应脚本:
postman.clearGlobalVariable("variable_key");
参数:需要清除的变量的key
2.清除一个环境变量
Clear an environment variable
对应脚本:
postman.clearEnvironmentVariable("variable_key");
参数:需要清除的环境变量的key
3.response包含内容
Response body:Contains string
对应脚本:
tests["Body matches string"] =responseBody.has("string_you_want_to_search");
参数:预期内容
4.将xml格式的response转换成son格式
Response body:Convert XML body to a JSON Object
对应脚本:
var jsonObject = xml2Json(responseBody);
参数:(默认不需要设置参数,为接口的response)需要转换的xml
5.response等于预期内容
Response body:Is equal to a string
对应脚本:
tests["Body is correct"] = responseBody === "response_body_string";
参数:预期response
6.json解析key的值进行校验
Response body:JSON value check
对应脚本:
tests["Args key contains argument passed as url parameter"] = 'test' in responseJSON.args
参数:test替换被测的值,args替换被测的key
7.检查response的header信息是否有被测字段
Response headers:Content-Type header check
对应脚本:
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
参数:预期header
8.响应时间判断
Response time is less than 200ms
对应脚本:
tests["Response time is less than 200ms"] = responseTime < 200;
参数:响应时间
9.设置全局变量
Set an global variable
对应脚本:
postman.setGlobalVariable("variable_key", "variable_value");
参数:全局变量的键值
10.设置环境变量
Set an environment variable
对应脚本:
postman.setEnvironmentVariable("variable_key", "variable_value");
参数:环境变量的键值
11.判断状态码
Status code:Code is 200
对应脚本:
tests["Status code is 200"] = responseCode.code != 400;
参数:状态码
12.检查code name 是否包含内容
Status code:Code name has string
对应脚本:
tests["Status code name has string"] = responseCode.name.has("Created");
参数:预期code name包含字符串
13.成功的post请求
Status code:Successful POST request
对应脚本:
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
14.微小验证器
Use Tiny Validator for JSON data
对应脚本:
var schema = {
"items": {
"type": "boolean"
}
};
var data1 = [true, false];
var data2 = [true, 123];
console.log(tv4.error);
tests["Valid Data1"] = tv4.validate(data1, schema);
tests["Valid Data2"] = tv4.validate(data2, schema);
参数:可以修改items里面的键值对来对应验证json的参数