Jmeter: How to store the response of several different request into csv file to re-use

20 Views Asked by At

I have a set of several get requests that return json data:

  • Get area
  • Get category
  • Get status

I would like to get some pieces of data from each of request's response to save into csv file to re-use for a POST request later

I write some groovy script as below for 'Get area' request:

import groovy.json.*
import groovy.io.FileType


String folderPath = "${root_folder}/Test_Data/J1/"

new File(folderPath).eachFile (FileType.FILES) { file ->
   //Delete file if file name contains old file
   if (file.name.contains('area_1level_alias_list')) file.delete()
   }

def slurper = new JsonSlurper()
def response = slurper.parseText(prev.getResponseDataAsString())
def res_array = response.data

log.info("Res: " + res_array)
def list_response = new File("${folderPath}/area_1level_alias_list.csv")

list_response << "area_1level\n"

for (int i = 0; i < res_array.size(); i++){
   var area_1level_alias = res_array[i].alias
   list_response << area_1level_alias
   list_response << "\n"
   }

So my question is, how to use JSR223 Post Processor for the rest 2 http request: Get category, Get status so that I can store the data I need into the same csv file that I cretead from the code above into the 2nd column, 3rd column...

1

There are 1 best solutions below

0
Dmitri T On

We don't know how does your response data look like and what parts of it you need to store hence we cannot come with the comprehensive solution or full code.

If you want to append some data into other "columns" you need to remove all line breaks from your code, to wit get rid of \n characters.

You should use it once you populate the last piece of data in the last "column". And better go for System.lineSeparator() instead.

With regards to the approach in general - it's suitable only for running it with 1 thread (virtual user), if you will have > 1 thread you will get into the race condition when multiple threads will be writing to the same file at the same time causing data corruption and/or loss. So if you plan to run your code in multithreaded manner it's a good time to consider switching to i.e. Flexible File Writer or storing the data elsewhere