从 CSV 中读取数据并在 Jmeter 中创建 Json 数组

Read data from CSV and create Json Array in Jmeter

我有一个 POST 请求,下面是我的 Body 负载。

{
    "ABC": "ITBK1",
    "Code": "AH01001187",
    "ScheduleDate": "2021-09-02T22:59:00Z",
    "FilterType": 2,
    "FilterData": [
        "LoadTest92","LoadTest93"
    ]
}

我正在将 ContractorId 传递给 filterData,如下所示。

{
    "ABC": "ITBK1",
    "Code": "AH01001187",
    "ScheduleDate": "${startTo}",
    "FilterType": 2,
    "FilterData": ["${contractorId}"]
}

但它一次需要一个 ID 来处理这个 Json。我如何从 csv 发送此 FilterData jsonArray 的多个数据请帮助解决这个问题。

首先don't post code (including CSV file content) as image

根据 CSV Data Set Config 文档:

By default, the file is only opened once, and each thread will use a different line from the file. However the order in which lines are passed to threads depends on the order in which they execute, which may vary between iterations. Lines are read at the start of each test iteration. The file name and mode are resolved in the first iteration.

所以这意味着您需要转到下一次迭代才能阅读下一行。

如果您想将列 J 中的所有值作为“FilterData”发送,您可以这样做:

  1. 添加 JSR223 PreProcessor 作为您要参数化的请求的子项

  2. 将以下代码放入“脚本”区域:

    def lines = new File('/path/to/your/file.csv').readLines()
    
    def payload = []
    
    lines.each { line ->
        def contractor = line.split(',')[9]
        payload.add(contractor as String)
    }
    
    vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
    
  3. 就是这样,在 HTTP 请求中使用 ${payload} 而不是您的 ["${contractorId}"] 变量。

更多信息: