postman+newman+jenkins实战

参考文档

https://www.getpostman.com/docs/v6/postman/scripts/test_examples
https://www.jianshu.com/p/87553ee60cb7

1、安装插件

   1、 google插件- 谷歌应用商店 chrome//app   Postman Interceptor
                 - postman插件版
   2、 桌面版 app     postman
   3、抓取请求时,打开 Interceptor开关,再打开postman插件上的开关 即可

chrome上面操作:

《postman+newman+jenkins实战》 image.png

postman插件上操作:

《postman+newman+jenkins实战》 image.png

2 、设置变量参数化

  • 读取外部文件进行参数化 csv

    《postman+newman+jenkins实战》 image.png

  • json 格式

    《postman+newman+jenkins实战》 image.png

pre- request Script 中设置bindMobilePhone  环境变量名
//pm.environment.set("bindMobilePhone", data["bindMobilePhone1"]); 获取json中字段值 给这个环境变量
//pm.environment.set("bindMobilePhone", data.bindMobilePhone1);
postman.setEnvironmentVariable("bindMobilePhone",data["bindMobilePhone1"]);
body中对json格式请求进行参数化
{"bindMobilePhone":"{{bindMobilePhone}}","sex":"1”}

//获取response里面的tid

//获取response的body中生成的订单号,如果不是数组的话可直接取

var onlineTradeNo = JSON.parse(responseBody);

//console.log("onlineTradeNo",onlineTradeNo.data[0].tid);

console.log("onlineTradeNo", onlineTradeNo.data.tid);

3、执行用例

运行-点击Runner,可以按整个Collections运行,也可以选择到文件夹运行

《postman+newman+jenkins实战》 image.png

4、newman 安装、执行

安装 neman: newman  -v 查看版本号 

 sudo npm install -g newman

 sudo   npm install  -g  newman-reporter-html    安装html形式报告

在控制台通过newman命令进行运行,需要导出两个json文件,一个是环境变量文件,另一个是coolection

《postman+newman+jenkins实战》 image.png
《postman+newman+jenkins实战》 image.png

newman run test2.postman_collection.json  --reporters cli,html --environment  test2.postman_environment.json  --reporter-html-export result.html

生成的报告文件:result.html

1、脚本执行: newman run 新零售代客下单场景.postman_collection.json --environment 线上环境.postman_environment.json
2、脚本执行,生成html报告  newman run 新零售代客下单场景.postman_collection.json --reporters cli,html --environment 线上环境.postman_environment.json --reporter-html-export result.html

5、集成到jenkins

  • 踩过的坑

之前一直可以用,今天登陆报错信息:jenkins java.net.BindException: Address already in use 一般这种是端口被占用

jenkins默认安装路径:/Applications/Jenkins/jenkins.war

通过控制台进入到/Applications/Jenkins 修改Jenkins端口java -jar jenkins.war –httpPort=8081

修改端口控制台运行出现以下信息:红色为初始化密码

《postman+newman+jenkins实战》 image.png

这串密码需要复制下来,因为http://localhost:8080 访问jenkins时候需要填写初始密码,一步步操作

  • jenkins中新建项目,通过shell脚本执行用例

(1)构建一个自由风格的软件项目

《postman+newman+jenkins实战》 image.png

(2)Build 中通过shell脚本去执行操作

《postman+newman+jenkins实战》 image.png

 命令: newman run /Users/chenxiaoqin/Downloads/新零售代客下单场景.postman_collection.json --reporters cli,html --environment /Users/chenxiaoqin/Downloads/线上环境.postman_environment.json --reporter-html-export /Users/chenxiaoqin/Downloads/result.html

(3)运行时产生的问题

env: node: No such file or directory
Build step ‘Execute shell’ marked build as failure
Finished: FAILURE
或者报/usr/local/bin/node的问题
尝试解决方法:
1、jenkins中系统管理-环境变量中设置

《postman+newman+jenkins实战》 image.png

2、.bash_profile export PATH=${PATH}:/node

3、安装到jenkins最新版本 下载最新jenkins.war 直接覆盖之前的版本 重启下

覆盖地址 jenkins-系统管理-系统信息中查看

《postman+newman+jenkins实战》 image.png

最后终于完美解决,生成的报告如下

《postman+newman+jenkins实战》 image.png

html格式报告如下:

《postman+newman+jenkins实战》 image.png

    原文作者:Jean_chen
    原文地址: https://www.jianshu.com/p/db150c07b4f1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