Make assertions of same endpoint but with different parameter values

Hi,

I am trying to make multiple assertions on the same endpoint with different query parameters.
E.g.
Get - http://testing.com/test?testdate={{testDateParam}}
where {{testDateParam}} = [‘May20’, ‘May20th’, ‘2018-05-20’, ‘20-05-2018’]

How can I sequentially put the testDateParam in the request and make an assertion for status 200 without creating separate request for each test data ?

Ah - this is a classic use case for using environment variables or local variables in script to track iteration and using postman.setNetRequest to control flow.

can you give an example of postman.setNextRequest in the above scenario ?

Hi @Codejockey7,

As you know that the postman.setNextRequest command is used to carve out workflows by essentially setting the next request to the desired one.

Also, the setNextRequest command works only in collection runners, so it will be required that you create a collection and place your request in that collection before adding the pre/post request test scripts

In your case we would use postman.setNextRequest and set it to the same request again and again until all the elements of your array have been tested. For this to execute correctly we would need (NOTE- the variable names are in bold, these are the same names which will be used in the example below)

  1. index - used to track the current element (will be stored as an environment variable with value equal to 0 )

  2. test-arr - the array of query parameters which in your case will be [‘May20’, ‘May20th’, ‘2018-05-20’, ‘20-05-2018’] (will be stored as an environment variable)

  3. test-val - an empty environment variable which will act as a placeholder for each element that we extract from the array.

Below are the sample request and pre/post request test scripts (NOTE - the request name is temp)

REQUEST ( request name - temp)

https://postman-echo.com/get?test={{test-val}}

PRE-REQUEST SCRIPT

   var index = Number(pm.environment.get("index"));  // getting the current index value
   var paramArray = JSON.parse(pm.environment.get("test-var")); // accessing the array
   var lengthOfArray = paramArray.length  // getting length of array

   if(index < lengthOfArray) { 
       // taking out a single element from the array if index satisfies the condition
       var testParam = paramArray[index]; 
       // setting the query parameter to the selected element
       pm.environment.set("test-val",testParam);
   }

TEST SCRIPT

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

    var index = Number(pm.environment.get("index")) + 1; // index increment
    var paramArray = JSON.parse(pm.environment.get("test-var"));
    var lengthOfArray = paramArray.length;
    
    // check if all elements have been tested
    if(index < lengthOfArray) {
        pm.environment.set("index", index); // storing the current index
        postman.setNextRequest('temp'); // running the same request again
    }

TEST COLLECTION FOR REFERENCE
Run in Postman

1 Like