Search code examples
jmeterjmeter-plugins

How to append Http Response to a Jmeter variable


I need to append response from set of http requests and assert response. Example

Request1-> Response is "Anil"
Request2-> Response is "Sunil"
Request3-> Response is "Kumar"
Request4-> Response is "Tej"

I need to create a global variable "GlobalRespone". Append and store all response to this variable ex: GlobalResponse = "Anil,Sunil,Kumar,Tej" So that I can write the custom validation rule at Request4 which will pass the Request4 if GlobalResponse contains "Sunil" or else fail the sampler.

How can we achieve this? appreciate if any one can help.


Solution

    1. Add JSR223 PostProcessor at the same level as all your requests

    2. Put the following code into "Script" area:

      vars.put('GlobalRespone', (vars.get('GlobalRespone') ?: '') + prev.getResponseDataAsString())
      
    3. Add Response Assertion as a child of the last request

    4. Configure it as follows:

      enter image description here

    The JSR223 PostProcessor will append to the variable GlobalRespone the response data of each Sampler in its scope and the Response Assertion will check whether this variable contains Sunil

    Demo:

    enter image description here