Postman內(nèi)置自動生成的隨機數(shù)的參數(shù)
{{$guid}}:添加一個V4風(fēng)格GUID(如: aa002-44ac-45ca-aae3-52bf19650e2d)
{{$timestamp}}:將當前的時間戳,精確到秒
{{$randomInt}}:添加0和1000之間的隨機整數(shù)
Postman的pre-requestScript中协屡,自定義隨機數(shù)
//產(chǎn)生隨機數(shù)字
function GetRandomNum(Min,Max)
{
var Range = Max - Min;
var Rand = Math.random();
return(Min + Math.round(Rand * Range));
}
//產(chǎn)生隨機字符串
function randomString(length, chars) {
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
return result;
}
var tid = GetRandomNum(1111111111,99999999999);
var num = GetRandomNum(25,35);
var oaid = randomString(num, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
console.log("oaid is : "+oaid);
console.log("tid is : "+tid);
//設(shè)置環(huán)境變量
pm.environment.set("oaid", oaid);
pm.environment.set("tid", tid);
Postman獲取當前時間
var moment = require("moment"); // 獲取時間
var data = moment().format(" YYYY-MM-DD HH:mm:ss"); //定義時間格式
console.log(data);
pm.globals.set("TIME", data); //設(shè)置為全局變量
Postman獲取當前時間戳(毫秒)
//設(shè)置當前時間戳(毫秒)
Timestamp = Math.round(new Date().getTime());
postman.setGlobalVariable("Timestamp",Timestamp);
Postman簽名
//設(shè)置簽名秘鑰
key = "E84F708A9B8B42E6A08F9025CBBCC934";
//字符串進行md5加密
var token=pm.request.headers.get("Token")||"";
var body=pm.request.body.raw;
body = body.replace("{{Timestamp}}",Timestamp);
//計算簽名
var str = token+"&"+body+"&"+key;
postman.setGlobalVariable("str",str);
var strmd5= CryptoJS.MD5(str).toString(CryptoJS.enc.Hex).toUpperCase();
postman.setGlobalVariable("SignKey",strmd5);