Postman – Looping multiple requests with different data

Good day, Friends!

We are going to discuss how we can execute multiple request monthly or yearly based on the specific event dates in postman.

Problem Statement :

Suppose we have to test scenario where we need to execute multiple requests Monthly and few request Annually based on the event dates in postman.

Solution Approach:

we can start preparing list of event dates on which we need to execute monthly requests and another list of event dates on which we need to execute annually requests.

Let’s create list of Monthly Event dates:

“2017-Jan-22″,”2017-Feb-22″,”2017-Mar-22″,”2017-Apr-22″,”2017-May-22″,”2017-Jun-22″,”2017-Jul-22″,”2017-Aug-22″,”2017-Sep-22″,”2017-Oct-22″,”2017-Nov-22″,”2017-Dec-22″,”2018-Jan-22″,”2018-Feb-22″,”2018-Mar-22″,”2018-Apr-22″,”2018-May-22″,”2018-Jun-22″,”2018-Jul-22″,”2018-Aug-22″,”2018-Sep-22″,”2018-Oct-22″,”2018-Nov-22″,”2018-Dec-22″,”2019-Jan-22″,”2019-Feb-22″,”2019-Mar-22″,”2019-Apr-22″,”2019-May-22″,”2019-Jun-22″,”2019-Jul-22″,”2019-Aug-22″,”2019-Sep-22″,”2019-Oct-22″,”2019-Nov-22″,”2019-Dec-22″,”2020-Jan-22”

and list of Annual Event dates:

“2017-Jan-22″,”2018-Jan-22″,”2019-Jan-22″,”2020-Jan-22”

Now, assume that we have 3 Postman requests which we need to execute on every monthly event date and 2 postman requests which we need to execute on every annual event dates.

We need to add below Code Snippets in first Monthly postman request’s Pre-Request Script:

console.log("in Monthly Request 1");
let EVENT_DATE_LIST = pm.globals.get("EVENT_DATE_LIST");

if(!EVENT_DATE_LIST || EVENT_DATE_LIST.length == 0) {
    EVENT_DATE_LIST = ["2017-Jan-23","2017-Feb-22","2017-Mar-22","2017-Apr-24","2017-May-22","2017-Jun-22","2017-Jul-24","2017-Aug-22","2017-Sep-22","2017-Oct-23","2017-Nov-22","2017-Dec-22","2018-Jan-22","2018-Feb-22","2018-Mar-22","2018-Apr-23","2018-May-22","2018-Jun-22","2018-Jul-23","2018-Aug-22","2018-Sep-24","2018-Oct-22","2018-Nov-22","2018-Dec-24","2019-Jan-22","2019-Feb-22","2019-Mar-22","2019-Apr-22","2019-May-22","2019-Jun-24","2019-Jul-22","2019-Aug-22","2019-Sep-23","2019-Oct-22","2019-Nov-22","2019-Dec-23","2020-Jan-22"];
}

let CURRENT_EVENT_DATE = EVENT_DATE_LIST.shift();
console.log("CURRENT_EVENT_DATE : " + CURRENT_EVENT_DATE);
pm.globals.set("CURRENT_EVENT_DATE", CURRENT_EVENT_DATE);
pm.globals.set("EVENT_DATE_LIST", EVENT_DATE_LIST);

this will take event dates from list of Monthly Event Dates and trigger postman request.

Now, in first & second Monthly request’s Test we need to add below Code Snippets:

if (EVENT_DATE_LIST && EVENT_DATE_LIST.length > 0){
    postman.setNextRequest("Monthly_Request2");
} else {
    postman.setNextRequest("Monthly_Request2");
}

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

it is checking list of event list and trigger next request.

Now, in third monthly request’s test we will add condition of Annual Event Dates by adding below Code Snippets in Test Script:

const EVENT_DATE_LIST = pm.globals.get("EVENT_DATE_LIST");
const ANNUAL_EVENT_DATES = pm.globals.get("ANNUAL_EVENT_DATES");
const CURRENT_EVENT_DATE= pm.globals.get("CURRENT_EVENT_DATE");
if (EVENT_DATE_LIST && EVENT_DATE_LIST.length > 0){
    if(ANNUAL_EVENT_DATES.includes(CURRENT_EVENT_DATE)){
        postman.setNextRequest("Annual_Request1");
    }
    else{
        postman.setNextRequest("Monthly_Request1");
    }
    
} else {
    postman.setNextRequest(null);
}

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

so it will check if current event date is present in Annual_Event_Dates, then trigger Annual Request else First Monthly Request.

Now , in First Annual Request’s Test script we will add below Code Snippets to trigger Second Annual Request :

const EVENT_DATE_LIST = pm.globals.get("EVENT_DATE_LIST");
if (EVENT_DATE_LIST && EVENT_DATE_LIST.length > 0){    
    postman.setNextRequest("Annual_Request2");  
} else {
    postman.setNextRequest("Annual_Request2");
}

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

So, we are almost done but we need to trigger First Monthly Request again to start second year monthly cycle.

so, we need to add below Code Snippets in second annual request’s Test Script:

const EVENT_DATE_LIST = pm.globals.get("EVENT_DATE_LIST");
if (EVENT_DATE_LIST && EVENT_DATE_LIST.length > 0){    
    postman.setNextRequest("Monthly_Request1");  
} else {
    postman.setNextRequest(null);
}

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

Finally, we have reached our complete solution

Please find below Postman Collection for your ready reference

Please find below snapshot for Global variables:

Global Variable setup

Further Enhancements:

Suppose event dates falls on weekend and we need to run request on Business Days (Monday-Friday) only, then with the help of Excel Formula we can set next business date and achieve our requirement.

Please find below snapshot for your reference:

Using Excel to remove weekend dates

Please check above solution and let me know your view.

If you are facing any other issue in postman collection, please do write to me with problem statement, we will work on problem and provide the better solution.

Leave a Comment

Your email address will not be published. Required fields are marked *