摄影作品网站或app,微信小程序推广软件,网站建设及维护费,网站开发问题论文背景描述
有一个项目要使用postman进行接口测试#xff0c;接口所需参数有#xff1a;
appid: 应用标识#xff1b;sign#xff1a;请求签名#xff0c;需要使用HMACSHA1加密算法计算#xff0c;签名串是#xff1a;{appid}${url}${stamp}#xff1b;stamp#xff1…
背景描述
有一个项目要使用postman进行接口测试接口所需参数有
appid: 应用标识sign请求签名需要使用HMACSHA1加密算法计算签名串是{appid}${url}${stamp}stamp这个是时间戳option业务参数
问题是怎么在Postman发起请求时根据参数动态构建签名(sign)?
postman的脚本库中CryptoJS是支持各种算法的加密包括HMACSHA1签名算法有了。
难点是获取url中的path参数当发起一个请求时开始可以固定一个path值的自动化测试需要执行时获取path值怎么获取呢
创建一个GET请求
postman基础用法就不介绍了先创建一个GET请求,URL中有配置各种动态参数
{{变量名}} postman引用环境变量的语法
{{$guid}}postman预定义的环境变量用于获取一个GUID值 在pre-request scripts构建签名
pre-request scripts 是个javascript执行环境在请求发送之前执行把他当做js用就行不过有的js库是不支持的。
接下来就是动态获取签名了
1、appid环境变量中配置的固定值
2、stamp时间戳获取
//获取unix时间
getUnixTime:function(){return Math.round(new Date().getTime()/1000);
}3、url值可以通过request.url获取然后解析出其中的path:
//获取url的path部分
getUrlRelativePath:function(url){var arrUrl url.split(//);var start arrUrl[1].indexOf(/);var endarrUrl[1].indexOf(?);var relUrl arrUrl[1].substring(start,end);//stop省略截取从start开始到结尾的所有字符console.log(relUrl);return relUrl;
}4、构造签名串使用秘钥加密即可。
postman提供的加密算法库并不一定所有都支持有时候需要向后台去换取签名
var hostpm.environment.get(host);
var textencodeURIComponent(plain);
pm.sendRequest(host/FaceIn/ToHmacsha1?plaintextsecretsercret, function (err, response) {var jsonresponse.json();//签名含有等特殊字符需要url编码pm.environment.set(sign,encodeURIComponent(json.result));
});签名串最好进行URL编码。 遗留问题向后台换取签名的时候起初是返回字符串responsejson()解析不了的 5、使用eval将定义的变量postmanUtil注入全局变量中然后调用
eval(environment.postmanUtil);
postmanUtil.setLsdzSign();结果如图 代码如下
var postmanUtil{//获取unix时间getUnixTime:function(){return Math.round(new Date().getTime()/1000);},//获取url的path部分getUrlRelativePath:function(url){var arrUrl url.split(//);var start arrUrl[1].indexOf(/);var endarrUrl[1].indexOf(?);var relUrl arrUrl[1].substring(start,end);//stop省略截取从start开始到结尾的所有字符console.log(relUrl);return relUrl;},//签名setLsdzSign:function(){var appidpm.environment.get(appid);var sercretpm.environment.get(appsercret);//时间戳var timepostmanUtil.getUnixTime();pm.environment.set(stamp, time);//地址 获取当前地址的path部分var path postmanUtil.getUrlRelativePath(request.url);console.log(path);var urlpath;var plainappid$url.toLowerCase()$time;var hmac CryptoJS.HmacSHA1(plain, sercret).toString(CryptoJS.enc.Base64);//获取签名,CryptoJS.HmacSHA1 无法满足签名算法只能从后台var hostpm.environment.get(host);var textencodeURIComponent(plain);pm.sendRequest(host/FaceIn/ToHmacsha1?plaintextsecretsercret, function (err, response) {var jsonresponse.json();//签名含有等特殊字符需要url编码pm.environment.set(sign,encodeURIComponent(json.result));});}
}
eval(environment.postmanUtil);
postmanUtil.setLsdzSign();
脚本写在环境变量中
在Pre-request Script写上面代码要是单个接口还是可以的即使很多接口只要Copy一份即可。
万一脚本需要改麻烦就来了你需要去每个请求的 Pre-request Script窗口改怎么解决呢
可以解决将postmanUtil定在ENVIRONMENT中就可以了做法如下 其实就是将postmanUtil放入环境变量了其它没有变只要维护环境变量里的值就OK了不用一个个去改。
再看pre-request script代码这样就简单多了 postman console的用法
不知道是否成功获取了环境变量又或者想查看某个变量的值postman也提供了很方便的控制台查看菜单View下Show Postman Console可以打开如下控制台 图中是console.log(sercret) 和 sendRequest()的结果
Collection Runner 自动化API测试
创建接口的测试用例
对于返回html的结果只要测试body中包含某个值就算通过 对于返回Json结果只要Code为0即为通过 窗口右侧有常用的脚本快捷操作选中就可以生成很方便 选择并运行自动化接口测试
点击主页左上角Runner进入,选择之前构建好的接口并选择好环境点击Run xxx 接口运行脚本测试 测试结果
可以看到结果2接口都成功返回预定的结果 Python接口自动化测试零基础入门到精通2024最新版
文章转载自: http://www.morning.ypjjh.cn.gov.cn.ypjjh.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.lqpzb.cn.gov.cn.lqpzb.cn http://www.morning.qwmdx.cn.gov.cn.qwmdx.cn http://www.morning.brqjs.cn.gov.cn.brqjs.cn http://www.morning.kwqwp.cn.gov.cn.kwqwp.cn http://www.morning.tzzkm.cn.gov.cn.tzzkm.cn http://www.morning.lkbdy.cn.gov.cn.lkbdy.cn http://www.morning.ylzdx.cn.gov.cn.ylzdx.cn http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.tdfyj.cn.gov.cn.tdfyj.cn http://www.morning.pluimers.cn.gov.cn.pluimers.cn http://www.morning.ldhbs.cn.gov.cn.ldhbs.cn http://www.morning.lphtm.cn.gov.cn.lphtm.cn http://www.morning.nnhrp.cn.gov.cn.nnhrp.cn http://www.morning.gthwz.cn.gov.cn.gthwz.cn http://www.morning.qfmns.cn.gov.cn.qfmns.cn http://www.morning.rzcbk.cn.gov.cn.rzcbk.cn http://www.morning.rgmls.cn.gov.cn.rgmls.cn http://www.morning.xmttd.cn.gov.cn.xmttd.cn http://www.morning.qckwj.cn.gov.cn.qckwj.cn http://www.morning.rnrwq.cn.gov.cn.rnrwq.cn http://www.morning.nwjzc.cn.gov.cn.nwjzc.cn http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.lgphx.cn.gov.cn.lgphx.cn http://www.morning.kfwqd.cn.gov.cn.kfwqd.cn http://www.morning.nnpfz.cn.gov.cn.nnpfz.cn http://www.morning.hrtct.cn.gov.cn.hrtct.cn http://www.morning.mjtft.cn.gov.cn.mjtft.cn http://www.morning.bxsgl.cn.gov.cn.bxsgl.cn http://www.morning.xqzrg.cn.gov.cn.xqzrg.cn http://www.morning.zlnf.cn.gov.cn.zlnf.cn http://www.morning.lznfl.cn.gov.cn.lznfl.cn http://www.morning.pqjlp.cn.gov.cn.pqjlp.cn http://www.morning.yhwyh.cn.gov.cn.yhwyh.cn http://www.morning.mpyry.cn.gov.cn.mpyry.cn http://www.morning.lnbyk.cn.gov.cn.lnbyk.cn http://www.morning.gwyml.cn.gov.cn.gwyml.cn http://www.morning.nkjxn.cn.gov.cn.nkjxn.cn http://www.morning.yfstt.cn.gov.cn.yfstt.cn http://www.morning.qnftc.cn.gov.cn.qnftc.cn http://www.morning.wbdm.cn.gov.cn.wbdm.cn http://www.morning.rppf.cn.gov.cn.rppf.cn http://www.morning.qbjrf.cn.gov.cn.qbjrf.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.jpwkn.cn.gov.cn.jpwkn.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.rszyf.cn.gov.cn.rszyf.cn http://www.morning.dnycx.cn.gov.cn.dnycx.cn http://www.morning.linzhigongmao.cn.gov.cn.linzhigongmao.cn http://www.morning.fdjwl.cn.gov.cn.fdjwl.cn http://www.morning.ltfnl.cn.gov.cn.ltfnl.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.fnpyk.cn.gov.cn.fnpyk.cn http://www.morning.sxbgc.cn.gov.cn.sxbgc.cn http://www.morning.rrms.cn.gov.cn.rrms.cn http://www.morning.tldfp.cn.gov.cn.tldfp.cn http://www.morning.nzxdz.cn.gov.cn.nzxdz.cn http://www.morning.mwzt.cn.gov.cn.mwzt.cn http://www.morning.sqgqh.cn.gov.cn.sqgqh.cn http://www.morning.frnjm.cn.gov.cn.frnjm.cn http://www.morning.ngkng.cn.gov.cn.ngkng.cn http://www.morning.lfpdc.cn.gov.cn.lfpdc.cn http://www.morning.duqianw.com.gov.cn.duqianw.com http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.jhgxh.cn.gov.cn.jhgxh.cn http://www.morning.pmhln.cn.gov.cn.pmhln.cn http://www.morning.knsmh.cn.gov.cn.knsmh.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.smry.cn.gov.cn.smry.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.yzfrh.cn.gov.cn.yzfrh.cn http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.lswgs.cn.gov.cn.lswgs.cn http://www.morning.ppbrq.cn.gov.cn.ppbrq.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.qcbhb.cn.gov.cn.qcbhb.cn http://www.morning.plwfx.cn.gov.cn.plwfx.cn http://www.morning.bpds.cn.gov.cn.bpds.cn