5

I want to pass some parameters to my request body. I don't want to send the random string. But I need to pass the random values from selected values. Ex; I have 5 values, say value1,value2,value3,value4,value5. I need to pass the random values among those five values. How can I do that? Any suggestions Please

Compass
  • 5,867
  • 4
  • 30
  • 42
Vinod
  • 2,263
  • 9
  • 55
  • 104
  • possible duplicate of http://stackoverflow.com/questions/22366723/random-product-selection-using-jmeter/22375601#22375601 – user3239835 Nov 26 '14 at 08:16

3 Answers3

8

Easy way

You can use chooseRandom function available via JMeter Plugins

Hard way

Use a Beanshell PreProcessor to get a random value as follows:

  1. Add a Beanshell PreProcessor as a child of the request, which parameter you need to randomize
  2. Put your value1,value2,value3,value4,value5 into the PreProcessor's "Parameters" input
  3. Put the following code into the PreProcessor's "Script" area

    String[] params = Parameters.split(",");
    Random random = new Random();
    String randomValue = params[random.nextInt(params.length)];
    vars.put("randomValue", randomValue);
    
  4. Refer generated value as ${randomValue} where required.

See the following reference information for

  1. Easy way: Installing JMeter Plugins
  2. Hard way: How to use BeanShell: JMeter's favorite built-in component
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • 1
    It is available in Standard Set so JMeterPlugins-Standard-1.2.0.zip should be enough. For current plugins version (1.2.0) direct URL is http://jmeter-plugins.org/downloads/file/JMeterPlugins-Standard-1.2.0.zip – Dmitri T Nov 26 '14 at 09:25
  • 1
    Make sure that you **extracted** it in the root of your JMeter installation. Alternatively double-check that `JMeterPlugins-Standard.jar` file is present in /lib/ext folder. JMeter restart is required so JMeter could pick the plugins up. No more configuration needed, new functions are available at the same location where built-in functions are. – Dmitri T Nov 26 '14 at 09:37
  • Use it in the request directly – Dmitri T Nov 26 '14 at 09:45
  • Each time function is called it'll return a random value. – Dmitri T Nov 26 '14 at 09:59
  • Exactly, but I need same value in both requests. So Is there any way to store the return value in user defined variables and use that variable in both requests. – Vinod Nov 26 '14 at 10:01
  • The **last** argument is the mandatory variable which holds the result. I.e. if you populate the function as `${__chooseRandom(value1,value2,value3,value4,value5,randomValue)}` result will be stored into `randomValue` variable which can be re-used in any number of samplers – Dmitri T Nov 26 '14 at 10:12
  • I got some issue, Please give some suggestion, http://stackoverflow.com/questions/27153288/javax-net-ssl-sslexception-in-jmeter – Vinod Nov 26 '14 at 15:45
4

Say you have a variable foo and it can be a b or c and you want the sampler to select a random value from that set.

Create user-defined variables foo1=a foo2=b foo3=c

Then, use the following in your http request defaults:

${__RandomFromMultipleVars(foo1|foo2|foo3)}

and voila uniform random selection with no need for scripting or plugins

EDIT: if youre in to javascript, consider this one-liner: ${__javaScript(Array("a"\, "b"\, "c")[Math.floor(Math.random()*3)])} ^^that creates an array of three elements and returns a random element from within. No variables, no problem :)

nachonachoman
  • 802
  • 1
  • 13
  • 29
0

how to pass the specific values not the random values, i.e for dynamic values how can we achieve this

Everest
  • 41
  • 1
  • 8
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Halo May 29 '22 at 16:25