我在Jmeter(2.13)中有一个特定的要求,我需要动态多次传递两个参数作为id和parentObjectApiName
{
"id":"SomeNumber",
"parentObjectApiName":"SomeName"
},
{
"id":"SomeNumber",
"parentObjectApiName":"SomeName"
},
}
我将从答复中得到:
{
"detailMap": {
"RootNumber": [
{
"id": "SomeNumber",
"properties": {
},
"isDeleted": false,
"version": "2017-11-20T08:13:30+00:00",
"referenceId": null,
"parentObjectApiName": "SomeName"
},
{
"id": "SomeNumber",
"properties": {
},
"isDeleted": false,
"version": "2017-04-21T15:40:10.742+00:00",
"referenceId": null,
"parentObjectApiName": "SomeName"
},
{
:
},
]
}
"state": {
"errorDetails": []
}
}
在Jmeter(2.13)中使用beanshell是否有针对上述要求的解决方法。
答案 0 :(得分:0)
您的要求可以通过以下步骤来实现。
保持JSON Extractor不变,将“Beanshell PostProcessor”添加到请求中并保留以下部分代码并尝试。你想要的id& parentObjectApiName将存储在变量“json”中。您可以在下一个请求中将其作为$ {json}
进行调用import java.io.file;
import java.util.*;
import org.apache.jmeter.services.fileserver;
StringBuilder output = new StringBuilder();
Random random = new Random();
int max = Integer.parseInt(vars.get("id_matchNr"));
for(int i=1;i<=max;i++)
{
output.append("{");
output.append("\"id\":\"" + vars.get("id_"+i) + "\",\"parentObjectApiName\":" + vars.get("parentObjectApiName_"+i));
output.append("}").append( "," );
}
String withoutLastComma = output.substring( 0, output.length( ) - ",".length( ) );
vars.put("json", withoutLastComma.toString());
答案 1 :(得分:0)
请注意{JMter中任何脚本的since JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language。 Groovy has much better performance than Beanshell does,而且Groovy has built-in JSON support。
import subprocess
terminal1 = subprocess.Popen(["gnome-terminal",'cd ~',"torcs -r ~/quickrace.xml"])
terminal2 = subprocess.Popen(['gnome-terminal','cd ~'])
下拉列表和groovy
框勾选将以下代码放入&#34;脚本&#34;面积:
Cache compiled script if available
上面的代码将生成以下JSON:
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import groovy.json.internal.LazyMap
def text = prev.getResponseDataAsString()
log.info('Original response: ' + text)
def json = new JsonSlurper().parseText(text)
def data = new ArrayList()
json.detailMap.RootNumber.each { rootNumber ->
def map = new LazyMap()
map.put("id", rootNumber.id)
map.put("parentObjectApiName", rootNumber.parentObjectApiName)
data.add(map)
}
vars.put('json',JsonOutput.prettyPrint(JsonOutput.toJson(data)))
log.info('Generated json: ' + vars.get('json'))
您可以根据需要[
{
"id": "SomeNumber",
"parentObjectApiName": "SomeName"
},
{
"id": "SomeOtherNumber",
"parentObjectApiName": "SomeOtherName"
}
]
访问它(例如在下一个HTTP Request采样器&#34;正文数据&#34;标签中)