3

I want to run the same request multi times with different pre-request scripts? Any idea how can I do it without using the Data Driven (CSV) test?

Eg., I have to run the below GET url multiple times (every 2 minutes) but whenever I run this, I need to have a different pre request tests!

{{url}}/legacy/COL

enter image description here

siv
  • 49
  • 1
  • 1
  • 2
  • you can use same logic , add what ever that changes to an environment varaible array and then access the value using pop for each iteration – PDHide Dec 04 '20 at 17:06
  • https://stackoverflow.com/a/36183449/2159683 – Artem Apr 14 '21 at 12:15

1 Answers1

1

Onetime operation:

If you want to send request 10 times (including first request 11) , then create two environment variables that contains the count. you can create the variables by simply copy pasting the below two lines in pre-request or test script ( remove all other code).

 pm.environment.set("repeat",10);
 pm.environment.set("repeat",10);

Once the variables are added remove the above lines from script.

Now in test script:

we can sendrequest multiple time by using pm.sendrequest or pm.setNextrequest. Here the example shows calling same request 10 more times using pm.setNextRequest.

The delay of 2mins or 3 mins can be set using the setTimeout javascript function which waits the mentioned time (here 3 seconds ) before executing the code inside that. so the setNextrequest will executed only after 3 sec in this case you can change it to 2 mins.

let repeatTemp = pm.environment.get("repeatTemp");

if (repeatTemp === 0) {
    pm.environment.set("repeatTemp", pm.environment.get("repeat"));

} else {
    let repeatTemp = pm.environment.get("repeatTemp")
    let increment =  pm.environment.get("increment")===0?15:pm.environment.get("increment")+5
    pm.environment.set("increment",increment)
    pm.environment.set("repeatTemp", repeatTemp-1);
    setTimeout(function () { postman.setNextRequest("something") }, 3000);

}

so if your request name is "yourrequestname" then it will send this request 1+10 times

Pre-request script:

in your format you mentioned yyyy-mm which is wrong mm stands for minutes not month for year-month you have to give capital YYYY-MM

let repeatTemp = pm.environment.get("repeatTemp");

let repeat = pm.environment.get("repeat");

if (repeatTemp===repeat) {
    pm.environment.set("increment", 0)
}


let moment = require('moment')
pm.environment.set('estimatedTimeArrival', moment().add(30 + pm.environment.get("increment"), 'minutes').format("YYYY-MM-DDThh:mm:ss"));
pm.environment.set('estimatedTimeDeparture', moment().add(2, 'hours').format("YYYY-MM-DDThh:mm:ss"));
pm.environment.set('scheduledTimeArrival', moment().add(10, 'minutes').format("YYYY-MM-DDThh:mm:ss"));

console.log(pm.environment.get('increment'))
console.log(pm.environment.get('estimatedTimeArrival'))

output:

enter image description here

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • Thank you for looking at my query. I want to change the time for each every iteration pm.environment.set('estimatedTimeArrival', moment().add(50, 'minutes').format("yyyy-mm-DDThh:mm:ss")); pm.environment.set('estimatedTimeDeparture', moment().add(2, 'hours').format("yyyy-mm-DDThh:mm:ss")); pm.environment.set('scheduledTimeArrival', moment().add(10, 'minutes').format("yyyy-mm-DDThh:mm:ss")); This is the Pre-requ tests for this request and I want to add the time from 30 minutes to 45,50,55 etc., in each iteration! How can I do that? Any idea to change the time? – siv Dec 04 '20 at 18:50
  • Do it in test script inside the else – PDHide Dec 05 '20 at 01:59
  • Added full answer , please use collection runner to run the script – PDHide Dec 05 '20 at 03:11