Amazing Arch

jmeter内对response值的处理

2017-08-21  本文已影响1117人  Null_ice

jmeter内对response值的处理

1. 正则表达式提取器

在取样器上右键,选择正则表达式提取器,如图:

image

接下来看一下正则表达式提取器的界面:

image

在这里对页面各字段进行一下说明,从上往下:

1. Apply to,是指你提取的变量要应用的范围,需要注意的是,Jmeter Variable是指应用到全局,也就是跨线程的。

2. 要检查的响应字段,是指你的正则表达式的提取范围,主体应该是整个response,单选body就是指在body里找,其它选项根据字面意思,就不再赘述。

3. 引用名称,是我们自定义的一个变量名称,也就是提取出来的值的变量名称,例如我给它定义为“var”,那么正则提取出来的值就存在var里,之后如果要用,就使用el表达式,也就是${var(刚刚定义的名称)}的方式来使用。

4. 正则表达式,这里写提取变量用的正则表达式

5. 模板,这里是指你提取多少个变量(如果在正则表达式中有多个提取表达式),一般我们提取一个就够了,模板采用$N$的方式,N代表数量,例如,我们设立一个只提取1个值的模板,就写作$1$。

6. 匹配数字,也就是提取匹配到的第几个值,例如写1就是从匹配到的值里选取第一个。

7. 缺省值,也就是如果提取不到,那么默认给一个值替换提取结果。

在这里举一个例子如下:

现在有一段response值,

{"errorCode":"","success":true,"message":"","data":{"scoreId":"98","templateId":"","title":"新建权重评分表-auto-test","type":"1","state":"2","containers":[{"id":"692","text":"维度1","components":[{"id":"2196","text":"第一个指标","percent":"25","type":"1","options":[{"id":"6978","text":"好","score":"100","sort_num":1},{"id":"6979","text":"较好","score":"80","sort_num":2},{"id":"6980","text":"一般","score":"60","sort_num":3},{"id":"6981","text":"较差","score":"40","sort_num":4},{"id":"6982","text":"差","score":"0","sort_num":5}]},{"id":"2197","text":"第二个指标","percent":"25","type":"2","options":[{"id":"6983","text":"评分标准1","score":"100","sort_num":1}]}]},{"id":"693","text":"维度2","components":[{"id":"2198","text":"第三个指标","percent":"25","type":"2","options":[{"id":"6984","text":"评分标准2","score":"100","sort_num":1}]},{"id":"2199","text":"第四个指标","percent":"25","type":"1","options":[{"id":"6985","text":"好","score":"100","sort_num":1},{"id":"6986","text":"较好","score":"80","sort_num":2},{"id":"6987","text":"一般","score":"60","sort_num":3},{"id":"6988","text":"较差","score":"40","sort_num":4},{"id":"6989","text":"差","score":"0","sort_num":5}]}]}]}}

我们需要从中把维度1对应的id提取出来,那么可以按照如下参数(."id":"(.+?)","text":"维度1"*)提取:

image

附上正则表达式的图:

image

2. 使用beanshell提取

在取样器上右键,选择beanshell postprocessor,如图:

image

接下来看一下beanshell的界面:

image

beanshell就是一个运行Java的微环境(大概可以这么理解),在Script框里我们写java代码,jmeter执行到这里的时候就会执行里面的java代码,beanshell里面会有一些jmeter内置的变量,用于jmeter和你写的Java代码交互,常用的大约有这么几个:

1. log:写入信息到jmeber.log文件,使用方法:log. info(“This is log info!”);

2. vars - (JMeterVariables):操作jmeter变量,这个变量实际引用了JMeter线程中的局部变量容器(本质上是Map),它是测试用例与BeanShell交互的桥梁,常用方法:

3. prev - (SampleResult):获取前面的sample返回的信息,常用方法:

在这里根据我在工作里遇到的一个实战来说明

首先说明下需求,现在存在一个接口是post类型,http协议的,他需要若干个参数用以传参,其中一个参数“scoreData”是json类型的数据,数据结构如下:

{
"645(题目的id)":{"id":"2546(选项的id)","percent":"25(分值,固定值)"},
"646(同上)":{"id":"2551(同上)","percent":"24.75(同上)","value(该参数只有在题目的type=2的时候才会出现)":"99(分值,固定值)"}
}

说明一下这个数据结构,我们的问卷可能会存在多个题目,上面case里的645,646就代表是两道题目,题目的类型分为选择题和填分题,选择题的type是1,我们只取第一个选项的id(也就是说传参代表勾选了第一项);填分题的type是2,我们这里固定填99分,也就是value是99;percent这个字段是根据权重计算出来的值,这里不多做说明,就当做它的值也是一个固定的值。

