Jmeter - Calling javascript using JSR223 Post processor - jmeter

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

Related

How to pass ${__threadNum} value in selenium javascript?

I used Webdriver sampler and write selenium -Javascript. Now in my script for achieving some result we need total number of users so I want to know how add ${__threadNum} value in my script?
or is there any way when multiple users run and some operations execute for only one user?
Either put the ${__threadNum} function to the "Parameters" section and refer it in the script as WDS.args[0] where required
var threadNum = WDS.args[0]
Or use WDS.ctx shorthand for JMeterContext class, there you will have access to getThreadNum() function. It will return zero-based number of thread so to get the same value you will need to increment it by 1
var threadNum = (WDS.ctx.getThreadNum() + 1)
Demo:
More information: The WebDriver Sampler: Your Top 10 Questions Answered article for more information if needed

Listing every one of the variables stored in JMeterVariables

I'm making extensive use of JMeterVariables/vars in my current JMeter code- in particular I'm generating variable names dynamically, based on JSON input.
Is there any way of getting the program to kick out a list of every single variable that is stored in vars??
Thanks!
You can do one of the following:
Add Debug Sampler in your script, with JMeter Variables = true
Similarly, add Debug Post-Processor under the sampler which creates dynamic variables
Since vars is just a regular Map, you can iterate it using one of the scripting languages (BeanShll, Groovy, etc):
for(java.util.Map.Entry<java.lang.String,java.lang.Object> var in vars.entrySet())
{
log.info(var.getKey() + "=" + var.getValue().toString());
}
Note that some variables are objects, so their representation will depend on how toString() function is implemented for them.
Add JSR223 Sampler to your Test Plan
Put the following code into "Script" area:
vars.entrySet().each { var ->
log.info(var.getKey() + '=' + var.getValue())
}
All the variable names and values will be printed to jmeter.log file
More information: Apache Groovy - Why and How You Should Use It

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.

JMeter set variable to random option

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.

Resources