Put value in parameter using beanshell and jmeter - jmeter

I have a question about a very basic script that I wrote,
I just want to get data from DB, put it in a variable using beanshell sampler.
and in the end of the thread group to create another bean shell and check the value of this variable.
the problem is at saving the data to a variable (var name is before), when I use the props.put command but the value still null.
Can someone please advise?

If you want to store a value into JMeter Variables you should amend your code like:
vars.put("before", String.valueOf("budgetInt"))
System.out.println("Before is " + vars.get("before"));
Once done you should be able to access the defined variable as ${before} or ${__V(before)} where required.
If you amend your property setting like:
props.put("Before", String.valueOf(budgetInt))
You should be also able to access the value as ${__P(Before,)}
Also consider switching to JSR223 Sampler and Groovy language.

var before isn't assign and therefore it's null.
You should get value as:
var before = props.get("Before");

Related

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 do I use a JMeter Variable declared in an extractor in a User Defined Variable config

I have an http request that uses an extractor to set a JMeter variable (lets call it test) from the body. When I look at the debug controller I can see this works fine. Next I want to append something to the beginning of the variable so I add a user defined variable node and add a variable with the name new and I set the value to ${test}. However when I look in the debug response I see ${test} instead of the value.
I tried the same thing setting the value manually in 2 different UDV nodes and it works fine, so how do I append to a JMeter variable declared in an extractor?
As per JMeter Documentation:
The User Defined Variables element lets you define an initial set of variables, just as in the Test Plan.
Note that all the UDV elements in a test plan - no matter where they are - are processed at the start.
So the User Defined Variables element will be read only once and only when the Test Plan is started.
If you need to overwrite the current variable with a new value you can go for i.e. __groovy() function, the relevant syntax would be something like:
${__groovy(vars.put('foo'\, 'some_prefix_' + vars.get('foo')),)}
Demo:
vars is a shorthand for JMeterVariables class instance, it provides read-write access to all JMeter Variables in the current thread scope. Check out The Groovy Templates Cheat Sheet for JMeter to learn what else you can do with Groovy scripting in JMeter tests
UDVs can't be used in dynamic way because they are process once at the start of the test.
Don't use UDV, use JSR223 Sampler (or PostProcessor) with vars;
vars.put("new", "prefix"+ vars.get("test"))
Another option is to use Set Variables Action plugin

Fetch the value of variable inside variable in jmeter

In the JSR Sampler I am assigning the value of a String I create i.e., SFDC_Prouct_1 in list. for e.g.,
list=SFDCProduct_5
Here SFDCProduct_5 is already a variable which has XML elements. Now I am trying to place a http request and in the payload I want to inject the value of SFDCProduct_5 . But I have to write ${list}, as list has the value of SFDCProduct_5 .
I tried ${${list}}, but thats not working.
Any ideas?
In JSR223 Sampler you can get the nested variable using vars.get
vars.get(vars.get("list"));
If I correctly understood your question you need to wrap your ${list} into __eval() function like
${__eval(${list})}
Demo:
See Here’s What to Do to Combine Multiple JMeter Variables article for more information if needed.

why does print vars.put ,the result is null?

String pass = "123456789";
vars.put("token01",pass);
System.out.println(pass);
System.out.println(token01);
the result of System.out.println(pass) is right ,it is 123456789,
but the result of token01 is null, I can not understand .
If you need to access a JMeter Variable value programatically you need to consider one of the following options:
If your variable is a String: String value = vars.get("token01")
If your variable is an Object: Object value = vars.getObject("token01")
Demo:
References:
vars.get()
vars.getObject()
You may also find Groovy Is the New Black article useful.
You have to use vars.get to access jmeter variables.
So you should use System.out.println(vars.get("token01")). Also you can use debug sampler and view results tree combination to make sure your script is working fine see How To Debug Your Apache Jmeter Scripts
for more information on jmeter troubleshooting techniques.

JMeter - Using Variables from other BeanShell Pre/Post-Processors?

Is there a way to reference a variable from one BeanShell Pre/Post-Processor to another BeanShell Processor (they are within the same Thread Group)?
If I create a String variable inside a BeanShell PreProcessor under an HTTP Request, can I then use or reference that variable inside a BeanShell PostProcessor under that same HTTP Request..?
I tried Accessing this variable in the following ways:
+ HTTP Request
+ BeanShell PreProcessor:
String preProcessor1_MYID = "Value_1";
+ BeanShell PostProcessor:
String postProcessor1_MYID = "Value_2";
//Try #1:
String tmp_preProcessor1_MYID = preProcessor1_MYID;
//Try #2:
String tmp_preProcessor1_MYID = ${preProcessor1_MYID};
//Try #3:
String tmp_preProcessor1_MYID = ${__V(preProcessor1_MYID)};
//Try #4:
String tmp_preProcessor1_MYID = vars.get("preProcessor1_MYID");
Is there a different function like ${__V()} or vars.get(), that I'm missing that I'm supposed to be using? I was also wondering if I needed a User Defined Variables object in order to share this variable between BeanShell Pre/PostProcessors but I wasn't sure. I also read about the ${__BeanShell()} function, but didn't think that was what I was looking for either... Any ideas? I would assume this should be possible, but was hoping I didn't need to add anything like the User-Defined Vars object.
Any thoughts or suggestion would be greatly appreciated!
Thanks in Advance,
Matt
If you need to use the value in other elements later,
store it in a vairable
vars.put("myvar", "value");
Now you can access it using
${myvar}
or in beanshell
vars.get("myvar").
You can also go for user defined variables, properties (share among thread groups).
Check this - it is also another option. - jMeter - Beanshell bsh.shared hashmap data in init file?

Resources