数据全部来源于前一个接口的返回值,返回值如下:

{"success":true,"message":"","errorCode":"","data":[{"submit_state":"0","score_id":"f5","travel_id":"5","travel_title":"auto-test-zyj1502356486388","inspect_time":"2033-08-23","category_codes":"2266","suggest_content":"","my_score":"0","categoryName":"商业包装设计与施工","scoreData":{"fbScoreId":"f5","templateId":"","title":"新建权重评分表-auto-test-zyj","type":1,"containers":[{"id":"8","text":"维度1","components":[{"id":"14","text":"第一个指标","percent":"25","type":"1","options":[{"id":"38","text":"好","score":"100","sort_num":1},{"id":"39","text":"较好","score":"80","sort_num":2},{"id":"40","text":"一般","score":"60","sort_num":3},{"id":"41","text":"较差","score":"40","sort_num":4},{"id":"42","text":"差","score":"0","sort_num":5}],"select":""},{"id":"15","text":"第二个指标","percent":"25","type":"2","options":[{"id":"43","text":"评分标准1","score":"100","sort_num":1}],"select":""}]},{"id":"9","text":"维度2","components":[{"id":"16","text":"第三个指标","percent":"25","type":"2","options":[{"id":"44","text":"评分标准2","score":"100","sort_num":1}],"select":""},{"id":"17","text":"第四个指标","percent":"25","type":"1","options":[{"id":"45","text":"好","score":"100","sort_num":1},{"id":"46","text":"较好","score":"80","sort_num":2},{"id":"47","text":"一般","score":"60","sort_num":3},{"id":"48","text":"较差","score":"40","sort_num":4},{"id":"49","text":"差","score":"0","sort_num":5}],"select":""}]}]},"travelDetail":{"travel_id":"5","score_id":"f5","travel_title":"auto-test-zyj1502356486388","company_name":"上海东方雨虹防水技术有限责任公司","supplier_type":"2","tax_qualify":"2","organizer_id":"195104","inspect_time":"2033-08-23","scoreName":"新建权重评分表-auto-test-zyj","supplierTypeName":"代理商","taxQualifyName":"小规模纳税人","organizerName":"125","Teams":"zyj账号01、zyj账号02","Locations":[{"name":"办公室","remark":"备注1"},{"name":"地点一","remark":"备注2"}],"QualifyFiles":[{"name":"营业执照","remark":"备注3"},{"name":"地点二","remark":"备注4"}]},"travelPhoto":[],"photoDescription":{"1":[{"address":"地点一"},{"address":"办公室"}],"2":[{"address":"地点二"},{"address":"营业执照"}]},"photo_count":0,"score_item_total_count":"4"},{"submit_state":"0","score_id":"f4","travel_id":"4","travel_title":"auto-test-zyj1502351094326","inspect_time":"2033-08-23","category_codes":"2266","suggest_content":"","my_score":"0","categoryName":"商业包装设计与施工","scoreData":{"fbScoreId":"f4","templateId":"","title":"新建权重评分表-auto-test-zyj","type":1,"containers":[{"id":"6","text":"维度1","components":[{"id":"10","text":"第一个指标","percent":"25","type":"1","options":[{"id":"26","text":"好","score":"100","sort_num":1},{"id":"27","text":"较好","score":"80","sort_num":2},{"id":"28","text":"一般","score":"60","sort_num":3},{"id":"29","text":"较差","score":"40","sort_num":4},{"id":"30","text":"差","score":"0","sort_num":5}],"select":""},{"id":"11","text":"第二个指标","percent":"25","type":"2","options":[{"id":"31","text":"评分标准1","score":"100","sort_num":1}],"select":""}]},{"id":"7","text":"维度2","components":[{"id":"12","text":"第三个指标","percent":"25","type":"2","options":[{"id":"32","text":"评分标准2","score":"100","sort_num":1}],"select":""},{"id":"13","text":"第四个指标","percent":"25","type":"1","options":[{"id":"33","text":"好","score":"100","sort_num":1},{"id":"34","text":"较好","score":"80","sort_num":2},{"id":"35","text":"一般","score":"60","sort_num":3},{"id":"36","text":"较差","score":"40","sort_num":4},{"id":"37","text":"差","score":"0","sort_num":5}],"select":""}]}]},"travelDetail":{"travel_id":"4","score_id":"f4","travel_title":"auto-test-zyj1502351094326","company_name":"上海东方雨虹防水技术有限责任公司","supplier_type":"2","tax_qualify":"2","organizer_id":"195104","inspect_time":"2033-08-23","scoreName":"新建权重评分表-auto-test-zyj","supplierTypeName":"代理商","taxQualifyName":"小规模纳税人","organizerName":"125","Teams":"zyj账号01、zyj账号02","Locations":[{"name":"办公室","remark":"备注1"},{"name":"地点一","remark":"备注2"}],"QualifyFiles":[{"name":"营业执照","remark":"备注3"},{"name":"地点二","remark":"备注4"}]},"travelPhoto":[],"photoDescription":{"1":[{"address":"地点一"},{"address":"办公室"}],"2":[{"address":"地点二"},{"address":"营业执照"}]},"photo_count":0,"score_item_total_count":"4"}]}

