Postman内建变量常用方法实例解析

网络编程 2021-07-04 14:06www.168986.cn编程入门
这篇文章主要介绍了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秒

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by