Process of Array of Strings in Beanshell preprocessor in Jmeter - jmeter

I have a Jmeter (version 4.0) test script where I am using json extractor to fetch an array of strings from the response json using $..Names and storing it in a variable groupOfNames. The various names are stored like groupOfNames_1, groupOfNames_2, groupOfNames_ALL.
I need to make POST call next with body as
{
"name1", "name2", "name3" (--actual values--)
}
How can i achieve this using bean shell preprocessor? groupOfNames_ALL gives me all value but like this.... name1, name2, name3 (without quotes surrounding individual names). Please help. Thanks.

I heard Groovy is the New Black so you can add quotation marks around each of names as simply as:
vars.put('groupOfNames_ALL',vars.get('groupOfNames_ALL').split(',').collect {"\"$it\"" }.join(', '))
Demo:
Also as a gentle reminder: JMeter users are encouraged to use JSR223 Test Elements for any form of scripting since JMeter 3.1

Put the below code in your BeanShell PreProcessor:
int matchNr = Integer.parseInt(vars.get("groupOfNames_matchNr"));
for(int i = 1; i <= matchNr; i++){
String Names = vars.get("groupOfNames_" + i);
if(i == matchNr){
vars.put("AllNames", vars.get("AllNames") + "\"" + Names + "\"");
}
else if(i == 1){
vars.put("AllNames","\"" + Names + "\", ");
}
else{
vars.put("AllNames", vars.get("AllNames") + "\"" + Names + "\", ");
}
Then use the variable ${AllNames} in your post as below:
{
${AllNames}
}

Related

Jmeter Beanshell: Take multiple variables from CSV Data Set Config?

I have a Jmeter test in which I want to post an XML message to a JMS queue. This message will be formed dynamically via a BeanShell Preprocessor, which pulls data from multiple CSV Data Set Config elements.
One item of this XML message that is dynamic is the number of elements in it - it will be a random number between 1 and 10. For each Line element, I want to pull a different variable from a CSV Data Set Config element. However, I'm finding if I do something like the below, I keep getting the same variable:
for (int i = 0; i < numberOfLines; i++) {
InputXML = InputXML + "<OrderLine ItemID=\"${ItemID}\" />";
}
The above will keep using the same ${ItemID} variable for all of lines but what I want is for it to grab the next one in the CSV file.
Is there any way to accomplish this via Beanshell?
To go along with your data, if CSV looks like (first row will saved as variables)
0,1,2,3,4,5,6,7,8,9
a,b,c,d,e,f,g,h,i,j
The Beanshell will use index i to get value of column i in CSV:
String InputXML = "";
for (int i = 0; i < 10; i++) {
String a = vars.get(String.valueOf(i));
InputXML = InputXML + "<OrderLine ItemID=\"" + a + "\" />";
}
vars.put("InputXML",InputXML);
InputXML variable will hold the full value.
If you want random value until 10, you can use JMeter function ${__Random(0,10,myRandom)}.
If you want to get random line in CSV you can use the similar answer.

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

How to get print the value of a variable which is extracted by regural expression extractor in 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.

How to add multiple values to a parameter in jMeter

How can I add multiple values (these values are extracted with regex extractor) to a parameter.
I have the following test:
Using the regex extractor I get the following:
Now I'm using a BeanShell PreProcessor that contains the following code:
int count = Integer.parseInt(vars.get("articleID_matchNr"));
for(int i=1;i<=count;i++) { //regex counts are 1 based
sampler.addArgument("articleIds", "[" + vars.get("articleID_" + i) + "]");
}
Using this will generate the following request:
This will add multiple parameters with the same name (articleIds) which will cause an error when I'm running the test. The correct form of the parameter should be:
articleIds=["148437", "148720"]
The number of articleIds is different from a user to another.
That's totally expected as you're adding an argument per match. You need to amend your code as follows to get desired behavior:
StringBuilder sb = new StringBuilder();
sb.append("[");
int count = Integer.parseInt(vars.get("articleID_matchNr"));
for (int i = 1; i <= count; i++) {
sb.append("\"");
sb.append(vars.get("articleID_" + i));
if (i < count) {
sb.append("\", ");
}
}
sb.append("\"]");
sampler.addArgument("articleIds", sb.toString());
See How to use BeanShell guide for more details and kind of JMeter Beanshell scripting cookbook.

How to deal with randomly generated post request parameters in Jmeter Scripts?

I am writing jmeter scripts for my web-based application. I am using firefox-firebug to watch POST request parameters. I could successfully write login page scripts because it had only "username" and "password" parameters.
But, after logging into the web application, I realized that, there are randomly generated required parameters which are sent along with the post request.
So, I am trying to find out the way to deal with these parameters.
Please let me know, if you have dealt with this situation.
Example: These are my post request parameters:
externalId=971&submit.go=Go&submit.go=&013f57c77c2a%3A6eed%3A1b320be7=105f230e-9f86-40f8-9473-215975812128
Where **013f57c77c2a%3A6eed%3A1b320be7** parameter and it's value are generated differently each time.
I don't know how to define this parameter.
I found an answer. You can use List Extractor(Regular Expression Extractor).
You can define any pattern as per your criteria.
for example regex patter is : input type="hidden" name="([^"]+?)" value="([^"]+?)"
Step2) Use Beanshell pre processor with this script.
log.info("=====================");
count = Integer.valueOf (vars.getObject("hiddenList_matchNr") ) ;
log.info("Number of hidden fields in previous sampler: " + count);
for (i=1; i <= count; i++) {
paramName = vars.getObject("hiddenList_"+ i + "_g1");
paramVal = vars.getObject("hiddenList_"+ i + "_g2");
log.info("Adding request parameter: " + paramName + " = " + paramVal);
sampler.addArgument(paramName, paramVal);
}
log.info("=====================");

Resources