How to use variable "sampler" in JSR223 Sampler (JMeter) - jmeter

I'm finding the way to use the variable sampler in JSR223 Sampler, JSR223 PreProcessor, JSR223 PostProcessor, and all other JSR223 script.
There are some other variables like vars, props, prev, SampleResult. I can use them easily.
For example:
vars: vars.get("VARIABLE_NAME"), vars.put("VARIABLE_NAME","VALUE"), ...
props: props.get, props.put, ...
prev: prev.getTime(), prev.isSuccessful(), prev.getLatency(), ...
SampleResult: SampleResult.getResponseCode(), SampleResult.getResponseMessage(), ...
But I don't know how to use variable sampler. Only thing I can do with this variable is:
sampler.sample(): It helps to returns the Name of current Sampler
So, could anyome please let me know there is any other way to use this variable?
Thanks in advance!

For JSR223 Sampler sampler variable stands for JSR223Sampler, see the JavaDoc for all available methods and fields.
When it comes to JSR223 Pre or Post Processor - in that case sampler variable stands for parent sampler class instance, for instance in case of HTTP Request it will be HTTPSamplerProxy, for JDBC Request - it will be JDBCSampler and so on.
You can check exact class using Groovy expression like:
log.info(sampler.getClass().getName())
You can check How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on pre-defined variables and their usage. It is applicable for Groovy as well.

sampler is a Sampler object. you can use whatever the methods available here, not only methods declared in Sampler class but also the methods in super classes/interfaces like TestElement.
For example:
sampler.sample() - returns sampler's name
sampler.setProperty() - set a property by specifying key, value
sampler.setThreadName() - set thread name for the sampler.

Related

How to use variables within JSR223 preprocessors in jmeter

I am using two JSR223 preprocessors,in which I want to use variable from one preprocessor into another preprocessor.
how can I use this in Jmeter??
Please assist me with an example.
In the first PreProcessor store some value into a foo JMeter Variable using vars shorthand for JMeterVariables class instance like:
vars.put('foo', 'some value')
In the second PreProcessor you can read the value using the same vars shorthand like:
String myString = vars.get('foo')
See Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on this vars shorthand and other JMeter API shortcuts available for the JSR223 Test Elements
The variable can be seen using Debug Sampler and View Results Tree listener combination.

How to pass response from one beanshell sampler to another beanshell sampler in same thread in Jmeter

I am making dme2 call to api from beanshell and i am getting response from it like {"stagedcustomerId":"165ce369-a9fb-4d42-b8f0-f119a6ae20eb"}
so now i want to pass only customer id value to another beanshell sampler for next api call as one of parameter in request body in same thread in jmeter.
Please suggest what can we do in this case. is there any way to do beanshell postprocessor?
You can use SampleResult shorthand in order to define the Beanshell Sampler response data like:
SampleResult.setResponseData("{\"stagedcustomerId\":\"165ce369-a9fb-4d42-b8f0-f119a6ae20eb\"}","UTF-8")
Once done you can add a JSON Extractor as a child of the Beanshell Sampler and configure it like:
That's it, now you will be able to access the extracted value as String id = vars.get("id"); in other Beanshell Sampler or as ${id} in any other test element.
Also be aware that starting from JMeter 3.1 it's highly recommended to use JSR223 Test Elements and Groovy language for scripting so consider refactoring your test on next available opportunity.

JMeter simple BeanShell PreProcessor

I'm new to JMeter BeanShell PreProcessor function.
For a test I'm trying to print sample URL address.
From the tutorial I have the follownig code:
org.apache.jmeter.samplers.SampleResult;
String currentURL = SampleResult.getUrlAsString();
print(currentURL)
But I get error "Attempt to resolve method: getUrlAsString() on undefined variable", how to define this variable first?
This means the SampleResult does not exist.
You need to use prev as per this doc:
http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_PreProcessor
Which references this javadoc:
http://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html#getUrlAsString--
Be aware that since JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language for any form of scripting in JMeter tests. So consider migrating to Groovy as soon as possible.
If you are using a PreProcessor it means that you don't have any SampleResult as sampler has not been yet executed. So you need to use sampler shorthand which is resolved into HTTPSamplerProxy like:
sampler.getUrl() as String
Demo:
More information: Apache Groovy - Why and How You Should Use It

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.

JMeter - do not pass post value in post data if null

In my JMeter script, I have one HTTP request which has 4 different parameters to be passed in post body. I have corresponding variables. Values of these variables are not available every time, depending on configuration.
If a value is not available, I get an error "bad request". How do I see if a variable is not null and only then pass corresponding parameter in request post body?
Given you have the following configuration:
and you don't want to send foo parameter if ${bar} variable is not defined
Add Beanshell PreProcessor as a child of your HTTP Request Sampler
Put the following code into the PreProcessor's "Script" area:
if (vars.get("bar") == null) {
sampler.getArguments().removeArgument("foo");
}
Where:
vars - is a shorthand to JMeterVariables class instance
sampler - shorthand to parent sampler implementation class instance, in this case - HTTPSamplerProxy
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using Java and JMeter API from Beanshell scripts.
Just use the Logic Controller - If Controller. It allows to define the if statement using your variables. So, you can perform your actions only in case all parameters are not equal to null:
I've defined one single User Defined Variable in this example. Jmeter sends HTTP request only if it has a value defined.

Resources