3
votes

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

2

2 Answers

7
votes

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
3
votes

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 :)