最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Using variables used in BSF post-processor as a parameter in another sampler - Stack Overflow

matteradmin4PV0评论

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?

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?

Share Improve this question edited Jun 30, 2012 at 10:23 Aliaksandr Belik 12.9k6 gold badges68 silver badges92 bronze badges asked Jun 30, 2012 at 8:09 Abhijeet VaikarAbhijeet Vaikar 1,6566 gold badges28 silver badges53 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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.

Post a comment

comment list (0)

  1. No comments so far