how to Bypass the Sampler based on previous response value in jmeter?

让人想犯罪 __ 提交于 2019-12-11 16:18:58

问题


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

  1. Add JSR223 PostProcessor as a child of the request which returns your JSON
  2. 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')
    }
    
  3. Add Switch Controller to your Test Plan and use ${size} as "Switch Value"

  4. 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 that empty Simple Controller
  5. 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 that notempty 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!