postman之内建变量的基础应用
一、Postman有以下内建变量,适合一次性使用:
{{$guid}}//生成GUID
{{$timestamp}}//当前时间戳
{{$randomInt}}//0-1000的随机整数
简单应用举例:
二、内建变量pre-request Script 使用的应用举例
// 随机整数
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秒