0
votes

Is there a way (any jmeter plugin) by which we can have the JMeter script read all the contents(String) from external text file ?

I have a utility in java which uses Jackson ObjectMapper to convert a arraylist to string and puts it to a text file in the desktop. The file has the JSON info that i need to send in the jmeter Post Body.

I tried using ${__FileToString()} but it was unable to deserialize the instance of java.util.ArrayList. It was also not reading all the values properly.

I am looking for something like csv reader where i just give the file location. I need all the json info present in the file. Need to extract it and assign to the post body.

Thanks for your help !!!

1

1 Answers

0
votes

If your question is about how to deserialize ArrayList in JMeter and dynamically build request body, you can use i.e. Beanshell PreProcessor for it.

  1. Add a Beanshell PreProcessor as a child of your request
  2. Put the following code into the PreProcessor's "Script" area:

    FileInputStream in = new FileInputStream("/path/to/your/serialized/file.ser");
    ObjectInput oin = new ObjectInputStream(in);
    ArrayList list = (ArrayList) oin.readObject();
    oin.close();
    in.close();
    
    for (int i = 0; i < list.size(); i++) {
        sampler.addArgument("param" + i, list.get(i).toString());
    }
    

The code will read file as ArrayList, iterate through it and add request parameter like:

param1=foo
param2=bar
etc.

This is the closest answer I'm able to provide, if you need more exact advice - please elaborate your question. In the meantime I recommend you to get familiarized with How to use BeanShell: JMeter's favorite built-in component guide to learn about scripting in JMeter and what do pre-defined variables like "sampler" in above code snippet mean.