How pass the variable from bean shell sample to other request - jmeter

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.

Related

Jmeter using LoopCount instead of the ThreadNum variable(to save variable for every count and use it in another request)

I am setting my Jmeter variable using this using beanshell assertion:
${__setProperty(id_${__threadNum},${id},)}
And using this in another request like this:
${__P(id_${__threadNum},)}
and running the requests like this
However I want to run the loop count instead of the thread. So that i can run the request one by one. How can i change it to so it is loopNum instead of the thread
Can i use this below when i want to use the loop instead of the thread??
I am setting my Jmeter variable using this:
${__setProperty(id_${__iterationNum},${id},)}
And using this in another request like this:
${__P(id_${__iterationNum},)}
For getting the current iteration (1st loop - 1, 2nd loop - 2, etc.):
props.put("id_" + vars.getIteration(), vars.get("id"));
For getting all iterations (each time it will return 10)
props.put("id_" + ctx.getThreadGroup().getSamplerController().getProperty("LoopController.loops"), vars.get("id"));
Few more hints:
Don't inline JMeter Functions or Variables into Scripts, either use "Parameters" section or go for code-based equivalents instead, like in my above examples props stand for JMeter Properties, vars - for JMeter Variables, etc.), see Top 8 JMeter Java Classes You Should Be Using with Groovy article for more details.
Don't use Beanshell for scripting, since JMeter 3.1 you are supposed to use JSR223 Test Elements and Groovy language

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 use array object value in next thread group using bean-shell assertion?

I am using BeanShell assertion to passing value to next thread group. Now I wanted to use that value in next thread group. I am using set Property function and I can see in logs those values stored in variable.
Given you set some property value via __setProperty() function like:
${__setProperty(foo,bar,)}
you can read it using __P() function like:
${__P(foo,)}
If you want to fetch the value in a script - you can use props shorthand like:
String myValue = props.get("foo");
Also be aware that starting from JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting so consider migrating to JSR223 Assertion. If you're setting the property value from the script - don't inline JMeter Function, use the aforementioned props shorthand instead like:
props.put("foo", "bar"); // this creates "foo" property with the value of "bar"
You should be able to re-use the same code. Check out The Groovy Templates Cheat Sheet for JMeter article which covers your use case in particular and few more common tasks in general for more details.

How to use variable "sampler" in JSR223 Sampler (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.

How to share the Integer type of value in jMeter

I wrote a Script in Test Case inside BeanShell PreProcessor and I want to use the value(Integer) in ThreadGroup.
By using vars.put() we can only share String type of values.
NOTE: I want to use the value in Number of Threads(users) block
There are at least 3 ways of doing this:
Use vars.putObject() - places an arbitrary Object into JMeterVariables
Cast Integer to String vars.put("foo", String.valueOf(bar));
Use bsh.shared namespace like:
bsh.shared.myInt = 15
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on Beanshell scripting in JMeter tests
NOTE! you won't be able to amend number of threads in the current Thead Group using Beanshell test elements.

Resources