Postman——pre-request产生随机字符串和随机数字传给request body
程序员文章站
2022-03-05 18:25:37
...
产生随机数字
function GetRandomNum(Min,Max)
{
var Range = Max - Min;
var Rand = Math.random();
return(Min + Math.round(Rand * Range));
}
//产生随机字符串
function randomString(length, chars) {
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
return result;
}
var num = GetRandomNum(3,12);
var roleId = GetRandomNum(1,2);
var rString = randomString(num, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
console.log("username is : "+rString);
console.log("role is : "+roleId);
//设置环境变量
pm.environment.set("UserName", rString);
pm.environment.set("PassWord", rString);
pm.environment.set("Role",roleId);
首先,设置将要引用的变量设置为环境变量
然后,在pre-request中对环境变量修改
最后,在request body中引用即可
下一篇: 使用postman 代码进行各种断言