Order of execution within a collection

In my Postman collection, I have the below structure

Collection 
|-> Scripts --> Pre-Scripts -->pm.environment.set("AuthToken", "X");
|->Group folder
  |-> Sub folder
    |->request-1
      |-> Scripts --> Pre-request --> pm.environment.unset("AuthToken");
    |->Token generation request
      |->Scripts --> Post-Scripts  --> pm.environment.set("AuthToken", "Y");
    |->Reqest-2
      |->Headers --> Authorization:{{AuthToken}}

At this level I expect the “AuthToken” to be “Y”, but this is getting always the value of “X” Why is that and how to get this sorted?

Hi @asanke. Welcome to the Postman Community.

Can you log the value of Y in the token generation request in the console and see if it indeed returns a value? Ideally, if all looks good, AuthToken should be the value of Y n your last request.

Alternatively, if you could use a different variable name to store Y to test and see if that works as against updating the value of AuthToken.

Hi @gbadebo-bello ,

Thanks for the response.

I did a bit of console.logs.
So I added a code similar to the folowing to all the folders and requests Pre-request and Post-response.
console.log("[Demo-X-Y].[Scripts].[Pre-response]" + pm.environment.get("AuthToken"));

And here is what I got in the exact order.

It appeasrs to befor every request all the porst requests are getting executed along the hierarchy from top to bottom. So the only instance that value “Y” is assigned to the variable is at the Post-response of the Token generation request.

Is there a way to strictly limit the order to

  1. [Demo-X-Y].[Screipts].[Pre-response]
  2. [Group Folder].[Screipts].[Pre-response]
  3. [Sub Folder].[Screipts].[Pre-response]
  4. [request-1].[Screipts].[Pre-response]
  5. [request-1]
  6. [request-1].[Screipts].[Post-response]
  7. [Token generation request].[Screipts].[Pre-response]
  8. [Token generation request]
  9. [Token generation request].[Screipts].[Post-response]
  10. [request-2].[Screipts].[Pre-response]
  11. [request-2]
  12. [request-2].[Screipts].[Post-response]
  13. [Sub Folder].[Screipts].[Post-response]
  14. [Group Folder].[Screipts].[Post-response]
  15. [Demo-X-Y].[Screipts].[Post-response]

Thanks

Hi @asanke. I am not sure I understand the question here correctly.

Pre-request script runs before a request is run and post-response script run after a response is gotten. What order are you trying to re-order or limit?

Hi @gbadebo-bello
Sorry for the confusion.

So as I observe, before every request, postman runs all the pre request scripts along the hierarchy. ( Check the screenshot in my previous response). That would reset the value to X before each request.

The order I observed is …

  1. [Demo-X-Y].[Screipts].[Pre-response] → Vallue is “X”
  2. [Group Folder].[Screipts].[Pre-response]
  3. [Sub Folder].[Screipts].[Pre-response]
  4. [request-1].[Screipts].[Pre-response] → Value unset
  5. [Demo-X-Y].[Screipts].[Post-response]
  6. [Group Folder].[Screipts].[Post-response]
  7. [Sub Folder].[Screipts].[Post-response]
  8. [request-1].[Screipts].[Post-response]
  9. [Demo-X-Y].[Screipts].[Pre-response] → Vallue is set back to “X”
  10. [Group Folder].[Screipts].[Pre-response]
  11. [Sub Folder].[Screipts].[Pre-response]
  12. [Token generation script].[Screipts].[Pre-response]
  13. [Demo-X-Y].[Screipts].[Post-response]
  14. [Group Folder].[Screipts].[Post-response]
  15. [Sub Folder].[Screipts].[Post-response]
  16. [Token generation script].[Screipts].[Post-response] → Value is to “Y”
  17. [Demo-X-Y].[Screipts].[Pre-response] → Vallue is set back to “X”
  18. [Group Folder].[Screipts].[Pre-response]
  19. [Sub Folder].[Screipts].[Pre-response]
  20. [request-2].[Screipts].[Pre-response] → Value is “X” ( I wanted this to be “Y”
  21. [Demo-X-Y].[Screipts].[Pre-response]
  22. [Group Folder].[Screipts].[Pre-response]
  23. [Sub Folder].[Screipts].[Pre-response]
  24. [request-2].[Screipts].[Pre-response]
    Thanks
    Asanke

Any script authored at the folder or at the collection level will run against every request in that folder or collection. I think this could be the reason why the value keeps updating on every iteration.

If your intention is not to run the pre-request script against every single request, then I will recommend that you do not author them at the collection or folder level.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.