Jmeter - 动态地将特定的JSON响应传递给HTTP请求

时间:2017-11-21 06:06:04

标签: json jmeter httprequest httpresponse beanshell

我在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是否有针对上述要求的解决方法。

2 个答案:

答案 0 :(得分:0)

您的要求可以通过以下步骤来实现。

  1. 将“JSON Extractor”添加到请求中,其中响应包含您要在下一个请求中传递的参数。(Image-1)
  2. JSON Extractor配置(Image-2)
  3. 保持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());
    

    Image-1

    Image-2

答案 1 :(得分:0)

请注意{JMter中任何脚本的since JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy languageGroovy has much better performance than Beanshell does,而且Groovy has built-in JSON support

  1. 添加JSR223 PostProcessor作为返回上述JSON
  2. 的请求的子代
  3. 确保在&#34;语言&#34;中选择import subprocess terminal1 = subprocess.Popen(["gnome-terminal",'cd ~',"torcs -r ~/quickrace.xml"]) terminal2 = subprocess.Popen(['gnome-terminal','cd ~']) 下拉列表和groovy框勾选
  4. 将以下代码放入&#34;脚本&#34;面积:

    Cache compiled script if available
  5. 上面的代码将生成以下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')) 
    

    JMeter Groovy Generate JSON

    您可以根据需要[ { "id": "SomeNumber", "parentObjectApiName": "SomeName" }, { "id": "SomeOtherNumber", "parentObjectApiName": "SomeOtherName" } ] 访问它(例如在下一个HTTP Request采样器&#34;正文数据&#34;标签中)