1

I have a BSF post-processor added to a sampler.
The script in the post-processor is:

var array = JSON.parse(prev.getResponseDataAsString());

array.forEach(function(object)
{
  OUT.println("patient_id: "+object.patientId);
  OUT.println("fname: "+object.fname);
  OUT.println("lname: "+object.lname);
});

now i want to use object.patientId, object.fname, object.lname values as parameters in another request's parameters.

E.g.

Thread Group
- Sampler1
    BSF Post-Processor
- Sampler2

I want to use the variables in the BSF Post Processor's javascript of Sampler1 as parameters in Sampler2. Is that possible?

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
Abhijeet Vaikar
  • 1,578
  • 4
  • 27
  • 50

1 Answers1

2

Easily: BSF PostProcessor provides read/write access to Jmeter Variables / Properties:

vars - ( JMeterVariables) - gives read/write access to variables: vars.get(key); vars.put(key,val); vars.putObject("OBJ1",new Object()); vars.getObject("OBJ2");

props - (JMeterProperties - class java.util.Properties) - e.g. props.get("START.HMS"); props.put("PROP1","1234");

In the simplest case you can use

vars.put(patientId,object.patientId.toString());
vars.put(fName,object.fname.toString());
vars.put(lName,object.lname.toString());

in your BSF PostProcessor to set the variables, and then get they values like

vars.get("patientId")

or

${patientId}

But since you are extracting ALL the records in foreach loop at once you cannot use this way.

In this case you have to better use something like the following: write all the records values extracted in foreach loop into csv-file, and then use e.g. CSV Data Set Config to read records one-by-one in loop and use values along with your Sampler2:

While Controller
    CSV Data Set Config
    Sampler 2

...As well, if I find another, better way, I'll be glad to know about it.

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
  • Do you mean that I should write the record values into a csv file directly from the BSF Post Processor using Javascript's File handling methods? Can i use something like this? function WriteToFile() { var fso = new ActiveXObject("Scripting.FileSystemObject"); var s = fso.CreateTextFile("Test.csv", true); s.WriteLine(object.patientId.toString()+','+object.fname.toString()+','+object.lname.toString()); s.Close(); } – Abhijeet Vaikar Jul 03 '12 at 07:20
  • I tried the above code for writing the records into a csv file. I'm using JMeter on Ubuntu, which is why i think it's giving me an error for using ActiveXObject. Is there any other way around to write into a csv file using Javascript? I searched a lot around but couldn't find anything. – Abhijeet Vaikar Jul 03 '12 at 08:19
  • Also, are there any jar files that i separately need to copy into JMeter's folders if I have to use Java as a language in BSF post processor? I tried using Java for the above scene. It says: Problem in BSF script org.apache.bsf.BSFException: unable to load language: java – Abhijeet Vaikar Jul 03 '12 at 09:41
  • Use then [Beanshell PostProcessor](http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_PostProcessor) instead, with beanshell (read: java) code along with it. Should work for out-of-box jmeter installation, without any additional libraries required. – Aliaksandr Belik Jul 03 '12 at 12:46
  • Thanks a lot for suggesting Beanshell PostProcessor. :) I'm able to run Java code in it. Will use that itself to write the records into the csv file and use them with csv dataset config. – Abhijeet Vaikar Jul 03 '12 at 13:10