2

I have this simple object:

var apple = {
    type: "macintosh",
    color: "red",
    getInfo: function(int){
        return "test" + int;
    }
}

In Jmeter I want to put this object into a global variable that allows me to access this object.

I tried:

vars.putObject("test",apple); (In a pre-processor, so before all the assertions)

var test = vars.getObject("test"); (In all the assertions)

But it seems that the function is casted as string and therefore I can't use it in my assertions.

How to make this work?

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Simo
  • 698
  • 1
  • 5
  • 18

2 Answers2

1

If you are looking for a "global" solution you need to consider JMeter Properties instead of JMeter variables, to wit use props shorthand instead of vars. As per Sharing Variables user manual chapter:

The get() and put() methods only support variables with String values, but there are also getObject() and putObject() methods which can be used for arbitrary objects. JMeter variables are local to a thread, but can be used by all test elements (not just Beanshell). If you need to share variables between threads, then JMeter properties can be used

For example in one test element:

props.put('test', apple)

In another one (can be in another Thread Group as well)

var apple = props.get('test')
log.info(apple.getInfo(1))

JMeter Properties across thread groups.

Also be aware that starting from JMeter 3.1 it is recommended to use Groovy language for any form of scripting as Groovy performance is much better than other scripting options, check out Apache Groovy - Why and How You Should Use It guide for more details.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

In JMeter you can use Java language which you can add Object apple

public class apple {
    String type = "macintosh";
    String color = "red";
    public String getInfo(){
        return "test";
    }
};
var a = new apple();

vars.putObject("a",a);

And get it later and use its methods:

var a = vars.getObject("a");
log.info(a.getInfo());

Also you can create Java classes with groovy

Ori Marko
  • 56,308
  • 23
  • 131
  • 233