How to get print the value of a variable which is extracted by regural expression extractor in JMeter - jmeter

I am trying to track the value by printing it to the log but getting void as an answer.enter image description here

The easiest way to see the variable names along with values is using Debug Sampler
However if you need to print all the extracted values to JMeter log for some reason you need to slightly change your script to look like:
log.info("Detected " + vars.get("urls_matchNr") + " URLs");
for (int i=1; i<= Integer.parseInt(vars.get("urls_matchNr")); i++) {
log.info("URL # " + i + ": " + vars.get("urls_" + i));
}
vars stands for JMeterVariables class instance so this way you get read/write access to all Jmeter Variables in scope.
See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on Beanshell scripting in JMeter

There are 2 problems in your script.
1) You are extracting Match No: -1, which is not right (Check your regular expression extractor). You can either chose 0 for random match or any positive number for the respective match.
2) In BeanShell assertion, you are trying to retrieve the value as
logs.info("the" +urls);- which is not the right way to do.
To get the value of a variable in BeanShell we have to use "vars.get" method.
So change your assertion to logs.info("the" +vars.get("urls")); and try once.

Related

Get value for dynamic vars.put

If I set the put key value to the dynamic i value in the graph (this is a requirement),
When I get vars.get("i"), I can only get the value aaa2 when i=2
I want to get all the values(aaa1 and aaa2), what should I do?
To view multiple values you need to save multiple variables by changing your vars.put line to:
vars.put("i" + i, a);
and then get it using ${i[number]} as:
${i0} ${i1} ${i2}
Another option is to concatenate values to variable ${i] similar to
vars.put("i" , (vars.get("i") == null ? "" : vars.get("i")) + a);
Also change your Beanshell sampler to JSR223 Sampler according to JMeter Best Practices
we advise switching from BeanShell to JSR223 Test Elements

Jmeter: Comparison of multiple result for result response with JDBC variables

In j meter: In a bean-shell assertion or any other I want to match the content of response which I have fetched using Jason extractor suppose:
Result[1]=A, Result[2]=b, Result[3]=c
and so on Versus variables I have fetched using JDBC pre-processor which has saved as:
Result_1=, Result_2=B, Result_3=c.
I am able to match 1 variable at a time but not all at one time. so need help with bean-shell code to compare all result at once.
Add JSR223 Assertion somewhere to your test plan (normally as a child of a Sampler you would like to fail)
Put the following code into "Script" area
for (int i = 1; i <= Integer.parseInt(vars.get('ResultFromJSON_matchNr')); i++) {
if (!vars.get('ResultFromJSON' + i).equals(vars.get('ResultFromJDBC_' + i))) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Variables mismatch')
}
}
Make sure you have groovy selected as the "Language" and have Cache compiled script if available box ticked
The above script assumes ResultFromJSON and ResultFromJDBC JMeter Variables reference names, amend them according to your real definitions.
More information: Scripting JMeter Assertions in Groovy - A Tutorial

Count the number of occurences of a string in response data

I want to search a string from a response in jmeter and count the number of occurences based on which i want to use a if controller to run the next requests. I am stuck with the code for counting the occurences
You can do it in at least 2 ways:
Using Regular Expression Extractor:
Add Regular Expression Extractor as a child of the request.
Configure it as follows:
Reference Name: anything meaningful, i.e. count
Regular Expression: string you want to count, i.e. JMeter
Template: $1$
Match No: -1
The number of matches will be stored in ${count_matchNr} JMeter Variable
Using Beanshell PostProcessor
Add Beanshell PostProcessor as a child of the request
Put the following code into the PostProcessor's "Script" area
import org.apache.commons.lang.StringUtils;
String response = new String(data);
int count = StringUtils.countMatches(response, "JMeter");
log.info("Found " + count + " \"JMeter\" words at the " + prev.getUrlAsString() + " URL");
vars.put("count", String.valueOf(count));
You'll be able to refer the matches count as ${count} JMeter Variable
References:
JMeter Regular Expressions
How to Use BeanShell: JMeter's Favorite Built-in Component

Jmeter - Accessing Regex Array Variables In Beanshell

I have problems picking up variables set by the Regular Expression
Extractor in a Beanshell.
I have an HTTP Request sampler which returns a list of 50 numbers in random form (4, 2, 1, 3....50, 45) which I have extracted via regEx.
Now I want to get each and every numbers in a variable so I used again regEx with expression (.+?)(,) on JMeter variable from step# 1 above.
I have problem here at this step when I am using BeanShell to access these values
Wasn't very sure I used below:
long var1 = Integer.parseInt(vars.get("Number_i"));
print("Value of var1: " +var1);
Practically I want to do this:
for (i=0; i<50; i++) {
if (var1==1) {
do this
}
}
I am not adept at Jmeter, so please bear with me.
Given you extract variables using Regular Expression Extractor and you have > 1 match you already have multiple variables, you can check them using Debug Sampler and View Results Tree listener combination
So you can access variables in JMeter like:
${number_1}
${number_2}
and in Beanshell test elements using vars shorthand which stands for JMeterVariables class instance like:
vars.get("number_1");
vars.get("number_2");
Example code which will iterate all the matches and "do something" when current variable value is "1"
int matches = Integer.parseInt(vars.get("number_matchNr"));
for (int i=1; i<=matches; i++) {
if (vars.get("number_" + i).equals("1")) {
log.info("Variable: number_" + i + " is 1");
// do something
}
}
See JMeter API - JavaDoc on all JMeter classes and How to Use BeanShell: JMeter's Favorite Built-in Component for more information on how to get started with Beanshell in JMeter

Need to extract dynamic values from a string response in Jmeter

I need to extract the dynamic value "BSS1,DS1,HYS1,MS1,PTS1,QS1,USG1,YS1,RT10086,RT10081,RT10084,RT10082,OT10076,RT10083,UT10081,RT10085,"
from the string response "ACCOUNT_DETAIL_ACCOUNT_PRODUCT_SERVICES_EDIT_UPDATE_NameSpace.grid.setSelectedKeys(["BSS1","DS1","HYS1","MS1","PTS1","QS1","USG1","YS1","RT10086","RT10081","RT10084","RT10082","OT10076","RT10083","UT10081","RT10085"]);"
I have tried using the regular expression extractor :
Regular Expression :Keys\(\[\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\"]\)
template : $1$$2$$3$$4$$5$$6$$7$$8$$9$$10$$11$$12$$13$$14$$15$$16$
But the above regular expression works only if there are 16 values in the response. If the response contains less number of values, for example, "ACCOUNT_DETAIL_ACCOUNT_PRODUCT_SERVICES_EDIT_UPDATE_NameSpace.grid.setSelectedKeys(["BSS1","DS1"]);"
then the above regular expression doesn't work.
How can I extract the values in the response if the total count is unknown?
Also the double quotes in the response need to be omitted.
Is there any post processor using which dynamic values can be extracted?
Any help is greatly appreciated.
I believe it will be easier with some scripting.
Add Beanshell PostProcessor as a child of the request which returns aforementioned response
Put the following code into the PostProcessor's "Script" area:
String response = new String(data);
String rawKeys = response.substring(response.indexOf("[") + 1, response.indexOf("]")); // get the data inside square brackets
String keysWithoutQuotes = rawKeys.replaceAll("\"", ""); // remove quotes
String[] keyData = keysWithoutQuotes.split("\\,"); // get array of keys
for (int i = 0; i < keyData.length; i++) { // store array of keys into JMeter variables like
vars.put("Keys_" + (i +1), keyData[i]); // Keys_1=BSS1, Keys_2=DS1, etc.
}
vars.put("Keys_matchNr", String.valueOf(keyData.length)); // set Keys_matchNr variable
Where:
data is byte array containing parent sampler's response data
vars is a shorthand to JMeterVariables class which provides read/write access to JMeter Variables.
As a result you'll have variables like:
Keys_1=BSS1
Keys_2=DS1
..
Keys_matchNr=X
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for additional information on Beanshell scripting in JMeter and some more examples

Resources