问题
I have caught up in a situation, where in i need to verify the response of the Previous Sampler for one of the value and if the Value for that is [], then i need to trigger the below request or else then switch to another Sampler.
Flow:
Check Response of Sampler for One of the attribute
IF(attribute value==[])
Execute the Sampler under IF Conditions.
ELSE
New Sampler
Sample Response: {"id":8,"merchant_id":"39","title":"Shirts-XtraLarge","subtitle":null,"price":110,"description":null,"images":"image_thumbs":[[]],"options":[],"options_available":[],"custom_options":[]}
I need to check if the attribute custom_options is empty or not! If Empty do some actions and if not empty do some other action !
Need if condition to simulate this!
Help is useful!
回答1:
Go for Switch Controller
- Add JSR223 PostProcessor as a child of the request which returns your JSON
Put the following code into "Script" area:
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') }
Add Switch Controller to your Test Plan and use
${size}
as "Switch Value"- Add Simple Controller as a child of the Switch Controller and give
empty
name to it. Put request(s) related to empty "custom_options" under thatempty
Simple Controller Add another Simple Controller as a child of the Switch Controller and give
notempty
name to it. Put request(s) related to not empty "custom_options" under thatnotempty
Simple Controller.
More information: Selection Statements in JMeter Made Easy
回答2:
A nice to have feature in JMeter would be Else statement, but until then you will have to use 2 If Controllers
If Controller allows the user to control whether the test elements below it (its children) are run or not.
Assuming you hold your attribute value using regex/json/css/other post processor extractor add two condition, first is positive and under it the Sampler:
${__groovy("${attributeValue}" == "[]")}
Second is negative and under it add the New Sampler
${__groovy("${attributeValue}" != "[]")}
__groovy is encourage to use over default Javascript
Checking this and using __jexl3 or __groovy function in Condition is advised for performances
来源:https://stackoverflow.com/questions/49407770/how-to-bypass-the-sampler-based-on-previous-response-value-in-jmeter