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
线程组中作业的状态. /p>
如何完成此操作? 我有以下脚本,但出现错误,
new groovy.json.JsonSlurper().parse(prev.getResponseData()).id.each { entry ->
new File('result.csv') << entry.get('id') << System.getProperty('line.separator')
}
错误详细信息,
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()
只需将entry.get('id')
更改为entry
,它应该可以工作:
new groovy.json.JsonSlurper().parse(prev.getResponseData()).id.each { entry ->
new File('result.csv') << entry << System.getProperty('line.separator')
}
但是,如果您使用>当多个线程同时写入同一文件时,您可能会遇到一个竞争条件的1个线程因此,更防错的方法将是:
-
将下一行添加到 user.properties 文件:
sample_variables=id
-
使用 JSON JMESPath Extractor 配置如下:
-
使用 Flexible File Writer 将值写入到文件:
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屋!