美文网首页
Jmeter实现响应数据中指定内容的excel导出

Jmeter实现响应数据中指定内容的excel导出

作者: 莫依痕 | 来源:发表于2018-02-27 19:08 被阅读0次

1、需求:通过Jmeter获取下图数据的名称值

例子数据.png

2、实现:在Jmeter中创建线程、HTTP信息头管理器、具体请求和察看结果树

  • 输入获取首页API列表的请求(请求方式:get)


    请求API列表接口.png
  • 运行一遍,去查看响应数据的结果,需要获取数据中字段为name的所有value值


    查看结果树.png
  • 在“查看API列表”右键添加->后置处理器->BeanShell PostProcessor,输入以下内容
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//JMeter的内置API:prev.getResponseData()获取请求的响应内容
byte[] responseData = prev.getResponseData(); 
//定义一个String类型的变量 
String names = "";
//定义正则表达式需要匹配的模式
Pattern pattern = Pattern.compile("\"name\":\"(.+?)\"");
//Matcher java.util.regex.Pattern.matcher(CharSequence arg0)需要将字节类型转成String类型
//获取响应数据符合条件的结果
Matcher result = pattern.matcher(new String(responseData));  
//boolean java.util.regex.Matcher.find()只要找到符合条件的就返回true
while(result.find()){
//  String java.util.regex.Matcher.group(int group),group的参数是int类型,0表示获取正则模式左边数起第一个"("开始的内容,1表示获取正则模式左边数起第一个"("开始的内容
//  将返回的结果用字符串全部连接起来,"\r\n"换行保存
    names += result.group(1)+"\r\n";
}
//导出的excel存放位置
private String filePath = "E:/application20160314/apache-jmeter-3.2/exportResult/exportFile.xls";  
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
   File file = new File(filePath);
   fos = new FileOutputStream(file); //不添加参数true,以非追加的方式添加内容
   bos = new BufferedOutputStream(fos);
   bos.write(names.getBytes());
} catch (Exception e) {
   e.printStackTrace();
} finally {
   if (bos != null) {
      try {
         bos.close();
      } catch (IOException e1) {
         e1.printStackTrace();
      }
   }
   if (fos != null) {
      try {
         fos.close();
      } catch (IOException e1) {
         e1.printStackTrace();
      }
   }
}
导出数据代码实现.png

3、结果:去上述代码中设置好的具体路径查看结果,获取到所有需要的name值

Excel数据保存.png

相关文章

网友评论

      本文标题:Jmeter实现响应数据中指定内容的excel导出

      本文链接:https://www.haomeiwen.com/subject/qoimxftx.html