1. 生成一个MD5或SHA1加密的字符串str_md5,str_sha1
string1 = "123456";
var str_md5= CryptoJS.MD5(string1).toString(); //MD5 加密
var str_sha1= CryptoJS.SHA1(string1).toString(); //SHA1 加密
pm.globals.set("str_md5", str_md5);
2. 生成一个 yyyyMMddHHmm 格式的字符串date_str
var myDate = new Date(); //获取当前时间
var Y = myDate.getFullYear(); // 获取完整的年份(4位)
var M = myDate.getMonth() + 1; // 获取当前月份(0-11,0代表1月)
M = M < 10 ? ('0' + M) : M;
var D = myDate.getDate(); // 获取当前日(1-31)
D = D < 10 ? ('0' + D) : D;
var h = myDate.getHours(); // 获取当前小时数(0-23)
h=h < 10 ? ('0' + h) : h;
var m = myDate.getMinutes(); // 获取当前分钟数(0-59)
m = m < 10 ? ('0' + m) : m;
var date_str = Y + M + D + h + m;
pm.globals.set("date_str ", date_str );
另外一种写法是封装了函数,可以直接调用,例如 timestamp :
function dateFormat(fmt) {
var nowDate = new Date();
var o = {
"M+" : nowDate.getMonth()+1, //月份
"d+" : nowDate.getDate(), //日
"h+" : nowDate.getHours(), //小时
"m+" : nowDate.getMinutes(), //分
"s+" : nowDate.getSeconds(), //秒
"q+" : Math.floor((nowDate.getMonth()+3)/3), //季度
"S" : nowDate.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt)) {
fmt=fmt.replace(RegExp.$1, (nowDate.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
}
var timestamp = dateFormat('yyyyMMddhhmmss');
pm.environment.set("timestamp",timestamp);
3. 生成自增的编号Num
var Num = pm.environment.get("Num");
Num_new = Number(Num)+Number(1);
pm.environment.set("Num", Num_new);
4. 生成随机数值
//使用 Random 函数获取随机数
const RandomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min; //取某个数值范围内的随机整数
const getRandomValue = list => list[RandomInt(0, list.length - 1)]; //取某个数组内的随机数值
const random_wishies= ["山河无恙","人月两圆","国泰民安","万事如意","贪吃不胖"];
pm.environment.set("random_wishies",getRandomValue(random_wishies));
5. 获取在某个时间范围内的yyyyMMddHHmm格式的随机时间get_rzsj
StartDate = new Date(2018,0,1);
EndDate = new Date(2018,11,31);
function getRandomDateBetween(StartDate, EndDate) // 获取指定日期范围内的日期;
{
var Radomdate = new Date(1958,0,1);
x = StartDate.getTime();
y = EndDate.getTime();
Radomdate.setTime(Math.random()*(y - x + 1) + x);
return Radomdate;
}
Radomdate = getRandomDateBetween(StartDate,EndDate);
var BY = Radomdate.getFullYear(); // 获取完整的年份(4位)
var BM = Radomdate.getMonth() + 1; // 获取当前月份(0-11,0代表1月)
BM = BM < 10 ? ('0' + BM) : BM;
var BD = Radomdate.getDate(); // 获取当前日(1-31)
BD = BD < 10 ? ('0' + BD) : BD;
var Radomdate_str = BY+ BM + BD ;
pm.globals.set("sjsj", Radomdate_str);
6. 生成随机男女生姓名Name
const FirstName = ['艾','倪','薛','屈','励','池'];
const BoyNames = ['伟','刚','勇','彬','富','顺','信','弘'];
const GirlNames = ['璐','凝','晓','欢','霄','枫','青','婷'];
//使用 getNum 函数获取随机数
const RandomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min; //取某个数值范围内的随机整数
const getRandomValue = list => list[RandomInt(0, list.length - 1)]; //取某个数组内的随机数值
let EmployeeName = ''; //初始化
let name_sex = ''; //初始化
//判断性别
sex = RandomInt(0,1);
str = BoyNames;//默认使用 BoyNames
if (sex === 0) //如果性别取0时,使用 GirlNames
{
str = GirlNames;
name_sex = "2"; //2代表女
}
else
{
name_sex = "1"; //1代表男
}
pm.globals.set("Employee_sex", name_sex);//获取性别
first = FirstName[Math.floor(Math.random()*FirstName.length-1)]; //随机取一个姓氏
second = str[Math.floor(Math.random()*(str.length-1))]; //随机取str的一个字
hasThird = RandomInt(0, 1); //使用RandomInt函数判断是否取名字的第二个字
third = "";
if (hasThird == 1)
{
third = str[Math.floor(Math.random()*(str.length-1))];
}
var ChineseName = first + second + third;
pm.globals.set("Name",ChineseName);
7. 生成随机身份证号码 randomIDcardno
const odd = ['1','3','5','7','9']; //奇数数组
const even = ['0','2','4','6','8']; //偶数数组
if (name_sex == '2')
{
var num = getRandomValue(even); //如果性别为女,顺序码第三位取偶数,这里getRandomValue 函数需要引用第5条或第6条的随机数函数,下同
}
else
{
num = getRandomValue(odd); //如果性别为男,顺序码第三位取奇数,引用第5条或第6条的随机数函数
}
function getIDcardno()
{
const address = ['110000','450501','361100','511323','410701','654322']; //所有区域代码,这里省略了大部分,需要的可以自己去查下加入
var areaCode = getRandomValue(address); //随机获取身份证号码前6位
var birthday = Radomdate_str; // 获取6位出生日期 var sequencecode = Math.floor(Math.random()*10).toString() + Math.floor(Math.random()*10).toString() + num; //生成3位出生顺序码,num 分奇偶数
var array = (areaCode + birthday + sequencecode).split(""); //split方法分隔17位数字 var coefficientArray = [ '7','9','10','5','8','4','2','1','6','3','7','9','10','5','8','4','2']; //加权因子
var lastNumberArray = [ "1","0","X","9","8","7","6","5","4","3","2"]; //校验码 var sum = 0;
for (var i = 0; i < array.length; i++)
{
sum = sum + parseInt(array[i])*parseInt(coefficientArray[i]); //权重求和
}
var lastNumber = lastNumberArray[parseInt(sum%11)]; //
var IDcardno_String = areaCode + birthday + sequencecode + lastNumber; //
return IDcardno_String;
}
randomIDcardno = getIDcardno();//获取函数返回值
pm.globals.set("randomIDcardno", randomIDcardno); //将函数返回值定义给全局变量randomIDcardno
8. 生成随机手机号码 phoneNum
function randomPhoneNum(){
var Num = '189';
for(var i=0; i < 8; i++){
Num += Math.floor(Math.random() * 10);
}
return Num;
}
var str_phone = randomPhoneNum();
pm.globals.set("phoneNum", str_phone);
9. 生成一个4位字母数字混合的字符串
pm.globals.set("random_code", ("0000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4));
10. 生成当前的时间戳
pm.globals.set("unixtime_now", Math.round(new Date().getTime()/1000));