JMeter set variable to random option - jmeter

I've been using JMeter and I'm aware of the __Random and __RandomString functions. I need to pick a random option and store it in a variable because it will be used as part of a parameter path for multiple calls. For example:
http://www.example.com/pets/{random option such as: cat, dog, parakeet}/
I've tried doing simple like this, where I set the variable ${query} to one, two, or three using a random controller with userdefined variables as children. This seems like it should work, however I always get ${query} set to three.
Any insight or ideas are will be well recieved. Thanks to all in advance.

You can use Beanshell Pre Processor to generate random value
String[] query = new String[]{"cat", "dog", "parakeet"};
Random random = new Random();
int i = random.nextInt(query.length);
vars.put("randomOption",query[i]);
After that in your HTTP Request
http://www.example.com/pets/${randomOption}
As an alternative to String[] query = new String[]{"cat", "dog", "parakeet"}; you can use Beanshell pre-defined Parameters stanza.
Random random = new Random();
int i = random.nextInt(query.length);
vars.put("randomOption",bsh.args[i]);

I know this is an old post and there is a new function available:
__RandomFromMultipleVars(animalCat|animalDog|animalParakeet, query)
somewhere you need to define the variables:
animalCat=cat
animalDog=dog
animalParakeet=parakeet

It looks like this is not a feature of Jmeter natively. I'm using a plugin that accomplishes this goal. http://jmeter-plugins.org/wiki/Functions/ implements a new function that lets you choose a random string from a list of strings. From their website:
${__chooseRandom(red,green,blue,orange,violet,magenta,randomColor)}

See also:
Get random values from an array
This however requires writing some code in a PreProcessor.

Related

How to pass a 2-D array from one thread to another thread in Jmeter

So I am using a bean shell post processor and towards the end I am storing the result in a 2-D array. Result is stored this way.
testArray[0][0] = "1"
testArray[0][1] = "Test"
testArray[1][0] = "2"
testArray[1][1] = "STG"
My requirement is that I need to pass this 2-D array to the next thread. How am I supposed to proceed ?
First of all since JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting so consider migrating to Groovy.
Coming back to your question just use props shorthand like:
In first thread:
props.put("testArray", testArray);
in the other thread:
testArray = props.get("testArray");
you might also want to add the current thread number as the prefix/postfix so different threads could have different arrays with separate values like:
props.put("testArray_" + ctx.getThreadNum(), testArray);
where ctx stand for JMeterContext class instance
More information on these ctx, props and other JMeter API shortcuts available for JSR223 Test elements: Top 8 JMeter Java Classes You Should Be Using with Groovy

Jmeter - Calling javascript using JSR223 Post processor

