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 Answers
Easy way
You can use chooseRandom function available via JMeter Plugins
Hard way
Use a Beanshell PreProcessor to get a random value as follows:
- Add a Beanshell PreProcessor as a child of the request, which parameter you need to randomize
- Put your
value1,value2,value3,value4,value5into the PreProcessor's "Parameters" input 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);- Refer generated value as
${randomValue}where required.
See the following reference information for
- Easy way: Installing JMeter Plugins
- Hard way: How to use BeanShell: JMeter's favorite built-in component
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 :)