0
votes

I have to send JSON requests based on the CSV test data. Suppose there are 90 records - which are basically the request bodies. I put the Thread in a loop to keep sending the request until the last one in the CSV. Every time I get the response, I need to append them into a single CSV file. Now, since Jmeter Listener does not consolidate all the responses into CSV (I do not want it in xml), I want to know if I can write a Java snippet in BeanShell, capture all responses and write them to a CSV file.

2

2 Answers

1
votes

You can use JSR223 Sampler with File.append adding text with , to append to CSV file

This will append to the end of the file.

File file = new File("out.txt")
file.append("hello\n")
0
votes

If you want the "program"

  1. Add JSR223 Listener to your Test Plan (since JMeter 3.1 it is recommended to use JSR223 Test Elements for scripting)
  2. Put the following code into "Script" area

    new File('myfile.csv') << prev.getResponseDataAsString() << System.getProperty('line.separator')
    

    where prev stands for previous SampleResult class instance. Check out Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on JMeter API shorthands available for the JSR223 Test Elements.


A better option would be saving a response into a JMeter Variable using i.e. Regular Expression Extractor and writing it to a file via sample_variables property