0

I'm very new at JMeter issues.

In a test script i have a BeanShell PreProcessor element that updates some variables previously defined at a "User Defined Variables" element.

Latter those variables are used in "Http Requests". However, the value that is used in the http request is the default one.

The scripts seems to be working due to some debug print();

My question is if it's necessary to delay the script to be sure that the BeanShell finishes?

Thanks a lot for your attention

user3022628
  • 1
  • 1
  • 1
  • 2

3 Answers3

2

There is no need to put any delay to Beanshell Pre-Processor as it's being executed before request. I'd recommend to check your jmeter.log file to see if there are any scripting issues as Beanshell Pre-Processor does not report errors anywhere including View Results Tree listener.

There are at least 2 ways to assure that everything is fine with your Beanshell script:

  1. Put your debug print code after variables replace logic to see if it fires
  2. Use JMeter __Beahshell function right in your HTTP request. If it's ok - View Results Tree will demonstrate beanshell-generated value. If not - the field will be blank and relevant error will be displayed in the log.

Example test case:

Given following Test Plan structure:

  • Thread Group with 1 user and 1 loop
  • HTTP GET Request to google.com with path of / and parameter q

If you provide as parameter "q" following beanshell function:

${__BeanShell(System.currentTimeMillis())}

and look into View Results Tree "Request" tab you should see something like:

GET http://www.google.com/?q=1385206045832

and if you change function to something incorrect like:

${__BeanShell(Something.incorrect())}

you'll see a blank request.

The correct way of changing existing variable (or creating new if variable doesn't exist) looks like

vars.put("variablename", "variablevalue");

*Important: * JMeter Variables are Java Strings, if you're trying to set something else (date, integer, whatever) to JMeter Variable you need to cast it to String somehow.

Example:

int i = 5;
vars.put("int_i", String.valueOf(i));

Hope this helps.

Dmitri T
  • 119,313
  • 3
  • 56
  • 104
0

You can update the vale of a "user defined variable".

  1. You have to create a bean shell sampler
  2. vars.put("user_defined_variable", "newvalue");

@theINtoy got it right.

http://www.blazemeter.com/blog/queen-jmeters-built-componentshow-use-beanshell

  • Make sure the variable that you defined does not have an initial value, otherwise it will refresh it in the next iteration. – Armando Sep 28 '17 at 20:16
-2

I'm new to jmeter too but as I know variables defined in "User defined variables" are constants, so you can't change them. I recommend to use "User Parameters" in preprocessors or CSV Data Set Config.

DAC84
  • 1,104
  • 1
  • 18
  • 30
  • 1
    This is not true. It is possible to override the value of a user defined variable using a BeanShell PreProcessor. See:[Changing JMeter Variables on the fly](http://www.blazemeter.com/blog/queen-jmeters-built-componentshow-use-beanshell) Essentially: Assuming that you have a User Defined Variable called "continue" with the value of "true" which is being used somewhere in the While loop. You can set it to "false" as simple as follows: `vars.put("counter","false");` – theINtoy May 14 '14 at 13:42