1. 程式人生 > 程式設計 >Postman內建變數常用方法例項解析

Postman內建變數常用方法例項解析

一、Postman有以下內建變數,適合一次性使用:

{{$guid}}//生成GUID
{{$timestamp}}//當前時間戳
{{$randomInt}}//0-1000的隨機整數

二、內建變數的應用舉例,獲取當前時間戳

Postman內建變數常用方法例項解析

Postman內建變數常用方法例項解析

Postman內建變數常用方法例項解析

// 隨機整數
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秒

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。