一、Postman有以下内建变量,适合一次性使用:
{{$guid}}//生成GUID
{{$timestamp}}//当前时间戳
{{$randomInt}}//0-1000的随机整数
二、内建变量的应用举例,获取当前时间戳
// 随机整数
const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
pm.globals.set("random_number", randomInt(300,1000));
//从多个选项中选择实现:
const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
const getRandomValue = list => list[randomInt(0, list.length - 1)];
const charsInName = ['王','李','张'];
pm.globals.set("people_name", getRandomValue(charsInName));
//随机生成手机号
const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
var mobile_num= `18${randomInt(100000000, 999999999)}`;
pm.globals.set("mobile_num", mobile_num);
//等待
const sleep = (milliseconds) => {
const start = Date.now();
while (Date.now() <= start + milliseconds) {}
};
sleep(5000) //5秒