如何根据 jmeter 中的先前响应值绕过采样器?
how to Bypass the Sampler based on previous response value in jmeter?
我遇到了一种情况,我需要验证前一个采样器对其中一个值的响应,如果该值的值为 [],那么我需要触发以下请求,否则然后切换到另一个采样器。
Flow:
Check Response of Sampler for One of the attribute
IF(attribute value==[])
Execute the Sampler under IF Conditions.
ELSE
New Sampler
响应示例:
{"id":8,"merchant_id":"39","title":"Shirts-XtraLarge","subtitle":null,"price":110,"description":null,"images":"image_thumbs":[[]],"options":[],"options_available":[],"custom_options":[]}
我需要检查属性 custom_options 是否为空!如果为空则执行一些操作,如果不为空则执行其他操作!
需要if条件来模拟这个!
帮助很有用!
Else 语句是 JMeter 的一个很好的功能,但在那之前你将不得不使用 2 If Controllers
If Controller allows the user to control whether the test elements below it (its children) are run or not.
假设您使用 regex/json/css/other post 处理器提取器保存您的属性值添加两个条件,第一个是正数,在它下面是采样器:
${__groovy("${attributeValue}" == "[]")}
第二个是负数,在其下添加新采样器
${__groovy("${attributeValue}" != "[]")}
__groovy 鼓励使用默认 Javascript
Checking this and using __jexl3 or __groovy function in Condition is advised for performances
- 将 JSR223 PostProcessor 添加为 returns 您的 JSON
请求的子项
将以下代码放入"Script"区域:
def size = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..custom_options')[0].size()
if (size == 0) {
vars.put('size', 'empty')
} else {
vars.put('size', 'notempty')
}
将 Switch Controller 添加到您的测试计划并将 ${size}
用作 "Switch Value"
- 添加 Simple Controller 作为 Switch Controller 的子项并为其命名
empty
。将与空 "custom_options" 相关的请求放在 empty
Simple Controller 下
添加另一个 Simple Controller 作为 Switch Controller 的子项,并为其命名 notempty
。将与非空 "custom_options" 相关的请求放在 notempty
Simple Controller.
下
我遇到了一种情况,我需要验证前一个采样器对其中一个值的响应,如果该值的值为 [],那么我需要触发以下请求,否则然后切换到另一个采样器。
Flow:
Check Response of Sampler for One of the attribute
IF(attribute value==[])
Execute the Sampler under IF Conditions.
ELSE
New Sampler
响应示例: {"id":8,"merchant_id":"39","title":"Shirts-XtraLarge","subtitle":null,"price":110,"description":null,"images":"image_thumbs":[[]],"options":[],"options_available":[],"custom_options":[]}
我需要检查属性 custom_options 是否为空!如果为空则执行一些操作,如果不为空则执行其他操作!
需要if条件来模拟这个!
帮助很有用!
Else 语句是 JMeter 的一个很好的功能,但在那之前你将不得不使用 2 If Controllers
If Controller allows the user to control whether the test elements below it (its children) are run or not.
假设您使用 regex/json/css/other post 处理器提取器保存您的属性值添加两个条件,第一个是正数,在它下面是采样器:
${__groovy("${attributeValue}" == "[]")}
第二个是负数,在其下添加新采样器
${__groovy("${attributeValue}" != "[]")}
__groovy 鼓励使用默认 Javascript
Checking this and using __jexl3 or __groovy function in Condition is advised for performances
- 将 JSR223 PostProcessor 添加为 returns 您的 JSON 请求的子项
将以下代码放入"Script"区域:
def size = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..custom_options')[0].size() if (size == 0) { vars.put('size', 'empty') } else { vars.put('size', 'notempty') }
将 Switch Controller 添加到您的测试计划并将
${size}
用作 "Switch Value"- 添加 Simple Controller 作为 Switch Controller 的子项并为其命名
empty
。将与空 "custom_options" 相关的请求放在empty
Simple Controller 下
添加另一个 Simple Controller 作为 Switch Controller 的子项,并为其命名
下notempty
。将与非空 "custom_options" 相关的请求放在notempty
Simple Controller.