Pre-request Script

Faith ·
更新时间:2024-11-10
· 850 次阅读

// 项目名称 function getGuid () {
  var d = new Date().getTime()
  var guid = 'xxxx-xxxx'.replace(/[xy]/g, function (c) {
    var r = (d + Math.random() * 16) % 16 | 0
    d = Math.floor(d / 16)
    return (c === 'x' ? r : (r & 0x7) | 0x8).toString(16)
  })
  return guid
}
let entryName = "项目名"+ getGuid()
pm.environment.set("entryName", entryName); // 主账号名称 postman.setEnvironmentVariable("mainName", ("0000" + (Math.random()*Math.pow(36,6) << 0).toString(36)).slice(-6));
// 真实姓名
function namea() {
    var xing = ["赵","钱","孙","李","周","吴","郑","王","冯","陈","褚","卫","蒋","沈","韩","杨","朱","秦","尤","许","何","吕","施","张","孔","曹","严","华","金","魏","陶","姜"];
    var ming = ["天","地","玄","黄","宇","宙","洪","荒","日","月","盈","昃","辰","宿","列","张","寒","来","暑","往","秋","收","冬","藏","闰","余","成","岁","律","吕","调","阳"];
    var real_name = xing[Math.floor(Math.random() * (xing.length))] + ming[Math.floor(Math.random() * (ming.length))];
    return real_name;
}
let realName = namea()
pm.environment.set("realName", realName);
// 手机号 function getMoblieNum(){
    var numArray = ["139","138","137","136","135","134","159","158","157","150","151","152","188","187","182","183","184","178","130","131","132","156","155","186","185","176","133","153","189","180","181","177"];   //这是目前找到的除了数据卡外的手机卡前三位,类型是字符串数组
    var numList = "";    //创建一个数组用来存放10个手机号
    var arraryLength = numArray.length;   //获取数组长度,这样如果手机号前三位取值单位发生变化,在下一步求i的地方就不用修改随机数取值范围了
    var i = parseInt( Math.random() * arraryLength);  //注意乘以的是上面numArray数组的长度,这样就可以取出数组中的随机一个数。random的取值范围是大于等于0.0,小于1.0,相乘后得到的就是0到(数组长度-1)的值。
    var num = numArray[i];   //取出随机的手机号前三位并赋值给num,手机号前三位是字符串类型的
    for ( var j = 0; j < 8; j++){
        num = num + Math.floor(Math.random() * 10);     //num是字符串,后面的数字被当做字符串。所以变成两个字符串拼接了
    }
    return num;
}
let phone = getMoblieNum()
pm.environment.set("phone", phone);
// 职位 function getPosition(){
    var zhi=["职位1","职位2","职位3","职位4","职位5"];
    var position = zhi[Math.floor(Math.random() * (zhi.length))];
    return position;
}
let position = getPosition()
pm.environment.set("position", position); // 随机字符 postman.setGlobalVariable("search", ("0" + (Math.random()*Math.pow(36,1) << 0).toString(36)).slice(-1)); // 密码 pm.environment.set("password", "123456");
作者:~o( =∩ω∩= )m



pre script request

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章