Scenario:Create a dynamic array , from Jmeter correlated values/Dynamic values.
My application gives a Json response for the sampler (Get Dynamic Json response) , from that json response we need to capture dynamic values (global_property_ids) and pass all caputed dynamic values (global_property_id's) as an array to next post request. Below are the steps.
Download this Project from github
Step 1: Add thread group
Step 2: Add "http request" sampler with below details to get "json Response" ,that contains dynamic values of global_property_ids . Add server IP,PORT,Protocol,Method and URI
Server IP: raw.githubusercontent.com
Port Number:80
Protocol : https
Method: Get
URI or PATH: /mcheepati/api/master/response.json
Step 3: Add the "View Results tree" to the "Thread Group" and Run the Jmeter test using Ctrl+r
and see the results from "View Results"
We need to correlate all request_id , to do this see the Step 4
Step 4: To capture dynamic values , add the "Regular expression extractor" from Jmeter "Post Processor" as a child to this sampler (Step 2)
Note: Match No. should be -1 (minus 1).Which will capture all the matched values.
Step 5: Check , Regular Expression by selecting "RegExp Tester" from "View Results tree".
Our Regular expression is correct, so it displayed all the " request_id" values. See more details above highlighted with red line
Step 6:Add "Http Request" Sampler as next request in "Thread Group" , add pass the dynamic array (arrayOutPut) to this request as post method. Please see details below
IP : LocalHost
Port :8080
PATH :/
Post Parameter:
request_ids_array:[${arrayOutPut}]
Step 7: This Step is very Important , we are creating dynamic array in this step only.
Add "Bean Shell Pre-Processor" as a child to Step 6 and using bean shell script to create dynamic array.
Below is the Code
int count = Integer.parseInt(vars.get("request_id_matchNr"));
String delimiter = ",";
StringBuffer sb = new StringBuffer();
for(int i=1;i<=count;i++) {
sb.append(vars.get("request_id_" + i));
if (i == count){
break; //to eliminate comma after the array
}else {
sb.append(delimiter);
}
}
vars.put("arrayOutPut",sb.toString());
Step 8: Run the Jmeter (CTRL+r) and verify the results
int count = Integer.parseInt(vars.get("request_id_matchNr"));
//creating String using dynamic values "request_id_"
//Converting StringBuilder to String
//Creating string Array from string
//Creating Integer array from string array
//Sorting Integer array
//Converting Integer array to string
Below is the jmeter output after using above code in "Beanshell preprocessor"
Step 9: If you want sorted array , use below code in "Beanshell Preprocessor "
log.info("Count is:"+count);
String delimiter = ",";
StringBuffer beforeSort = new StringBuffer();
for(int i=1;i<=count;i++) {
beforeSort.append(vars.get("request_id_" + i));
log.info("values is:"+vars.get("request_id_" + i));
if (i == count){
break; //to eliminate comma after the array
}else {
beforeSort.append(delimiter);
}
}
log.info("Before Sort the string is :"+beforeSort);
String stringBuilderToString=beforeSort.toString();
String[] numbers = stringBuilderToString.split(",");
Integer[] intValues = new Integer[numbers.length];
for (int i = 0; i < numbers.length; i++) {
intValues[i] = Integer.parseInt(numbers[i].trim());
}
Collections.sort(Arrays.asList(intValues));
StringBuilder builder = new StringBuilder();
for (int i = 0; i < intValues.length; i++) {
Integer intValue = intValues[i];
builder.append(intValue);
if (i < intValues.length - 1) {
builder.append(", ");
}
}
log.info("sort is :"+builder);
vars.put("arrayOutPut",builder.toString());
Below is the code block in Jmeter
12 comments:
Hi Madhu,
This blog helped me a lot as I had a similar scenario to test. There is one addition requirement in my scenario and that is to sort the elements which we get. For example we have following in variable : 91,32,73,11,3,87
Is there a way by which we can sort these values and then send it in our request parameter like: 3,11,32,73,87,91.
Thanks.
Below is the code
int count = Integer.parseInt(vars.get("request_id_matchNr"));
log.info("Count is:"+count);
//creating String using dynamic values "request_id_"
String delimiter = ",";
StringBuffer beforeSort = new StringBuffer();
for(int i=1;i<=count;i++) {
beforeSort.append(vars.get("request_id_" + i));
log.info("values is:"+vars.get("request_id_" + i));
if (i == count){
break; //to eliminate comma after the array
}else {
beforeSort.append(delimiter);
}
}
log.info("Before Sort the string is :"+beforeSort);
//Converting StringBuilder to String
String stringBuilderToString=beforeSort.toString();
//Creating string Array from string
String[] numbers = stringBuilderToString.split(",");
//Creating Integer array from string array
Integer[] intValues = new Integer[numbers.length];
for (int i = 0; i < numbers.length; i++) {
intValues[i] = Integer.parseInt(numbers[i].trim());
}
//Sorting Integer array
Collections.sort(Arrays.asList(intValues));
//Converting Integer array to string
StringBuilder builder = new StringBuilder();
for (int i = 0; i < intValues.length; i++) {
Integer intValue = intValues[i];
builder.append(intValue);
if (i < intValues.length - 1) {
builder.append(", ");
}
}
log.info("sort is :"+builder);
vars.put("arrayOutPut",builder.toString());
Hi Madhu,
Thanks a lot for very detailed response. I had also found a similar workaround on stackoverflow which I used in my script. I will try your solution as well as it may be more efficient and flexible.
I am also planning to write a blog on similar lines at my blog testerlogic.com . Since I got lot of help from you so I will mention your blog (link, not content) in my post. Hope that will be okay with you. Thanks again.
Hi Madhu,
This info helped me a lot. I have an additional requirement please help me on the same:
From the above regular expression extractor we have got the values(For Ex: 1,2,3,4). How do i use them individually in next request. For Ex: API one should call "1" first and then same API should call "2" likewise till "4".
Thanks in advance.
Regards,
Harish Gunda
More details:
Step1:Call API1 and fetch all region details(like: US, INDIA, JAPAN).
Step2: Call API2 and fetch use fetched details(US, INDIA, JAPAN) as a separate requests.
Hope you are clear on my requirement.
Thanks in advance.
Regards,
Harish Gunda
Use For Each controller.
Hi Madhu
Thanks a lot this blog helped me a lot. I have the similar requirement I need to fetch the offer_id from response and pass the same offer_id which I fetch through the Json extractor to another http_request of request body.
This piece of code I need to create as dynamic, Please help me how I can change the piece of code to dynamic so that it can fetch data from the Json extractor.
"couponDefinitionId":"98560683506442407865",
"offers":[
{
"offerId":"836229223"
},
{
"offerId":"622838141"
},
{
"offerId":"568095428"
},
{
"offerId":"609717089"
},
{
"offerId":"844470905"
}
]
}
]
}
],
Code in beanshell.
String offer_id = vars.get("offer_id");
int count = Integer.parseInt(vars.get("offer_id_matchNr"));
String delimiter = ",";
StringBuffer sb = new StringBuffer();
for(int i=1;i<=count;i++) {
sb.append(vars.get("offer_id_" + i));
if (i == count){
//offerIds[] = "{$offer_id1\":\""}",
}
break; //to eliminate comma after the array
}else {
sb.append(delimiter);
}
}
Please help me out and I am new to Jmeter.
Regards,
Megha
Hi,
How can we shuffle array strings from previous responses.
the response on sorting numbers i have gone through the sort solution you have provided is good, i tired using it by placing shuffle it was working but unable to do it on string array.
created a string array using extracted values, but unable to shuffle it.
ex: [apple, bat, cat] -> Shuffle -> [cat,Apple,bat]
how to shuffle and assign to each value to jmeter variables.
Hi Madhu,
Thanks for sharing the knowledge , Currently I am persisted with a problem as below , Kindly help me.
I need to split this sting "C_SelWorkflow0","C_SelWorkflow1","C_SelWorkflow2".. this is dynamics sting which I am fetching with Regex and save string(C_SelWorkflow0,1,2,3) each time I execute loop in Jmeter using loop controller.
Thanks in Advance.
Regards,
Malli
Hi Madhu,
I have a scenarios like, where 2 users will execute the same Get method API.
Here user1 will get one dynamic ID and user2 will get one dynamic ID along with user1's dynamic ID in JSON response.
So for User2, these two IDs are generated in 'Alphabetical order'. This order of ID's-match number is changing one to test to another test.
we need to pass these ID's in subsequent API's.
Can you please share any sample code to pass the values correctly
Thanks in advance.
Thanks Madhu for this Awesome post Please also check this JMeter Tutorial by ArtOfTesting
Hi Madhu,
Can you please help with this scenario -
Using regular expression extractor we have got the values(For Ex: 1,2,3,4) from first GET API request.
How do i use them randomly in next PATCH API request using For each controller or any other option if possible.
Example: PATCH API should call "1" first
then same PATCH API should call "4"
Post a Comment