nurub
nurub

Reputation: 355

Passing global variables between Thread Groups

I'm new to jmeter and I'm trying to play around to learn how to pass global variables from one Thread Group to another I built this small script enter image description here

In User Defined Variables, I created 1 variable like this: enter image description here

In JSR223 Sampler 1, I added the following code:

log.info("access_token before 1 = " + vars.get("access_token"))
vars.put("access_token", "aaaaa")
log.info("access_token after  2 = " + vars.get("access_token"))

In JSR223 Sampler 2, I added the following code:

log.info("access_token 2 = " + vars.get("access_token"))

I got the following log entries related to my code:

2024-04-26 21:08:00,241 INFO o.a.j.p.j.s.J.JSR223 Sampler 1: access_token before 1 = 
2024-04-26 21:08:00,241 INFO o.a.j.p.j.s.J.JSR223 Sampler 1: access_token after  2 = aaaaa
2024-04-26 21:08:00,273 INFO o.a.j.p.j.s.J.JSR223 Sampler 2: access_token 2 = 

I was expecting to see the right value of access_token variable inside thread group 2 and see it's value in the log but it was not the case as shown in the logs above.

Can someone please help me out here and show me what I'm missing in my tiny script. Thank you

Upvotes: 0

Views: 169

Answers (2)

nurub
nurub

Reputation: 355

The trick was to use props instead of vars. It worked.

Upvotes: 0

Ivan G
Ivan G

Reputation: 2707

  1. You don't need to declare the access_token in the User Defined Variables, this element is for setting initial set of global variables along with their values. If a variable comes from a Post-Processor or a Sampler - it's being created

  2. As per JMeter Documentation:

    Variables are local to a thread; properties are common to all threads

    so go for props shorthand instead of vars, see Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on what do these and other JMeter API shorthands mean

  3. There is Inter-Thread Communication Plugin which is more handy and flexible

  4. And last but not the least, normally you should not be passing variables between threads and especially thread groups, it might be an issue with your test scenario design

Upvotes: 1

Related Questions