why this is happening only request at the last is only getting executed.
pre- request script:
const testCases = ['1002', '3002', '100002', 'singh', '1002y'];
// Set the productid variable for the current iteration
pm.variables.set('productid', testCases[3]);
pm.sendRequest({
url: pm.variables.replaceIn("{{baseUrl}}/products/:priductId"),
method: 'GET'
});
pm.variables.set('productid', testCases[0]);
pm.sendRequest({
url: pm.variables.replaceIn("{{baseUrl}}/products/:priductId"),
method: 'GET'
});
test:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
if(pm.response.code === 200)
console.log("ok");
});
pm.test("Status code is 400", function () {
pm.expect(pm.response.code).to.equal(400);
if(pm.response.code === 400)
console.log("Bad Response");
});
console:
GET https://valentinos-coffee.herokuapp.com/products/:priductId
400
271 ms
GET https://valentinos-coffee.herokuapp.com/products/:priductId
400
275 ms
GET https://valentinos-coffee.herokuapp.com/products/1002
200
269 ms
ok
In this code also same thing is happening
const testCases = ['1002', '3002', '100002', 'singh', '1002y'];
// Loop through test cases
for (let i = 0; i < testCases.length; i++) {
// Set the productid variable for the current iteration
pm.variables.set('productid', testCases[i]);
// Make the API request
const response = pm.sendRequest({
url: pm.variables.replaceIn("{{baseUrl}}/products/:productId"),
method: 'GET'
});
}
as you can see from the console only at last the productid is being assigned,
tried promise also then only one request is going out,
tried adding artificial wait also to make it wait 3 seconds then also same thing is happening only the request at last is having the productid assigned.
i want to rectify it so that for each request there is an output from test then next request goes just like that
2
Answers
Each
pm.sendRequest
is an asynchronous operation that doesn’t block the execution of subsequent code. As a result, when you’re looping through your test cases and setting theproductid
variable, the loop doesn’t wait for each request to complete before moving on to the next iteration. This leads to only the last request being executed with the expectedproductid
because the loop has completed its execution and setproductid
to the last item in yourtestCases
array before any of the requests have had a chance to start.Moreover, in your pre-request script, there’s a typo in the URL parameter name (
:priductId
should be:productId
), which might be contributing to the requests not working as expected.To rectify the issue and ensure that each request is executed with its corresponding
productid
, you can use JavaScript’s async/await pattern along with Postman’s sandbox environment’s ability to handle asynchronous operations. Here’s how you can modify your loop to wait for each request to finish before proceeding to the next one:Using
Run collection
.It will make it easy to loop API call
Save as
1-demo-collection.json
fileImport collection
Collection Variable
URL
Pre-request Script
Tests Script
Run it
Result