JMeter-从 JSON 响应中提取“id"变量并将其保存在 CSV 文件中并在拆卸线程中使用它 [英] JMeter- extract 'id' variables from JSON response and save it in CSV file and use it in tear down thread
问题描述
在我的帖子请求中创建一个工作,其响应只有 id 如下,
{id":626}
我想将 626 的 id 值保存在 csv 或任何文件中,在我的测试完成后,我想使用该文件中的所有这些值来检查 tear down中的作业状态
如何完成?我有以下脚本但出现错误,
new groovy.json.JsonSlurper().parse(prev.getResponseData()).id.each { entry ->新文件('result.csv')<<entry.get('id') <<System.getProperty('line.separator')}
错误详情,
2021-01-15 12:13:05,699 ERROR o.a.j.e.JSR223PostProcessor:JSR223 脚本中的问题,JSR223 PostProcessorjavax.script.ScriptException:groovy.lang.MissingMethodException:没有方法签名:java.lang.Integer.get() 适用于参数类型:(String) 值:[id]可能的解决方案:getAt(java.lang.String)、next()、grep()、grep(java.lang.Object)、wait()、abs()
只需将 entry.get('id')
更改为 entry
即可:
new groovy.json.JsonSlurper().parse(prev.getResponseData()).id.each { entry ->新文件('result.csv')<<入口<
但是,如果您使用 > 运行脚本,则您的方法存在潜在问题.当多个线程同时写入同一个文件时,您可能会遇到
使用
In my post request create a job whose response have only id as follow,
{"id":626}
And I want to save the id value which is 626 in a csv or any file and after my test complete I want use all these value from this file to check the status of the job in the tear down
thread group.
how to complete this ? I have following script but getting error,
new groovy.json.JsonSlurper().parse(prev.getResponseData()).id.each { entry ->
new File('result.csv') << entry.get('id') << System.getProperty('line.separator')
}
error details,
2021-01-15 12:13:05,699 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor
javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.get() is applicable for argument types: (String) values: [id]
Possible solutions: getAt(java.lang.String), next(), grep(), grep(java.lang.Object), wait(), abs()
Just change entry.get('id')
to just entry
and it should work:
new groovy.json.JsonSlurper().parse(prev.getResponseData()).id.each { entry ->
new File('result.csv') << entry << System.getProperty('line.separator')
}
However there is a potential problem with your approach, if you run your script with > 1 thread you may run into a race condition when multiple threads will be concurrently writing into the same file so more error-proof approach would be:
Add the next line to user.properties file:
sample_variables=id
Extract the id from the response using JSON JMESPath Extractor configured like:
Use Flexible File Writer to write the value(s) to the file:
这篇关于JMeter-从 JSON 响应中提取“id"变量并将其保存在 CSV 文件中并在拆卸线程中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!