Postman使用手册
Postman interceptor
作用
记录浏览器请求并直接导入到postman,可添加filter,对浏览器中的请求进行过滤
安装
- chrome上下载插件postman interceptor
使用
- chrome打开Postman interceptor按钮
- postman工具中也打开Interceptor按钮
- 请求的内容将自动同步到postman的history中
- Filter requests默认的是".*",表示捕获所有的网站。如果只想捕获雅虎,可以在此处设置“yahoo”。
- 打开postman interceptor可以看到‘last 10 requests’。
- chrome中打开yahoo网页,输入任何内容后确定,查看postman history结果即可看到请求内容。
- 然后就可以将history的request进行修改,过滤,以及编写测试了。
切换环境和常用Scripts
环境变量
- 请求发送之前创建的情况:在Pre-request Script标签里面添加如下代码:
postman.setEnvironmentVariable("key","value"); - 请求发送之后创建的情况:在Tests标签里面添加如上代码:
全局变量
- 请求发送之前创建的情况:在Pre-request Script标签里面添加如下代码:
postman.setGlobalVariable("key","value"); - 请求发送之后创建的情况:在Tests标签里面添加如上代码:
*变量的引用:在需要的地方加上{{变量名}}即可
脚本
可使用Tests标签下右侧的SINPPETS中的命令实现。
Pre-request Script:
Tests:
参数化和参数传递
业务背景
一个api返回body中的某个值需要作为下一个api的参数来调用。而且每次body中返回的这个参数都是变化的。
实施步骤
- 需要参数传递的是getitems api中返回的body中的acknowledgementUri值作为sendAcknowledgemen api中的参数来调用。
-
getitems api执行后在Tests里编写脚本
Test scripts
//status code check
tests["Status code is 200"] = responseCode.code === 200;
//获取body中返回的所有参数
var jsonData = JSON.parse(responseBody);
//把返回参数中的keys设置为环境变量
postman.setEnvironmentVariable("acknowledgementUri",jsonData.receipts[0].acknowledgementUri);
- postman的设定中定义环境变量,对定义的变量类型和值无特殊要求。如key,values就行。当第二个api执行后,设定的environment中会自动生成相应的值进行传递。
- Step3_sendAcknowledgemen接口3中使用该参数{{acknowledgementUri}}
实现过程:Step1_exchange API来用劵交换item,Step2_getitems来查看相应item的获取情况,Step3_sendAcknowledgement里取出此item
3个apicollection中导出的数据test_seriese.postman_collection.json
坑坑
坑1:
返回参数中的key作为环境变量取值时,要根据返回的json数据格式正确写值,如Step2_getitems中返回的值在json中是数组类型,
此时如果写作jsonData.receipts.acknowledgementUri是无法通过的,一定要根据相应的json数据格式来取值。
坑2:
虽然在第二个api的Tests中完成了环境变量设置,but一定要在postman的设定>Manege Environment中设置环境参数,
并在第三个api执行之前选择上述设定的环境再执行api,否则会报错(Could not get any response)。
Postman Newman
作用
批量执行api脚本文件,同时可以与jenkins环境结合使用
安装
- 安装nodejs,并配置好环境
- cmd,运行npm install -g newman
- cmd, 运行newman -V 校验安装是否成功(本机安装的版本号为3.9.3)
使用
需要进一步研究!!!!!!!
http://www.cnblogs.com/miniren/p/5607861.html
Postman+Newman+Jenkins API自动化测试
需要进一步研究!!!!!!!
https://www.2cto.com/kf/201607/529387.html
常用的基础操作
控制台的使用
- 下载postman客户端版本(chorme插件版无法实现)
- View>Show DevTools(开启和chrome开发者控制台类似的开发者模式)
-
脚本中使用console.log(),相应的log即可在console输出
Runner使用
一般使用:collection设置保存>Tests中设置好断言的方法并保存>Runner,选择刚刚的collection>输入迭代次数会生成测试报告
复杂使用:每次迭代赋值的变量都不同,需要用到csv或者json文件
- 设置好断言,定义相应变量(当然了不要忘记环境变量),如:acknowledgementUri,如果每次迭代中给这个变量赋值,就需要使用到csv或者json文件。
注意:json的key一定要与变量名一致 -
按照规则写好json文件jsontest.json
[{"acknowledgementUri":"xxxxxx?xxx=xxxxx&xxxx=xxxx"},{"acknowledgementUri":"xxxx&xxxxx"}] -
在runner中选择好该json文件,设置迭代次数,可以预览效果查看格式是否正确
- 点击start run,即可查看测试结果
浙公网安备 33010602011771号