简体   繁体   English

JMeter:需要根据某些标准将 CSV 文件中的所有值发送到请求正文中的不同变量

[英]JMeter: Need to send all values in a CSV File to different variables in a request body, based on some criteria

I have a CSV file, which has some user details, like their employee id (123456) and email id (abc@xyz.com).我有一个 CSV 文件,其中包含一些用户详细信息,例如他们的员工 ID (123456) 和电子邮件 ID (abc@xyz.com)。 The employee id/email id of the users are present in the first column, and the second column contains the type of the id (employee id/email id).用户的员工 ID/电子邮件 ID 显示在第一列中,第二列包含 ID 的类型(员工 ID/电子邮件 ID)。 It looks like below:如下所示:

id ID idType idType
123456 123456 empid空的
abc@xyz.com abc@xyz.com emailid电子邮件ID
111111 111111 empid空的
ghi@xyz.com ghi@xyz.com emailid电子邮件ID

In the body of the HTTP request (JSON), I need to pass these values to different variables, based on the id type given in the CSV file.在 HTTP 请求 (JSON) 的正文中,我需要根据 CSV 文件中给出的 id 类型将这些值传递给不同的变量。 For example, if the id type id empid, I need to pass those values to empid array;例如,如果 id 类型为 id empid,我需要将这些值传递给 empid 数组; and so on.等等。 I want the passed values to look like below:我希望传递的值如下所示:

{"empid":["123456", "111111"], "emailid":["abc@xyz.com", "ghi@xyz.com"]}

If the value is passed to a single variable, I can achieve the same using User Parameters Preprocessor, but this requirement is a bit tricky.如果将值传递给单个变量,我可以使用用户参数预处理器实现相同的目的,但这个要求有点棘手。 Is there any method to pass the values to the request in the above mentioned format?是否有任何方法可以将值以上述格式传递给请求?

You can do it using a suitableJSR223 Test Element , ie您可以使用合适的JSR223 测试元素来做到这一点,即

  1. Add JSR223 PreProcessor as a child of the request which needs to have this JSON body generated添加JSR223 PreProcessor作为需要生成此 JSON 正文的请求的子项

  2. Put the following code into "Script" area:将以下代码放入“脚本”区域:

     def lines = new File('/path/to/your/file.csv').readLines().drop(1) def empIds = lines.findAll { line -> line.split(',')[1] == 'empid' }.collect { line -> line.split(',')[0] } def emailIds = lines.findAll { line -> line.split(',')[1] == 'emailid' }.collect { line -> line.split(',')[0] } def payload = ['empid': empIds, 'emailid': emailIds] vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
  3. Use ${payload} JMeter Variable reference in the "Body Data" tab of the HTTP Request sampler (or wherever you need to use this JSON)在 HTTP 请求采样器的“Body Data”选项卡中使用${payload} JMeter 变量引用(或您需要使用此 JSON 的任何地方)

More information:更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM