Run next request with new iteration data from json by using setNextRequest

Hi,

I have a case where i have a folder in a collection which has to run for a specific set of iterations before going to the next folder.

There is no direct feature in postman yet which allows you to set different iterations at each folder level and still run the collection as whole.

I want to run a specific request in a folder with all the testdata i provide in a json and then move to the next request as per the collection folders.

I tried achieving the same usning setNextRequest in the same request which has to be run multiple times but with different data.
However, it runs the same data again for the second time as well and through the runner it goes to iteration 2 which was set to 2 at collection level as well.

How can I achieve my usecase.

Hi Ra_Na

If I understand the problem statement correctly, you would like to run a request/folder for a particular iteration(s) only.

I think you achieve this by using pm.info.iteration. You can find more information about this here

Your logic can be something like

let iteration = pm.info.iteration,
    // whatever iterations for which you would like to execute the specific request 
     conditionalIterations = [0, 5, 7]; 
if (_.includes(conditionalIterations, iteration)) {
  // if you instead have a specific folder, then put the first request in that folder here
  pm.sendNextRequest('your_specific_request');
}

Let me know if I misunderstood the problem and in that case feel free to give more information about the problem statement.

1 Like

Hi harryi3t,

Thanks for the help. I however couldnt understand

 if (_.includes(conditionalIterations, iteration)) 

condition , what is includes here. I tried running the same using collection runner. I gave iteration count as 1 for the collection bu including the above code, i couldnt understand how conditionalIterations variable is working here.

Can you elaborate more on the usage please.

Thanks

Hi harryi3t,
So i tried searching for the usage of includes, and found that , its from the loaddash javascript library.
I understood the usage in your solution, where it checks for the presence of the iteration value in the collection specified and runs the request.

However , that is not my scenario.
My scenrio is , I have a folder with 2 requests in a collection. The first POST request has to run for the whole data set provided and then the GET Request in the folder should get invoked.

So, its like suppose i have 2 test data , then , POST -> POST -> GET and then proceed to next folder.
Now i tried the following:

 let iterationCount = pm.info.iteration;
 let totalIterations = pm.info.iterationCount;

 if (_.inRange(iterationCount, totalIterations)) {
    postman.setNextRequest('POST REQUEST');
 }

What this does is, it doesnt increment the iterationCount present in the Collection Runner and keeps running iteration 0 multiple times with the same data.

Is there a way to increase the iteration count of the Collection Runner using javascript, so that new data is picked up from the testdata set provided.

Hi Ra_Na,

Sorry about the delay in this reply.
I saw the your above posted implementations. The iterations are supposed to be used for running the complete collection multiple times with different sets of data. A collection can have requests which have to executed in some order to achieve some goal.

You can have a complex workflow where one requests executes multiple times until a condition is met and then move to the next request.
But, even in that complex workflow, using the iterations would mean that running the complete collection multiple times. Think of iterations like a for loop. Every iteration runs your collection with one set of values from the iteration data. You can’t use a part of iteration data (say, the first 2 rows) for a single request and the rest of it for the remaining requests.

If you want request-1 to run multiple times and then request-2 to run a single time, the data for all 3 requests should be present in a single row of your data file. Try to organize your data as you would if you were using a for loop run your collection.

If you are still confused about this, please let me know more about your use case. I will be happy to clarify any doubts. If you give me a sample data and the expected behavior, maybe I can suggest you the right data structure.

Regards

Hi harryi3t,

The link to my updated collection: https://www.getpostman.com/collections/8fafe1d9df0d95d6ddd0
The use case is as follows:

  1. The Post_Service folder in the collection has a Post Request whose pre-request script contains the test data which has run multiple times with different data .
  2. After the request is run, the get_service request has to run once.
  3. The iteration count however is required for the entire data file [3 iteration in my case for test data in json added in the test Runner for other folders which dont require inidividual iterations for a request]

So, its like POST–>POST --> GET , more folders if added later has to run for the entire testdata in the json data given in the testRunner and the post request should not run again for any more iterations of the ones given in the test runner.

Meaning, i want to control my iterations at a folder level like i did in Post_Service folder and that folder once completed should not run again for the iterations specified in the test Runner.

Kindly let me know a way to handle this , I have handled the multiple request part for the Post_Service folder but am observing that the run happens again for the 2nd iteration which is specified in the Test Runner.

Regards

Hi @Ra_Na

As I have mentioned already try to think of the iteration data as a for loop. Structure your data such that a for loop can solve this.
I looked at your collection and I came up with this structure. You can modify it to suit your needs

[
  {
    "dataForPost": [
      {
        "name": "Raghav",
        "address": "abcd",
        "value": "Iteration 1 - Post-1"
      },
      {
        "name" : "Santosh",
        "address" : "defg",
        "value": "Iteration 1 - Post-2"
      }
    ],
    "dataForGet": [
      {
        "value": "Iteration 1 - GET-1"
      }
    ]
  }
]

This is how it will look in Postman

Now in your pre-request script, instead of using

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

You can use

let testdataset = pm.iterationData.get("dataForPost");

The key point here is to understand that iterations are like normal for loop. You cannot change for loop’s behaviour but you can always structure your data such that it works with the loop :slight_smile:

Hi @postman-harryi3t ,

Thanks for the input.

The updated link to my collection: https://www.getpostman.com/collections/8fafe1d9df0d95d6ddd0
Apologies, may be i wasnt very clear on my use case previously. So, once again on the usecase ,

  1. The Post_Service folder , POST REQUEST should run for all the testdata mentioned in the pre-request script iteration data.
  2. After it has run the post request for all the data points, the GET request should run just once.
  3. The Test folder will get the data from the collection runner test data file when running all the tests together, which will run the request for multiple iterations.

Meaning, the Test folder request , should be able to pick the test data from test runner data file whereas the Post_Service folder should pick data from the iteration data that is set in pre-request script and only iterate through that.