How to access Javascript function using JSR223 Sampler in JMeter? - jmeter

I have a Javascript function in JSR223 Sampler and I would like to call this function in my HTTP Request sampler. Any idea how to call the function?
Example:
I know I can do something like this and use the "total" variable from my HTTP Request Sampler.
var total;
function sum(a, b){
return a+b;
};
vars.put("total", sum(2,4));
But is there a way to call the function sum(a,b) directly from HTTP Request sampler? If not, is there any other way to do this? A sample example will really help me a lot.

First of all, are you aware of __intSum() function which does exactly what you're trying to achieve, the syntax would be:
${__intSum(2,4,total)}
If you still want to proceed with coding:
Don't use JavaScript as it might become a performance bottleneck. Since JMeter 3.1 it is strongly recommended to use Groovy for scripting.
Create a file, i.e. sum.groovy in "bin" folder of your JMeter installation and put the following code into "Script" area:
int sum (int a, int b) {
return a + b;
}
Run JMeter as follows:
jmeter -Jgroovy.utilities=sum.groovy
If you want to make the change permanent - add the relevant line to user.properties file
groovy.utilities=sum.groovy
See Apache JMeter Properties Customization Guide for more detailed information on setting and overriding JMeter properties
You will be able to sum numbers with __groovy function like:
${__groovy(sum(2\, 4),total)}

Related

Assigning value to the variable based on the request

I have created a jmeter script as below
I am using the user defined variable with function __P() and passing the Url from .sh file
My requirement is, if the url passed is "www.abc.com" then set the value of the variable ${Prefix} to "foo" else set it to "bar"
I tried using JSR223 PostProcessor, but JSR223 PostProcessor has to have a sample associated with it.
Any suggestion how do I achieve it?
Use the following __groovy() function in the HTTP Request sampler directly:
${__groovy(if (vars.get('Url') == 'www.abc.com') {return 'foo'} else {return 'bar'},Prefix)}
For subsequent requests (once the __groovy() function has been run) you will be able to use just ${Prefix}
More information: Apache Groovy - Why and How You Should Use It
If you're not too comfortable with Groovy scripting you can consider using __if() function, it's a part of Custom JMeter Functions bundle, can be installed usign JMeter Plugins Manager

How to use StringFromFile function within a JSR223 Sampler?

I can't find any good docs/examples on how to use the StringFromFile function to get multiple records from a source data file from within a JSR223 Sampler. Could somebody please help me?
My code which doesn't work:
String filename = "filename.csv";
String record = StringFromFile(filename,"id");
You shouldn't be using it directly in the scripts, as per JSR223 Sampler documentation:
When using this feature, ensure your script code does not use JMeter variables directly in script code as caching would only cache first replacement. Instead use script parameters.
So the solution would be using File.readLines() function which reads the file into a List of strings
More information: The Groovy Templates Cheat Sheet for JMeter

jmeter beanshell call jmeter function

I have a HTTP request sampler configured. In the request body, I call out to a beanshell function I wrote:
${__BeanShell(createHeader("GET"\,"Customer"\,"${__UUID}"\,"${__time(yyyy-MM-dd'T'hh:mm:ss)}"))}
The function just builds some request strings with the passed-in parameters. I want to remove the jmeter function calls (__UUID and __time), and call them directly from within the beanshell function. I could not find a way to do that.
Thanks
Don't inline JMeter Functions or Variables into scripts, in your case you will have to go for code-based equivalents instead, to wit:
__UUID() -> UUID.randomUUID().toString()
__time() -> new java.text.SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss").format(new Date())
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting, in your case it would be __groovy() function. If you want to re-use createHeader method - you can put it into a separate .groovy file and define groovy.utilities property pointing to this file.
See Apache Groovy - Why and How You Should Use It article for more information on using Groovy scripting in JMeter tests.

How pass the variable from bean shell sample to other request

import java.util.Random;
Random r = new Random();
int low = 1;
int high = 4;
int randomvalue = r.nextInt(high-low) + low;
log.info("-------------------------");
String Value="10000"+randomvalue+"A";
log.info(Value);
How to pass the data present in the Value variable to other requests in JMeter?
Change Beanshell to JSR223 Sampler and add value to JMeter variable
vars.put("Value", Value);
You can call it later as ${Value} or inside JSR223 Sampler as vars.get("Value")
Or save using JMeter's property (effect all threads)
props.put("Value", Value);
You can call it later as ${__P(Value)} or inside JSR223 Sampler as props.get("Value")
It is recommended to use JMeter built-in test elements where possible, your requirement can be implemented via __Random() and __intSum() functions combination. Both provide possibility to store the generated value into a JMeter Variable. Check out Apache JMeter Functions - An Introduction article to learn more about JMeter Functions concept.
If you absolutely have to go for scripting be aware of vars shorthand for JMeterVariables class instance, it provides read/write access to JMeter Variables so you can put the generated value into i.e. ${foo} JMeter variable as:
vars.put("foo", Value);
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so consider migrating to Groovy on next available opportunity.

how can i pass dynamic values to url in jmeter

I have to give dynamic values to url which takes number of users and their age , which can be selected though web page. but I want to give it in Jmeter using BeanShell PostProcessor.
Help me in this,since I'm new to Jmeter.
This is the path:
/destinations/packages?airports%5B%5D=LGW&units%5B%5D=000577%3ADESTINATION&when=29-05-2016&until=&flexibility=true&flexibleDays=3&noOfAdults=2&noOfSeniors=0&noOfChildren=1&childrenAge=3&duration=7114&first=0&searchType=search&searchRequestType=ins&sp=true&multiSelect=true
from what I've got looks like you can use CSV Data Set Config.
Create .txt file with the data you want to feed your test with;
Place the above .txt file to the folder where your .jmx file lies;
In your Test plan: Under your request sampler - place CSV Data set Config;
Then if you need to use your dynamic values within one threadgroup => refer these data as ${quanity}, ${age} in you url.
If you need to pass these values across the threadgroups => add BeanShell Assertion
Then (in the other Tread group) refer those as ${__property(_quantity)},${__property(_age)}.
Hope, it helps.
First of all you need Beanshell PreProcessor, not Beanshell PostProcessor.
All this parameters are basically name/value pairs which can be defined via HTTPSamplerBase class. HTTPSamplerBase class instance is available to Beanshell PreProcessor as sampler pre-defined variable so if you add the following code into the Beanshell PreProcessor "Script" area
sampler.addEncodedArgument("airports[]","LGW");
sampler.addEncodedArgument("units[]","000577:DESTINATION");
sampler.addEncodedArgument("when","29-05-2016");
sampler.addEncodedArgument("until","");
sampler.addEncodedArgument("flexibility", "true");
sampler.addEncodedArgument("flexibleDays","3");
sampler.addEncodedArgument("noOfAdults","2");
//etc
Your HTTP request will be populated with the values, you set via Beanshell.
JMeter Variables can be accessed via vars shorthand which stands for JMeterVariables class instance.
String airport = vars.get("airport");
sampler.addEncodedArgument("airports[]", airport);
//etc
See How to Use BeanShell: JMeter's Favorite Built-in Component article for comprehensive information on how to use Beanshell Test Elements in Jmeter.
Remember that it is recommended to avoid scripting where possible so if there is another way of implementing your task - go for it.

Resources