如何使用Postman内建变量
更新:HHH   时间:2023-1-7


小编这次要给大家分享的是如何使用Postman内建变量,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

一、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秒

看完这篇关于如何使用Postman内建变量的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。

返回开发技术教程...