要提取数据,尤其是这么大的json数据,首先我们要把这个json的数据结构分析清楚,我推荐大家把这个json放到json解析网站里解析一下结构,这样会更清楚,例如bejson,解析出来如下(部分要点截图):

image

我们现在需要做两件事:

  1. 取值,具体是取每个components下面id的值(也就是题目的id),以及每个options下面第一个id的值(也就是选项的id);
  2. 将取到的值组合成需要的数据结构再输出。

对于json的处理,我们需要用到额外的包,json的官网里有,在java选项下面,第一个JSON-java就是,这个包叫做org.json,我们下载下来之后,把这个包放到jmeter的apache-jmeter\lib\ext下面,这样就可以用了。

简单介绍下org.json,这部分建议看一下教程

JSONObject

是一个无序的键/值对集合。

JSONArray

是一个有序的序列值。

我们大约只用到这两种就可以了

这里直接放出处理的源码:

import java.util.HashMap;
import java.util.Map;

import org.json.*;
    
    System.out.println("test................................................................"); 
    //提取返回值作为变量response_data
    String response_data = prev.getResponseDataAsString();
    //解析json
    JSONObject data_obj = new JSONObject(response_data);        
    JSONArray jsonarray = data_obj.getJSONArray("data");
    JSONObject scoreData = null;
    int id = Integer.parseInt(vars.get("travel_id"));
    System.out.println("id="+id);   
    for (int i = 0; i < jsonarray.length(); i++) { 
        JSONObject jsonobj = jsonarray.getJSONObject(i); 
       System.out.println(jsonarray.optString(i)); 
       System.out.println(jsonobj.getInt("travel_id")); 
       System.out.println(jsonobj.getString("travel_title")); 
       int travle_id = jsonobj.getInt("travel_id");
       if(travle_id==id){   
           scoreData = jsonobj.getJSONObject("scoreData");
            }           
        }   
    JSONArray containers = scoreData.getJSONArray("containers");
    Map data = new HashMap();   
    for (int i = 0; i < containers.length(); i++){
        
        JSONArray component_ary = containers.getJSONObject(i).getJSONArray("components");
         
            for (int j = 0; j < component_ary.length(); j++){
                System.out.println(component_ary.optString(j)); 
             System.out.println(component_ary.getJSONObject(j).get("id")); 
             String data_id = component_ary.getJSONObject(j).get("id").toString();
             JSONArray options = component_ary.getJSONObject(j).getJSONArray("options");    
             String score_id = options.getJSONObject(0).get("id").toString();
             System.out.println(options.getJSONObject(0).get("id")); 
              Map score = new HashMap(); 
             score.put("id", score_id);
             score.put("percent", "25");
             if(component_ary.getJSONObject(j).getInt("type")==2){
                 score.put("value", "99");                  
                }
             System.out.println(score); 
             data.put(data_id, score);
             System.out.println(data); 
                
            }
            
        }
    //将map类型转换为json字符
    JSONObject putdata = new JSONObject(data);
    System.out.println(putdata);
    //将putdata输出到jmeter变量里
    vars.put("putdata", JSONObject.valueToString(putdata));

运行的结果(不是用上面的参数值运行,是我脚本实例的结果,但数据结构是一样的),

putdata的值:putdata={"671":{"id":"2660","percent":"25","value":"99"},"672":{"id":"2661","percent":"25","value":"99"},"673":{"id":"2662","percent":"25"},"670":{"id":"2655","percent":"25"}},这个值被作为变量“putdata”给jmeter使用,我们可以在debugSampler看到这个值:

image

我们可以在接口里使用这个值了,如图:

image

一些tips

1. 新建处理器的时候,注意选择前置还是后置,前置是指在请求之前进行处理;后置是指在请求之后进行处理。

2. 调试脚本,可以新建一个debugSampler,脚本运行之后,我们可以在查看结果树里查看他的响应数据来观察变量的值;如果是beanshell,可以先在编译器里调试好再复制进去,或者直接打包成jar引用。

引用资料

json教程:http://www.open-open.com/lib/view/open1381566882614.html

beanshell的使用: http://www.cnblogs.com/puresoul/p/4915350.html

上一篇下一篇

猜你喜欢

热点阅读