How to run a request multiple times?

Hi,

I have a scenario, where i want run the ā€˜Create SEā€™ and ā€˜Move SEā€™ requests 100 times after running ā€˜Create SDā€™ request. can anyone help me to achieve this.?

image

  1. Add an environment variable ex., count

  2. Add a Test script in ā€œCreate SDā€ request to set the count value
    pm.environment.set("count", 100);

  3. Add the below Test script in ā€œMove SEā€ request

    var currentCount = pm.environment.get("count")
    if (currentCount > 0){
     currentCount = currentCount -1 ;
     pm.environment.set("count", currentCount);
     postman.setNextRequest("Create SE");
    }
4 Likes

Hi jeevananthank,

Thank a lot for your help. I tried this code for count=10 it worked. I will try for count=100 and i ll get back to you if any issues.

1 Like

With if it doesnā€™t work. Should I user a while loop? With if how can I decrease the count?

Can i slightly change the requets parameter in each of these requests ?

Yes, it is possible. you have to update the variable value in the environment (using code in tests tab) before sending the request.

Here is a similar question asked and answered: Is it possible to generate requests dynamically?

And also a code sample: Postman

Looks great but requires to add a Test script in each request. The example has 2 requests, so easy.
But what if you have 200 different requests and you want to run each of them 10 times with different parameters? Can this be achieved by some collection scripts, i.e. in one single place rather than 200 test scripts?

Thanks

Hello jeevananthank,
in you example, is count defined in your API Body?

I am trying to apply same logic to what I have in API Body and one parameters in particular is ā€œtripIdā€ (tripId has a value of 9997)

ā€œtripIdā€: ā€œ9997ā€,

I am trying to implement your script but does not seem to be working. I need the script modify the original value. Loop and run again. Is a while loop not going to work here? Or is do I have to set field in Body to a variable? Any help appreciated.

var i = 0;
var current_tripId =pm.environment.get(ā€œtripIdā€);
while (i <= 2){
if (currentBookingId < 10000){
current_tripId = current_tripId +1;
pm.environment.set(ā€œbookingIdā€, current_tripId);
postman.setNextRequest(ā€œCreate SEā€);
}
i++;
}

@postmannub

The while is not needed, as setNextRequest is the loop and should be the same request that is currently running.

You need to get the currenBookingId before your IF statement, as you donā€™t appear to have that declared at the moment.

Your bookingId and tripId seem to be the same thing, so I donā€™t think you need both.

// untested, but the logic should be something like this
response = pm.response.json();
let current_tripId = pm.environment.get("tripId");
console.log(current_tripId);

if (current_tripId < 10000){ // should this be less than, or less than and equal?
    current_tripId++;
    pm.environment.set("tripId",current_tripId);    
    postman.setNextRequest("requestName"); // keep looping until tripId = 10000
} else {
    postman.setNextRequest(null); // break the loop
}

Please use the preformatted text option in the editor when pasting code, so it doesnā€™t all get aligned to the left.