Discussion:
changing User Defined Variable using script in BeanShell PostProcessor
Grzegorz Kończyk
2008-04-07 14:01:58 UTC
Permalink
My TestPlant looks like this:

TestPlant
|-User Defined Variable
|-HTTP Request
|-BeanShell PostProcessor

TestPlan settings are:
Number of threads: 1
Ramp-up: 1
Loop: 1

In User Defined Variable I have 2 variables defined:
offer = 100
step = 2

In BeanShell PostProcessor I have following script (print functions are only for "debugging"):

print( ${offer} );
int of = Integer.parseInt( vars.get("offer") );
int st = Integer.parseInt( vars.get("step") );
of = of + st;
String newVal = Integer.toString( of );
print( newVal);
vars.put( "offer", newVal );
print( ${offer} );

The console output (black cmd window opened while starting JMeter) shows me following results:

100
102
100 - here is the error - why it's not 102 here?

AFAIK, vars.put( "offer", newVal ); should overwrite old value of "offer" (100) with the new value (102), but it doesn't.

I tried also another way:


import org.apache.jmeter.util.*;

print( ${offer} );
int of = Integer.parseInt( vars.get("offer") );
int st = Integer.parseInt( vars.get("step") );
of = of +st;
String newVal = Integer.toString( of );
print( newVal);
JMeterUtils.setProperty( "offer", newVal );
print( ${offer} );

But it also gives me:

100
102
100 (why not 102 ??)

Any ideas?
sebb
2008-04-07 14:16:36 UTC
Permalink
Post by Grzegorz Kończyk
TestPlant
|-User Defined Variable
|-HTTP Request
|-BeanShell PostProcessor
Number of threads: 1
Ramp-up: 1
Loop: 1
offer = 100
step = 2
print( ${offer} );
The variable reference is substituted before the script is run.
Post by Grzegorz Kończyk
int of = Integer.parseInt( vars.get("offer") );
int st = Integer.parseInt( vars.get("step") );
of = of + st;
String newVal = Integer.toString( of );
print( newVal);
vars.put( "offer", newVal );
print( ${offer} );
The variable reference is substituted before the script is run.

Try print(vars.get("offer"));
Post by Grzegorz Kończyk
100
102
100 - here is the error - why it's not 102 here?
AFAIK, vars.put( "offer", newVal ); should overwrite old value of "offer" (100) with the new value (102), but it doesn't.
Yes it does.
Post by Grzegorz Kończyk
import org.apache.jmeter.util.*;
print( ${offer} );
int of = Integer.parseInt( vars.get("offer") );
int st = Integer.parseInt( vars.get("step") );
of = of +st;
String newVal = Integer.toString( of );
print( newVal);
JMeterUtils.setProperty( "offer", newVal );
print( ${offer} );
100
102
100 (why not 102 ??)
Any ideas?
See above.
Post by Grzegorz Kończyk
---------------------------------------------------------------------
For additional commands, e-mail: jmeter-user-h
Grzegorz Kończyk
2008-04-07 15:12:00 UTC
Permalink
Post by sebb
The variable reference is substituted before the script is run.
Oh man, that was missing link in my knowledge ;)
So I was doing ok, but I wasn't able to check it.

Anyway, thanks sebb for the answers and the patience ;)

Loading...