I capture 6 elements using a regular expression. say
Variable : UserDetails
Regular Expression : loadHeadWorkFlow\('(.+?)','(.+?)','(.+?)','(.+?)','(.+?)','(.+?)','/I
Template : $1$$2$$3$$4$$5$$6$
Now I could access these values via UserDetails_g1, UserDetails_g2.....UserDetails_g6
Next, these 6 values need to be encrypted using a javascript file. The file contains the logic.
How should my code be using JSR223 post processor?
The steps that I followed:
1.
load('Encryption.js');
var result = encrypt("${UserDetails_g1}","password");
log.info("encrypted value is "+result);
vars.put("LoginDataString",result);
var result1 = encrypt("${UserDetails_g2}","password1");
vars.put("UserId",result1);
var result2 = encrypt("${UserDetails_g3}","password2");
vars.put("RoleId",result2);
First value is encrypted correctly. But the other values aren't correct. If I add individual post processors for every variable. All the encrypted values show correctly.
Is there a way where I could use a single post processor to perform all the 6 encryptions. Thanks in advance
Regards,
Ajith
Use vars instead of ${} syntax
var result = encrypt(vars.get("UserDetails_g1"),"password");
log.info("encrypted value is "+result);
vars.put("LoginDataString",result);
var result1 = encrypt(vars.get("UserDetails_g2"),"password1");
vars.put("UserId",result1);
var result2 = encrypt(vars.get("UserDetails_g3"),"password2");
vars.put("RoleId",result2);
From JMeter perspective there is no problems, just check that your UserDetails_g2 variable has anticipated value using Debug Sampler and View Results Tree listener combination., the you might want to check this encrypt() function implementation.
Another possible reason is this JavaScript language selection itself, the Nashorn engine performance is a big question mark when it comes to the load, according to JMeter Best Practices it's recommended to use Groovy language for scripting so you might want to consider re-writing the function in Groovy

Nested Parameter substitution problems in jmeter

I am trying to create a jmx to test some database entries. The exact queries are not known beforehand and need to be picked from one of many sets of queries, that I have declared in separate pre-processor units.
For eg. one of the set of queries is:
String[][] animals = new String[][]{
{"VLS_CATS_ASSOC","CAT_ID_AS = '${CAT_ID_AS}'"},
{"VLS_DOGS_EXCH","DOG_ID_ST = '${DOG_ID_ST}' and DOG_TXT_REF = '${DOG_TXT_REF}'"},
};
The 2D array has two entries, the 'table name' and the 'where clause'
So, the jdbc request is select * from ${table_name} where ${where_clause}
I've set up a loop controller for iterating through the tables one by one, and as a child i have jdbc sampler, that has the csv table config which will contain data for CAT_ID_AS, DOG_ID_ST, DOG_TXT_REF.
Currently, when i see my requests through a results listener, i see that the queries sent are:
select * from VLS_CATS_ASSOC where DOG_ID_ST = '${DOG_ID_ST}' and DOG_TXT_REF = '${DOG_TXT_REF}'
It is clear from the output that the first level of substitution has worked, but not the second one.
Can anybody please help me on this?
Edit: Adding an image of the test plan. Jmeter Test Plan
Don't use ${CAT_ID_AS} notation in (I presume) the BeanShell strings: while it is generally working, that's an undocumented and error-prone feature.
The legit way is vars.get("CAT_ID_AS")
vars is a pre-defined object available in any BeanShell piece inside your test plan
So your code would be looking like
String[][] animals = new String[][]{ {"VLS_CATS_ASSOC","CAT_ID_AS = '" + vars.get("CAT_ID_AS") + "'"}, ... etc

How to capture thread specific different data form drop down list in jmeter?

My application consist the Select Title drop down list contains values as Mr, Miss, Dr & Mrs.
I want to capture the different title (Random but from above 4) for different thread. please suggest how it is possible.
This is my script i have pass the title parameter as ${randomTitle}
Value pass to database as,
Post request as,
Out of interest, is it vital to use enum there?
Try amending your code as follows:
import java.util.Random;
String[] frm_titles = {"Mr", "Miss", "Dr", "Mrs"};
Random randGenerator = new Random();
int randInt = randGenerator.nextInt(frm_titles.length);
vars.put("randomTitle",frm_titles[randInt]);
It should work this way. Enum must not be local to Beanshell interpreter, if you need to use enum structure - compile it as .jar and place it to JMeter classpath.
See How to use BeanShell: JMeter's favorite built-in component guide for more details on Beanshell scripting in Apache JMeter.
You can use Beanshell Preprocessor:
import java.util.Random;
public enum frm_titles {"Mr", "Miss", "Dr", "Mrs"};
Random randGenerator = new Random();
int randInt = randGenerator.nextInt(frm_titles.values().length);
vars.put("randomTitle",frm_titles.values()[randInt].toString());
Then, in your test plan change the post params and add ${randomTitle} instead your title param.
Check this links for more info:
http://www.beanshell.org/manual/quickstart.html
http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_Sampler
http://jmeter.apache.org/usermanual/functions.html
http://testeverythingqtp.blogspot.com.es/2013/01/jmeter-bean-shell-script-create-file.html
http://jmeter-kh.blogspot.com.es/2009/07/how-to-make-beanshell-work-in-jmeter.html
I think 2 ways are possible,
you can try beanshell processor
you can try regular expression extractor and counter
first approach is best explained above, for 2nd approach you can try,
Random product selection using Jmeter

Jmeter while controller doesn't seem to evaluate variables as numbers

I am writing a jmeter script that keeps loading data until a table reaches a specified size. I have a while loop, in which I have one HTTP Sampler to loads the data, then another HTTP Sampler with an XPath Post-processor to check the table size (they call two different APIs). The reference variable of the XPath Post Processor is currentSize and I have a user defined variable maxSize, but using ${currentSize} < ${maxSize} as the condition for the while loop creates an infinite loop.
Thinking maybe the problem is that the output of XPath is a string, I've tried doing various things in beanshell to coerce it to a number, but I'm a beanshell noob, so I haven't been successful with that either. Can anyone guide me about how to get jmeter to recognize a variable as a number? (Preferably a decimal, but if I have to round to an int, I can live with that.)
Thanks!
I think using __javascript(parseInt()) should suffice for you to check the condition.
e.g.
${__javaScript(parseInt(${time_elapsed_string}) < parseInt(${duration}))}
Assuming that you have following variables:
currentSize
maxSize
continue
where continue is set via User Defined Variables and has the value of true
You can use following Beanshell code to check if current size is equal or greater than maximum size:
import java.math.BigDecimal;
String currentSize = vars.get("currentSize");
String maxSize = vars.get("maxSize");
BigDecimal currentSizeNumber = new BigDecimal(currentSize);
BigDecimal maxSizeNumber = new BigDecimal(maxSize);
if (currentSizeNumber.compareTo(maxSizeNumber) > -1){
vars.put("continue", "false");
}
Make sure that following criteria are met:
Your While Controller has ${continue} as a condition
Beanshell Sampler, Pre / Post Processor or Assertion with the code above is added as a child of the While Controller
See How to use BeanShell guide for more details and kind of Beanshell cookbook.
Everything should work this way.
Hope this helps.

Resources