简体   繁体   English

具有Groovy的Jmeter PostProcessor,并获取响应数据的内容

[英]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: 演示:

JMeter Groovy正则表达式

More information: Apache Groovy - Why and How You Should Use It 更多信息: Apache Groovy-为什么以及如何使用它

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

相关问题 在 JSR223 后处理器(Groovy)中使用来自响应的解密 JSON - Jmeter - Using the decrypted JSON from response in JSR223 Postprocessor(Groovy) - Jmeter 如何使用 JSR223 后处理器读取作为 CSV 出现的 jmeter 响应数据并将其分配给 JSON object - How to read jmeter response data that comes as a CSV using the JSR223 PostProcessor and assign it to a JSON object JMeter:在另一个后处理器中使用 JSON 后处理器_ALL 变量来获取数据 - JMeter : Using the JSON Postprocessor _ALL variable in one more postprocessor to fetch the data 验证Groovy + Jmeter中的Json响应 - Validate Json response in groovy + Jmeter 使用PostProcessor将响应保存到JMeter中的文件 - Save responses to a file in JMeter with PostProcessor 我们可以使用 Jmeter 从 BeanShell PostProcessor 中的响应断言打印成功消息/断言结果吗? - Can we print success message/Assertion Result from Response Assertion in BeanShell PostProcessor using Jmeter? 如何在JMeter中使用Groovy编写响应声明 - How to Write Response Assertion Using Groovy in JMeter 无法移至变量完​​整响应-Groovy Jmeter - Can not move to variable full response - Groovy Jmeter 在jmeter中使用groovy响应断言脚本的问题 - Issue with groovy response assertion script in jmeter Jmeter,使用jsr223 postProcessor将数据写入文件(如果存在) - Jmeter, over writing data to file if it exists using jsr223 postProcessor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM