2

While recording a test, when I select a checkbox then it returns 'On' as value. I have made my test plan in JMeter and now I need to randomly set value to this field as On or OFF on every request.

Can I achieve it using Beanshell scripting + Split or Random function?

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Satyajit
  • 152
  • 10

3 Answers3

3

You can use __RandomFromMultipleVars function to random between values

The RandomFromMultipleVars function returns a random value based on the variable values provided by Source Variables

If you put ON and OFF inside variables, e.g. inside on and off JMeter variables, then you can use:

${__RandomFromMultipleVars(on|off, rnd)}

It will save the random value inside rnd variable (second parameter) that can be used later as ${rnd}

Another option to get same result is to add the following JMeter variables:

a_matchNr = 2
a_1 = on
a_2 = off

Then use function with JMeter variable prefix a:

${__RandomFromMultipleVars(a, rnd)}
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • Can I use ${__RandomFromMultipleVars(on|off, rnd)} by directly inserting the values instead of defining them as variables before, if yes how we can do it ?? – Satyajit Aug 14 '18 at 07:12
  • 1
    @Satyajit no, you can't, this function only get values from variables – Ori Marko Aug 14 '18 at 07:13
  • I have written this code ---> ${__split(${String1},value,|)} ${__RandomFromMultipleVars(value_1|value_2, rememberMe)}.....So now in this its extracting values from String1 and storing it as value_1, value_2,...value_n...now I have to use this value randomly and as these values are large in number I don't want to specify these in the __RandomFromMultipleVars function, so I am looking for a way to use these two functions in one line so that __RandomFromMultipleVars function will automatically get the values that the __split is creating. – Satyajit Aug 14 '18 at 07:20
  • 1
    @Satyajit You are just missing adding JMeter variable `value_matchNr` with value `2` – Ori Marko Aug 14 '18 at 07:30
  • Actually, I want to use all this in one line, instead of specifying value_1, value_2 which I have done right now, I want to specify the array values generated by the split function. In short, I want to use split function in the random function and want the code in one line..Your help will be appreciated.. – Satyajit Aug 14 '18 at 07:35
  • At last got it after running it multiple times, we can write it as - ${__RandomFromMultipleVars{__split(${String1},value,|)}, rememberMe)}. Now even if the string will generate a 100 values after splitting, these values are passed to the __RandomFromMultipleVars which accept only variables value. – Satyajit Aug 14 '18 at 08:17
  • Thanks a lot for the help. – Satyajit Aug 14 '18 at 08:18
1

Starting from JMeter version 3.1 users are encouraged to switch to Groovy from Beanshell mainly because Groovy has much better performance comparing to other scripting options.

So you can do this using __groovy() function like:

${__groovy(['On'\,'OFF'][new Random().nextInt(2)],)}

Demo:

JMeter Random Groovy

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • At last got it after running it multiple times, we can write it as - ${__RandomFromMultipleVars{__split(${String1},value,|)}, rememberMe)}. Now even if the string will generate a 100 values after splitting, these values are passed to the __RandomFromMultipleVars which accept only variables value. @Dmitri T - Thanks a lot for the help. – Satyajit Aug 14 '18 at 08:19
0

At last got it after running it multiple times, we can write it as - ${__RandomFromMultipleVars{__split(${String1},value,|)}, rememberMe)}. Now even if the string will generate a 100 values after splitting, these values are passed to the __RandomFromMultipleVars which accept only variables value.

Satyajit
  • 152
  • 10