How to save a sample response for every different response code in jmeter?

Viewed 122

I want to sample response data for every single different response code.

For Example, if there are 4 different kinds of response codes received in a test, namely 200, 403, 400, 500, then I want to save one response for each code and there should be 4 different files generated after this test.

2 Answers

Assuming that you want exactly one sample for each response code, I propose to use a PostProcessor. Just some pseudo code:

case response code:
   200: 
       if !(200.log exists): write previous response to file
   400: 
       if !(400.log exists): write previous response to file
   403: 
       if !(403.log exists): write previous response to file
   500: 
       if !(400.log exists): write previous response to file
  1. Add JSR223 Listener to your Test Plan

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

    new File(prev.getResponseCode() + '.txt') << prev.getResponseDataAsString()
    
  3. That's it, once you run your test the script will generate files like 200.txt, 403.txt, etc. in "bin" folder of your JMeter installation.

In the above code snippet prev stands for previous SampleResult, see the linked JavaDoc for the list of available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy to learn more about other JMeter API shorthands available for JMeter's JSR223 Test Elements

Related