How to iterate the foreach loop in JMeter - jmeter

I got the array response from the one API and I passed to the response to the other API.
For example:
Response from one API is {status:200,data:{name:"Manikyam", selected_products:[1,2,3,4,5]}
I need to iterate the selected_products from the response using foreach or other loop which is suitable of this.
Iterations like:
http://dummy.com/product/1
http://dummy.com/product/2
http://dummy.com/product/3
http://dummy.com/product/4
http://dummy.com/product/5
I tried some ways like using forEach but I haven't any luck.

The below answer assumes that your response is a valid JSON looking like:
{
"status": 200,
"data": {
"name": "Manikyam",
"selected_products": [ 1, 2, 3, 4, 5 ]
}
}
Add JSON Extractor as a child of the request which returns the above JSON and configure it as follows:
Names of created variables: anything meaningful, i.e. product
JSON Path Expressions: $.data.selected_products.*
Match No: -1
Add ForEach Controller and configure it as follows:
Input variable prefix: product
Output variable name: product
That's it, if you add a Sampler as a child of the ForEach Controller, the controller will iterate all the variables so you will be able to refer each and every as ${product} where required like http://dummy.com/product/${product}
Demo:

Related

How to pass multiple values in a for each controller

This is how the test plan looks :
Thread group
Bean shell sampler
For each Controller
Graphql request
In the previous thread I extracted 2 attributes which are id and price. Now this id and price are unique for each case.
I extracted these 2 attributes from the json response and stored in a text file separated by colon. Sample below (id : price)
123-456-789 : 45.5
889-332-121 : 60
I need to run the above thread for each such combination i.e. id and price need to be passed at run time to the variable section of graphql request.Using for each controller I am able to pass each of the Ids but how do I pass the corresponding price ?
If you have JMeter Variables like:
id_1=123-456-789
id_2=889-332-121
price_1=45.5
price_2=60
The id you can get from the ForEach Controller configured like:
And refer it as ${id} under the ForEach Controller
With regards to the "price" you will need to use __V() and __intSum() functions combination like:
${__V(price_${__intSum(${__jm__ForEach Controller__idx},1,)},)}
Demo:
More information: Here’s What to Do to Combine Multiple JMeter Variables

How to compare Variable with CSV file content in Jmeter

I have HTTP request whose response is below
{
"DATA": {
"G_1": {
"OR_ID": "100400",
"LEGAL_ENTITY": "TEST",
"BUSINESS_UNIT": "TEST BU"
},
"G_2": {
"OR_ID": "100500",
"LEGAL_ENTITY": "Test1 ",
"BUSINESS_UNIT": "Test1 "
},
"G_2": {
"OR_ID": "100100",
"LEGAL_ENTITY": "TEST3 ",
"BUSINESS_UNIT": "Test3"
}
}
I need to get OR_ID from the above response, which I am able to do it using Regular exp extractor.
There is Input CSV file which has multiple rows. For the CSV file, i need to check the OR_ID exists or not in column 2, if exists then I have to take columns 5 and 7 and pass it to my next post request in the body. In CSV the same OR_ID repeated, so i need repeat post request for all the repeated values of OR_ID in csv. CSV file has no header.
441919244,100010,QUTRN,TEST Inc.,100100,TEST,VCG and A, INC,USD,3409.0900,O,ICO-VCG-0140,2019-10-31,52 945,USD,USD,359409.0900,359409.0900,359409.0900,Processed,93901372,File,2019111NG52.csv,
441919028,100400,QUQED,TEST MEDICAL EDUCATION INC.,100020,QUINC,TEST INC.,USD,12.340,O,ICO-INC-8718,2019-10-31,52 729,USD,USD,12.3400,12.3400,12.3400,Processed,93901372,,File,20191113NG52.csv,
Can you please help.
Assuming that you can extract the OR_ID from the JSON response following solution could be useful.
In the CSV Data Set Config Element or Random CSV Data Set Config plugin read the CSV file assign the variable names to the respective columns
variable names = C1,OR_ID,C3,C4,C5,C6,C7,C8,C9
Add a While Controller as a parent to the CSV Data Set config elements and the HTTP Request where you want to send data from the CSV file.
${__jexl3("${OR_ID}"!="EOF")}
This will check EOF in the column 2 of the CSV file. Hence please add
,EOF,,
as the last line of the CSV file.
Add IF controller to the HTTP Request with following condition
${__jexl3("${OR_ID}"=="${OR_ID_J}")}
OR_ID_J is the OR_ID picked from the JASON response.
Use ${C5} and ${C7} in the places where you want to insert the data from the CSV file.
Reset the OR_ID to "" using a JSSR223 Sampler with following
vars.put("OR_ID", "");
Sample Test Plan is available in GitHub

Jmeter, How to get one of response result to assign to next request

I have a response for request #1: 2 step codes that correspond with stepId as below.
In next request, I want to use only stepId = stepId that I assign from CSV file
**content**":[
{
"stepId":21,
"stepCode":"11",
"stepName":"11",
},
{
"stepId":17,
"stepCode":"??",
"stepName":"To be checked",
}
]
For example if you need to get stepCode value where stepId is 17 you can use JSON Extractor and the following JSON Path query:
$..[?(#.stepId == '17')].stepCode
Demo:
You can replace this 17 with a variable coming from the CSV Data Set Config like:
$..[?(#.stepId == '${your_variable_from_csv}')].stepCode
References:
Json Path Operators
JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios

Get property of related/nested object

I want to get a property of a related/nested object. A question object has many choices, and I want to return a certain choice with a correct field equal to True/1.
So I get the question by ID:
$question = Question::find($request->get('id'));
Then get the related choice which has correct == 1:
$answer = $question->choice->where('correct', 1);
return $answer;
The response is an object within an array:
[
2
{
"id": 3,
"choice": "Non eos architecto ut.",
"question_id": 1,
"correct": 1,
}
]
I wanted to access the choice field:
return $answer->choice;
But get this error:
Undefined property: Illuminate\Database\Eloquent\Collection::$choice
I tried using flatten:
$answer = $question->choice->where('correct', 1)->flatten();
But all this does is remove the question ID 2, but doesn't allow me access to the object properties.
Use the other answer if you want to retrieve just that one value from the database.
But it looks like you already retrieved all the answers as a Collection and want to filter that collection.
The where method returns another Collection. So to get just the one element, use first:
$answer = $question->choice->where('correct', 1)->first();
This gives you the actual Eloquent object
And then you can access any properties as usual.
return $answer->choice;
Use the value method to get a single value from the database:
$choice = $question->choice()->where('correct', 1)->value('choice');
BTW, you should rename your choice method to choices, since it's a hasMany relationship.

How to get only Odata.Count without value

Is there any way I can get only count of the data in response payload without any value array?
I am using ODataV4.0 with Webapi 2.2.
Currently it returns all the values and count when I query something like:
http://odata/People?$count=true
I just need something like "#odata.count":1, "value":[] or without "value".
Is the only way to have function for this job?
Set the $top to zero and $count to true.
For example:
http://services.odata.org/V4/Northwind/Northwind.svc/Customers?$count=true&$top=0
returns the count but no results
{
"#odata.context": "http://services.odata.org/V4/Northwind/Northwind.svc/$metadata#Customers",
"#odata.count": 91,
"value": []
}
Count is calculated after applying the $filter, but without factoring in $top and $skip.
For example: http://services.odata.org/V4/Northwind/Northwind.svc/Customers?$count=true&$top=0&$filter=Country%20eq%20%27Germany%27
informs you that there are 11 results where the Country is 'Germany', but without returning any records in the response.
You can also append $count as a path element to just get a raw count, E.G.,
https://services.odata.org/V4/Northwind/Northwind.svc/Customers/$count
This will also work with filters, etc, applied:
https://services.odata.org/V4/Northwind/Northwind.svc/Customers/$count?$filter=Country%20eq%20%27Germany%27
For a count of Customers in Germany.

Resources