没有方法签名:org.apache.jmeter.threads.JMeterVariables.put()适用于参数类型:(java.lang.String,java.lang.Integer)

时间:2018-03-28 10:42:29

标签: jmeter beanshell jmeter-3.2 jsr223

我有一个示例JSON响应如下:

{
"id": 37,
"merchant_id": "39",
"title": "Parker Pens",
"subtitle": null,
"price": 1000,
"description": null,
"images": [],
"image_thumbs": [],
"options": [{
    "code": "color",
    "label": "Color",
    "extra_info": "",
    "values": [{
        "label": "Red",
        "value": "8"
    }, {
        "label": "Yellow",
        "value": "9"
    }, {
        "label": "Pink",
        "value": "10"
    }]
  }, {
    "code": "size",
    "label": "Size",
    "extra_info": "",
    "values": [{
        "label": "Small",
        "value": "4"
    }, {
        "label": "Medium",
        "value": "5"
    }, {
        "label": "Large",
        "value": "6"
    }]
}],
"options_available": [{
    "combination": [{
        "code": "color",
        "value": "Red"
    }, {
        "code": "size",
        "value": "Small"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Red"
    }, {
        "code": "size",
        "value": "Medium"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Red"
    }, {
        "code": "size",
        "value": "Large"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Yellow"
    }, {
        "code": "size",
        "value": "Small"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Yellow"
    }, {
        "code": "size",
        "value": "Medium"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Yellow"
    }, {
        "code": "size",
        "value": "Large"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Pink"
    }, {
        "code": "size",
        "value": "Small"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Pink"
    }, {
        "code": "size",
        "value": "Medium"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Pink"
    }, {
        "code": "size",
        "value": "Large"
    }]
}],
"custom_options": []
}

我将我的采样器作为

import org.json.JSONObject;
import org.json.JSONArray;

String response= prev.getResponseDataAsString();
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("options");
Integer count= jsonArray.length();
vars.put('counts', count);

但是在运行脚本时我遇到了错误: No signature of method: org.apache.jmeter.threads.JMeterVariables.put() is applicable for argument types: (java.lang.String, java.lang.Integer)

除了值之外(Counts = 2)。我的目的是在“选项”键中获取数组的计数(参见上面的响应)

2 个答案:

答案 0 :(得分:2)

vars.put不支持与常规String不同的值,并且您尝试设置Integer值。

通过简单转换轻松解决问题:

vars.put("counts", Integer.toString(count));

另一个选项是使用vars.putObject

保存对象
vars.putObject("counts", count);

根据JMeter best practices移动到JSR223 Sampler:

  

从JMeter 3.1开始,我们建议从BeanShell切换到JSR223测试元素

答案 1 :(得分:1)

转到JSR223 PostProcessor和Groovy语言:

  1. Groovy performance is much better comparing to Beanshell
  2. Groovy has built-in JSON support
  3. Groovy provides a lot of enhancements on top of Java SDK
  4. 你的7行Beanshell代码的Groovy相当于:

    vars.put('counts',new groovy.json.JsonSlurper().parse(prev.getResponseData()).options.size() as String)
    
相关问题