【原】postman常用设置全局变量的js片段

postman知识总结:

API自动化利器:http://www.bayescafe.com/tools/use-postman-to-test-api-automatically.html

 

1.获取环境变量内容

var ostype = pm.environment.get("ostype");

 

2.设置全局变量内容

postman.setEnvironmentVariable("ts",Math.floor(new Date().getTime()/1000));

 

3.auth签名

var auth = CryptoJS.SHA1(pm.environment.get("device_secret"),{asString: true});
postman.setEnvironmentVariable("auth", auth);

 

4.随机标识

const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;  // 随机整数
const getRandomValue = list => list[randomInt(0, list.length - 1)];  // 随机选项
const chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'k', 'l', 'm', 'n', 'o', '1'];
let identifier = '';
for (let i = 0; i < 5; i++) {
  identifier += getRandomValue(chars);
}
pm.environment.set("identifier", identifier);

 

5.schema校验

let json;
try {
  json = JSON.parse(responseBody);
} catch(err) {
  tests['服务端没返回合法的JSON格式,请检查相关服务、网络或反向代理设置(以下跳过其他断言)'] = false;
  tests[`[INFO] 返回:${responseBody}`] = true;
  console.error(err);
}
if (json) {
  const result = tv4.validateResult(json, schema);
  console.log(result);
  tests['JSON Schema格式正确 ' + result.error ] = result.valid;
  } else {
    console.error(result.error);
    console.error(responseBody);
}

 

转载于:https://www.cnblogs.com/HYanqing/p/11271848.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值