Jmeter- how to pass a fixed number of values from an array at a time until iterating through all values to a http request - jmeter

In my get htttp request, I get an array of ids ex: list=[1, 2, 3.....1000]
And then for my next http request, I want to pass all the values in the list 10 at a time, so it will be 100 requests total and each time, it takes 10 values from the list array. I'm using a loop controller to call the http request 100 times. but I dont know how to retrieve 10 values at a time, and then go to the next 10, next 10 until all values are used.
How should I do that?

Add a regular expression extractor to your get request as a child.
Add the following properties shown in the screenshot to extract List
Add a Beanshell sampler/ JSR223 sampler and add the following code.
The code below creates a series of variables and store series of 10 values in a variable starting from Final_0 to Final_99
import java.util.Arrays
String complete_List=vars.get("List");
String[] complete_List_Array = complete_List.split(",");
int i;
for(i=1;i<=complete_List_Array.length;i++)
{vars.put("List_"+i,complete_List_Array[i-1]);}
int j;
int loopcount=complete_List_Array.length/10;
vars.put("loopcount",Integer.toString(loopcount));
for(j=0;j
{
StringBuilder sb = new StringBuilder();
sb.append("[");
for(i=j*10+1;i<=(j+1)*10;i++)
{
sb.append(vars.get("List_" + i));
if (i < (j+1)*10)
sb.append(",");
}
sb.append("]");
vars.put("Final_"+j,sb.toString());
}
vars.put("counter","0");
Add a loop counter and mention loop count as ${loopcount} as shown below
You can add HTTP Request as a child of loop counter and to pass series of 1o value at a time use ${__V(Final_${counter})}
Add a beanshell post processor to the http request to that will increment the counter
add the following code to the beanshell sampler
int counter = Integer.parseInt(vars.get("counter")) +1;
vars.put("counter",Integer.toString(counter));
You can see in the results its passing series of 10 values at a time and loop will run for 100 times
For more info on beanshell please follow the link
Please let me know if it helps.........

Considering you have the list array available at your disposal by using any extractor.
Extract the sublist from the array and put them in properties. Then, fetch the properties where ever you want it.
In the below I am fetching the sublist from the array and putting it in jmeter properties.
Here, I am fetching the values from the properties. This is just for demonstration and you dont need this. After putting list in properties just fetch in HTTP sampler as shown in the last image.
Now, to fetch it in HTTP sampler you can use loop and counter and fetch the properties using groovy. Loop for iteration and counter for increment the variable mylist_x.
Hope this helps.

Related

User Beanshell processor to write code so that data driven testing performed rather than using csv

I want to test a Get Request with list of values. I dont want to use CSV ,
so i started using Beanshell Preprocessor and has those values in Array. Then used for loop to use those values and send to Get Request in HTTP Request. Every this was Successful except sending values to Get request. It is reading all values and sending the last read value to Get request.
Question : I want my request to run for each value when code reads the data one by one.
var TtValuedetails;
int i;
n=22;
String[] ttvalue = {"34324324224","fdadsfadsf","dfdsfdsfds","dafadsfa",
"45435435","dfadsfads"
};
for(int i=0;i<n;i++)
{
if(i==0)
{
TtValuedetails=ttvalue[i];
if(ttvalue[0]=="34324324224")
{
vars.put("TtValuedetails",TtValuedetails);
log.info(TtValuedetails);
log.info("first value is executed" );
Org.Apache.......startRequest();
}
}
} ;
We cannot help you without seeing the full code and knowing what you're trying to achieve but one thing is obvious: you should not be using Beanshell, since JMeter 3.1 it's recommended to use Groovy for scripting.
Current problem with your code is:
ttvalue array contains 6 elements
n=22
the line TtValuedetails=ttvalue[i]; will cause failure on 7th iteration of the loop due to IndexOutOfBoundsException
If you want to send a request for each value of the ttvalue array the easiest is converting it into separate JMeter Variables like:
ttvalue_1=34324324224
ttvalue_2=fdadsfadsf
etc.
and using ForEach Controller for iterating the values.
Example Groovy code for storing the values into JMeter Variables:
String[] ttvalue = ["34324324224", "fdadsfadsf", "dfdsfdsfds", "dafadsfa",
"45435435", "dfadsfads"];
for (int i = 1; i <= ttvalue.size(); i++) {
vars.put("ttvalue_" + i, ttvalue[i - 1]);
}

Jmeter - How to handle duplicate values captured in RegExpression from the same response

How to ensure/handle duplicate values are not getting captured while using regular expression?
In my scenario i need to capture multiple offers from my response. But cant use the same offers again and again within the transaction
There are multiple ways to do this. If you are using the default Regular Expression Extractor, the problem is it creates the variables (eg: offer_1, offer_2 etc), ready to go with the execution. It would've been easier if it returned a ArrayList of some sort where we can remove the duplicates. What I am about to suggest is to add those variables to a list in a JSR223(groovy) sampler/post processor then convert them back to usual jmeter variables to use in the usual jmeter script flow.
Snippet:
I created a sample script as per your description which would return multiple offers with some duplicates. Following is the state of jmeter variables before post processing.
offer_1=RUSSIA
offer_1_g=1
offer_1_g0=offer="RUSSIA"
offer_1_g1=RUSSIA
offer_2=UK
offer_2_g=1
offer_2_g0=offer="UK"
offer_2_g1=UK
offer_3=ICELAND
offer_3_g=1
offer_3_g0=offer="ICELAND"
offer_3_g1=ICELAND
offer_4=USA
offer_4_g=1
offer_4_g0=offer="USA"
offer_4_g1=USA
offer_5=UK
offer_5_g=1
offer_5_g0=offer="UK"
offer_5_g1=UK
offer_6=USA
offer_6_g=1
offer_6_g0=offer="USA"
offer_6_g1=USA
offer_7=USA
offer_7_g=1
offer_7_g0=offer="USA"
offer_7_g1=USA
offer_matchNr=7
As you can see above there are duplicates in the variables. Put the following Groovy code in a JSR223 post processor with groovy as language selected.
// Count of offers extracted by Regular Expression Extractor
def count = Integer.parseInt(vars.get("offer_matchNr"))
// An empty list which will store the offers
def offer_list = []
for (int i = 1; i <= count; i++){
def offer = vars.get("offer_" + i)
offer_list.add(offer)
}
// Removes the duplicates in the list
offer_list.unique()
// Following one liner adds new variables but with only unique offers in similar format as jmeter variable.
offer_list.eachWithIndex{ it, index -> vars.put("unique_offer_${index+1}", "${it}")}
After post processing:
unique_offer_1=RUSSIA
unique_offer_2=UK
unique_offer_3=ICELAND
unique_offer_4=USA

How to get Maximum value from correlated IDs in Jmeter?

I have scenario where I need to capture dynamic Order-value IDs from response and among those values, I need to select maximum value and pass it to the next request.
How can we achieve this with the help of Jmeter tool?
Assuming you have the following JMeter Variables from the PostProcessor:
foo_1=1
foo_2=5
foo_3=10
foo_matchNr=3
You can get the maximum value as follows:
Add JSR223 PostProcessor as a child of the request, make sure it goes after the PostProcessor which returns your Order-ID values
Put the following code into "Script" area:
List values = new ArrayList()
for (int i=1; i <= (vars.get('foo_matchNr') as int); i++) {
values.add((vars.get('foo_' + i) as int))
}
vars.put('foo_max', Collections.max(values) as String)
Assuming everything goes well you should be able to access the maximum value as ${foo_max} where required.
See Apache Groovy - Why and How You Should Use It article for more information on Groovy scripting in JMeter tests

In jMeter, how to update a parameter value on each occurrence?

I'm facing a situation where I need to build a request string dynamically before sending it in an HTTP sampler. I'm choosing a random number between 15 and 50, and then for that many times, I append an XML tag with a parameter. So if my random number is 22, the this appended string (I call it ricString) will contain the same xml tag 22 times! And all I want is for it to use 22 different parameter values from the CSV file. But it doesn't do that. It takes the same value 22 times, and then uses the next value in the next iteration. Here is what I have written in my beanshell pre-processor.
counter = ${__Random(15,50)};
i = 0;
String ricString;
while(i<counter)
{
i++;
ricString = ricString + "<req:RCS>${__StringFromFile(...\RIC_3_01_Flag.csv)}</req:RCS>";
}
I have tried using both __StringFromFile as well as __CSVRead(filename, next) functions but no luck. It just does not update the value when inside the while-loop. Anyone know what I'm doing wrong?
Use a CSV DataSet that you nest into your loop (this is very important).
Then just use that variable that your CSV DataSet defines in your XML.

Same random number and string for each thread

I need to generate the same JSON data twice for each thread for an HTTP request. I am having problem setting that up in JMeter.
My structure is:
Test Plan
- HTTP Header Manager
- Thread Group 1 users, 5 loop
- Random Variable,
- HTTP Request
I tried the combination of Per Thread User set to true and using seed for random function, but I can't achieve what I want. It keep on generating new number/string per loop.
Basically for each user, I want the exact same JSON request data.
I see three decisions for your case:
Use User Defined Variables before HTTP Request. Specify variables here. In this case it will be better to use __Random() function instead Random Variable.
Test Plan
- HTTP Header Manager
- Thread Group 1 users, 5 loop
- User Defined Variables, (Name:varName; Value:${__Random(1,100)})
- HTTP Request
Use Loop Controller (loop count=5) before HTTP Request (instead loop=5 in Thread Group)
I can suggest another solution which seems to me verbose and awkward but no other idea.
You create file using BSF (or BeanShell) PreProcessor and write value of random variable into file. Then read file before every request. In the next example I used Groovy and BSF PreProcessor.
import java.util.Random
def out= new File('File1.txt') // create file if it is not exists
if(!out.exists())
{
out.createNewFile()
Random rand = new Random()
int max = 10
def a = rand.nextInt(max+1)
out << a // write text to file
}
//then read value of generated variable
String fileContents = new File('File1.txt').text
//then put your variable into User defined Variable that I named HELLO
vars.putObject("HELLO",fileContents)
And in needed request use ${HELLO}

Resources