[英]Jmeter PostProcessor with Groovy and fetching content of response data
In JSR223 PostProcessor I am using this method to get the response data: 在JSR223 PostProcessor中,我使用此方法来获取响应数据:
def json = new JsonSlurper().parseText(response)
Here is a snippet of my json output is like this 这是我的json输出的片段是这样的
XmlItemResult:[[xPath:/Blocks/Block[001], name:abc, folder:\A\abc\a1, id:84, information:[[xPath:/Blocks/Block[001], result:Block number 1: abc], [xPath:/Blocks/Block[001]/Steps/CallSteps/Step[001], result:Call step StepNo 1],
folder:\\Voice133, id:2542, information:[[xPath:/TestCases/TestCase[001], 文件夹:\\ Voice133,ID:2542,信息:[[xPath:/ TestCases / TestCase [001],
This response, as you see contains two things which I am interested in: 如您所见,此响应包含我感兴趣的两件事:
folder:\A\abc\a1, id:84,
folder:\Voice133, id:2542,
I need to get the id value for only this line --> folder:\\Voice133, id:2542, 我只需要获取此行的ID值->文件夹:\\ Voice133,ID:2542,
note 2542 is variable and can be different each time and after each run. note 2542是可变的,每次和每次运行后都可以不同。
I tried 我试过了
json.find ("Voice133, id:(.+?),")
Your string is not a valid JSON, you can check it yourself using any online JSON validator , therefore you won't be able to use JsonSlurper , you will have to go for Regular Expressions instead. 您的字符串不是有效的JSON,您可以使用任何在线JSON验证器自己检查它,因此您将无法使用JsonSlurper ,而必须使用正则表达式。
In Groovy you can use =~
- Find Operator in order to be able to extract the required value(s), example code would be something like: 在Groovy中,您可以使用
=~
- Find运算符以便能够提取所需的值,示例代码如下所示:
def response = 'XmlItemResult:[[xPath:/Blocks/Block[001], name:abc, folder:\\A\\abc\\a1, id:84,' +
' information:[[xPath:/Blocks/Block[001], result:Block number 1: abc],' +
' [xPath:/Blocks/Block[001]/Steps/CallSteps/Step[001], result:Call step StepNo 1], '
def matcher = (response =~ 'folder:\\\\A\\\\abc\\\\a1, id:(\\d+),')
if (matcher.find()) {
log.info('Folder ID = ' + matcher.group(1))
}
Demo: 演示:
More information: Apache Groovy - Why and How You Should Use It 更多信息: Apache Groovy-为什么以及如何使用它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.