Search code examples
jmeterjmeter-plugins

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


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!


Solution

  • 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.

      JMeter Switch Controller

    More information: Selection Statements in JMeter Made Easy