使用场景:
一个接口多次执行,要求body中的某个参数在每次运行时都要填写不同的值,根据不同值的传入,返回不同的结果
参考示例:通过接口测试创建5条待办名称不一样的待办事项。名称格式不作要求
解决方法:
解决的问题:实现添加5条待办名称不一样的待办。
第一步:rquest的 Pre-request Script中填写自动生成name名称的代码。
postman.setEnvironmentVariable("name", ((Math.random()*Math.pow(36,6) << 0).toString(36)).slice(-6)); //将自动生成的name添加到环境变量中
第二步、修改request中body里面填写的参数name
第三步、保存运行,即可成功。
补充(后面有需求再来补充):
/* //随机生成一个手机号码,传入body const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min; // 随机整数 postman.setGlobalVariable("mobile",`18${randomInt(100000000, 999999999)}`); //生成6位数字+字母的名称 postman.setEnvironmentVariable("name", ((Math.random()*Math.pow(36,6) << 0).toString(36)).slice(-6)); //生成4位数字+字母的名称 postman.setEnvironmentVariable("name", ("000000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4)); //获取当前的时间戳并设置到body中,格式:2018-4-16 00:00:00 var aa = Math.round(new Date() / 1000) console.log(aa) postman.setEnvironmentVariable("finished_at",aa) */