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

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.

Related

Jmeter check using if controller, for a variable has a value or not

in one of my steps in the Jmeter script, I'm using json extractor to read a value from a key(address) in the JSON response and store it in a variable called "TypeOfRequest." In the next step, I need to check if the "TypeOfRequest" value is null or not(There can be situations I don't find that key in the JSON response). Then I need to take a different route.
Snippet how I'm getting the TypeOfRequest from Json extractor $.communicationMethods[:1].hTTPS.address
So my question is, how do I check if TypeOfRequest has a value or not in the if controller?
tried using '${__javaScript(vars.get("TypeOfRequest") == null)}(ref https://www.blazemeter.com/blog/jmeter-if-controller and https://sqa.stackexchange.com/questions/32969/how-do-i-check-if-a-variable-is-null-using-a-if-controller) but unable to go through the if condition, can someone help me with this. Thanks in advance
Just use Debug Sampler to see what JMeter Variables are defined and inspect their values, my expectation is that you're "unable to go through the if condition" because your TypeOfRequest variable is not null, i.e. it's present but it's an empty string.
Also the referenced article suggests using __groovy() or __jexl3() function so I believe if you change your condition to something like:
${__groovy(org.apache.commons.lang.StringUtils.isEmpty(vars.get('TypeOfRequest')),)}
you will be able to "go through the if condition"

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.

Put value in parameter using beanshell and 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");

JMeter - how to randomize number of parameters in request

I am new to JMeter.
In my GET request I want to have random number of parameters, so sometimes I want to have:
a = value1
a = value2
a = value3
and sometimes I want to have
a = value1
a = value2
etc.
Can I achieve it in JMeter in another way than making separate request modules?
You can specify parameters dynamically in the Path field, using a variable:
The variable should be created / formatted before HTTP request is sent. For example here I am using counter and BeanShell pre-processor to create a proper set of parameters:
So if I run this with 3 iterations, I will get:
GET http://stackoverflow.com/x?a=value0
GET http://stackoverflow.com/x?a=value0&a=value1
GET http://stackoverflow.com/x?a=value0&a=value1&a=value2
etc. Of course the logic of creating params should be based on your needs, this is merely an example. The reusable part of that example is saving params in a string, and then saving them into variable:
String myDynamicParameters = "";
// your logic here
vars.put("myDynamicParameters", myDynamicParameters);
If you want to generate variable directly inside your GET parameters, the fastest way is to use inline snippets.
something like:
GET http://xx.com/${__Random(1,99999)}
You can do it via i.e. Beanshell PreProcessor like:
sampler.addArgument("name", "value");
See:
HTTPSasmplerBase.addArgument() method JavaDoc
How to Use BeanShell: JMeter's Favorite Built-in Component for comprehensive information on using JMeter and Java APIs in your test.

Using Extracted JSON Value in Another JMeter Thread

First, let me preface this question that I've only been using JMeter for 36 hours.
I've been able to successfully create a thread that performs a POST (json body) to generate a new record.
{
"id":1257697771,
"displayName":"TERM2",
"functionName":"f_1257697771",
"displayableSourceExpression":"TRUE",
"typeId":200,
"groupId":300,
"clobObjId":1257697772,
"typeCode":5,
..........
}
I need to take the new record's ID (1257697771) value returned in order to perform updates, get by ID, delete, etc. on this record in other threads.
After much reading, I've created a Regular Expression Extractor where:
Apply to: Main Sample Only
Field to Check: Body as Document
Reference Name: newRecord
Regular Expression: "id":(.+?)\,"displayName"
Template: $1$
Match No: 1
Default Value: NONE
At this point, I'm not sure if my Regular Expression is formatted correctly where (.+?) is valid.
Also, I'm confused if I can either just specify the new reference (newRecord) in another thread's HTTP request's Parameters or use a BeanShell Post-Processor, or a Response Assertion, etc....
There a lot of answers for the same function of "Passing". Not being a programmer, I've tried to follow the discussion "how to extract json response data in jmeter using regular expression extractor?", but I'm still not clear.
Any insight is appreciated. Thanks.
JMeter Variables are local to Thread Group, you need to convert your variable to JMeter Property.
Use:
__setProperty() function in the Thread Group where you define your newRecord variable like:
${__setProperty(newRecord,${newRecord},)}
__P() function to access property value like:
${__P(newRecord,)}
See Knit One Pearl Two: How to Use Variables in Different Thread Groups article for more detailed explanation.
Also be aware of the Function Helper Dialog as it looks like JMeter functions syntax was developed by aliens.
To pass a value between threads you need to use the jmeter property function.
In a jsr223 postprocessor using groovy the code to get the value is as follows:
def userProperty = props.get('propertyToGet')
vars.put('userProperty', String.valueOf(userProperty))
You would then access the variable in your thread using:
${userProperty}
Or you can use shorthand directly:
${__P('propertyToGet')}
Variables in jmeter are thread specific.
Thanks everyone. I was able to resolve it with your help!
In the first thread:
set the Reg Expression Extractor Regular Expression = "id":(.+?)\,"displayName"
added a Bean Assertion where Parameters = ${__setProperty(newRecord,${newRecord},)}
In the second thread:
appended the Path url with ${__P(newRecord,)}
Executing the first thread (POST) resulted an new record with a unique ID. (1257698108)
Executing the 2nd thread (GET) shows
GET http://server/.../.../.../.../1257698108
And returns the exact data generated in the first thread.
Thanks everyone for your help!

Resources