jmeter接口测试-调用java的jar包-csv参数化请求-BeanShellPreProcessor生成验签作为请求验证参数-中文乱码----实战
Posted DaisyLinux
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jmeter接口测试-调用java的jar包-csv参数化请求-BeanShellPreProcessor生成验签作为请求验证参数-中文乱码----实战相关的知识,希望对你有一定的参考价值。
背景及思路:
需求:要做 创建新卡 接口的测试,要求:
1. 不需要每次手动修改请求参数。
方案:文中先用excle将数据准备好,导出为csv格式,再用jmeter的csv请求进行参数化
2. 卡号需要唯一;
方案:文中用jmeter的beanshell按时间戳加随机数生成
3. 请求参数中有一个参数,会根据相应的请求参数生成(文中的sign值),接口请求会验证sign是否和相应请求参数对应;
方案:
1. 文中将生成sign的源码打包放在jmeter的libext 下,
2. 再用jmeter的beanshell引用jar包,
3. 用同样的规则生成sign保证其一致性。
-----下面正式开始实战------:
-
将生成验签的java源码生成jar包,并将jar包放置在 libext目录下
1.1 将生成sign规则的java类 打jar包 包路径:com.util.lt
1.png
1.2 选择JAR file
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-ac8ac4591f8e139a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/514/format/webp)
1.3 放置在jmeter的 libext下面 取名为 AppLoginUtils
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-31dff054b7a637e3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/554/format/webp)
1.4 如果对路径不熟悉的话,可以将刚刚生成的jar包放在D盘, 解压,查看目录路径
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-3d5d728892cf8fff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/662/format/webp)
进 META-INF
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-6155c50c183ec7b1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/726/format/webp)
-----至此---java生成jar包并放在jmeter相应目录准备工作做好了.-----
-
参数化 excle 准备数据
2.1 excle 列出个参数及值
6.png
2.2 另存为csv格式
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-1e8a6102749ea4fd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
2.3 用文本编辑器(如:editplus)打开文本,第一行为参数名,下面为参数,剪切第一行数据为jmeter csv参数行,实际数据去掉第一行数据
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-1bce984ccbdb4f70.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
-------------至此----测试参数准备好了.----------------
-
jmeter 开始进行测试 添加线程组
9.png
3.1 jmeter设置csv参数 variableNames行数据为上面剪切的第一行数据
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-0d334e6b9b6f2c71.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/630/format/webp)
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-af6ee49d4a306b73.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
3.2 BeanShell PreProcessor 添加脚本生成需要的信息字段(生成cardNo为时间戳唯一数 为后续生成sign做准备)
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-d824f602e691ab3b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/549/format/webp)
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-8b8841329be2e5d0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
脚本如下:
<pre>
![技术图片](/img/jia.gif)
import java.sql.Date; import java.text.SimpleDateFormat; Long timeStamp = System.currentTimeMillis(); //获取当前时间戳 SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss"); //定义时间格式 String timeNow = sdf.format(new Date(timeStamp)); // 时间戳转换成定义好的时间格式 //(数据类型)(最小值+Math.random()(最大值-最小值+1)) //从1到9999的int型随机数 String cardNo = timeNow + (int)(1+Math.random()(9999-1+1)); //vars.get("name"); 从jmeter中获得变量值 //vars.put("key","value"); 数据存到jmeter变量中 vars.put("cardNo",cardNo); //将生成的当前时间加随机数生成的唯一字符串保存到jemter变量cardNo中 log.info("------------日志开始----------------"); log.info("cardNo is:"+cardNo); log.info("------------日志结束----------------");
</pre>
3.3 BeanShell PreProcessor 调用jar包的方法 生成sign值
同上添加 BeanShell PreProcessor 步骤,
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-372d36585d6835c5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/549/format/webp)
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-f88dca479d247935.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
脚本如下:
<pre>
![技术图片](/img/jia.gif)
1 import java.util.*; 2 import com.util.lt.AppLoginUtils; 3 4 public static getSign(String cardNo){ 5 Map paras = new HashMap(); 6 //取出刚刚csv参数化的数据,放到map中 7 paras.put("sex",vars.get("sex")); 8 paras.put("xxx", vars.get("xxx")); 9 paras.put("xxxx", vars.get("xxxx")); 10 paras.put("xxxxx", vars.get("xxxxx")); 11 paras.put("type",vars.get("type")); 12 paras.put("operator", vars.get("operator")); 13 paras.put("cardNo",cardNo); //传参 14 paras.put("codeName", vars.get("codeName")); 15 paras.put("operatorName", vars.get("operatorName")); 16 paras.put("xxxxxx", vars.get("xxxxxx")); 17 18 //调用 上面java打包的 生成验签码的方法 19 String sign = AppLoginUtils.getParametersToString(paras,"xxxxxxxxxxxxxxxxxxxxxxx"); 20 vars.put("sign",sign); //将生成的sign存到jmeter变量sign中,供后续请求用 21 return sign; 22 } 23 24 //vars.get("name"); 从jmeter中获得变量值 25 //vars.put("key","value"); 数据存到jmeter变量中 26 27 log.info("------------日志开始----------------"); 28 //下面供调试使用 使用上面 beanshell生成的 cardNo作为请求参数 以此保证使用的是一套数据 29 log.info("sign is:"+ getSign("${cardNo}")); 30 log.info("------------日志结束----------------");
</pre>
3.4 http请求 输入相应的地址 端口号 路径 编码格式
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-7e02ae3c1c19c273.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/617/format/webp)
3.4.1 parameters请求信息形式:
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-e22dd69b4378874e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
3.4.2 或者用 bodydata请求信息形式(与上面的请求形式二选一 其中一个可以右键--禁用):
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-9f093e6ac4abd930.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
3.5 添加 debug sample 进行测试调试用 (可不用添加)
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-438c2a3ad8c0a92d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/620/format/webp)
3.6 添加监控器 查看结果树 聚合报告:
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-c0162d0679d33a73.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/581/format/webp)
3.6.1监测的请求:
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-d191180721ef3629.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-12ea1898ae6fb374.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
3.6.2 监测的返回:
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-be1d15fa54642090.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
3.6.3 监测的 debug Sample:
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-aec48bd5ddf0c996.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/878/format/webp)
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-6d0d5bf1cf293964.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/998/format/webp)
-------至此----一个完整的调用和监控结束了.-------
接下来是 在做的过程中的一些注意点:
一. 怎么调试:
1.可以用beanShellSample 进行请求,debugSample 在查看结果树中 进行调试 。
2.也可以打印日志 log.info() 进行调试,需要先打开查看日志的选项:
![技术图片](http://upload-images.jianshu.io/upload_images/2191113-23bcb6c0da13d292.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/511/format/webp)
二. 编码问题:
jmeter乱码 修改jmeterinjmeter.properties文件:
-
把“jsyntaxtextarea.font.family=Hack”这行的“#”注释去掉 或者添加一行 用中文字体都行
25.png -
sampleresult.default.encoding="utf-8"
26.png -
参数化的话 文件另存为utf-8 的话 ,编码需要 utf-8,系统默认的话就不用
27.png
28.png -
http请求的 content encoding:utf-8
29.png -
生成加密的java源码的方法中指定请求编码类型: s.getBytes("utf-8")
30.png
作者:Alisa168
链接:https://www.jianshu.com/p/86569ae63d96
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
以上是关于jmeter接口测试-调用java的jar包-csv参数化请求-BeanShellPreProcessor生成验签作为请求验证参数-中文乱码----实战的主要内容,如果未能解决你的问题,请参考以下文章