Search code examples
unit-testingqpostmannewman

Passing variable from one postman collection to another using command line tool Newman


I had two separate postman collection preReq.json and postReq.json which has different set of request. from preReq collection I'm getting some value which I need to pass on second collection postReq.json Both collection are using same environment file

Is there any way to pass environment variables to different collection suit

command:

newman run preReq.json -e Demo_Beta.json -k 

newman run postReq.json -e Demo_Beta.json -k

I want to set a value which i get from preReq.json to postReq.json


Solution

  • In Postman:

    You could save your dynamic value in your environment.

    In preReq.json - Tests tab:

    postman.setEnvironmentVariable("value", value);
    

    Now in your second collection postReq.json you can just reference this value with {{value}} in the URL editor.

    If you want to reference your value in tests you can use: postman.getEnvironmentVariable('value')' or postman['value'].

    After you're done with your requests & tests you can clear the variable with:

    postman.clearEnvironmentVariable("value");
    

    In newman:

    You could use the following combination:

    newman run preReq.json -e Demo_Beta.json -k --export-environment Demo_Beta.json
    
    newman run postReq.json -e Demo.Beta.json -k
    

    The problem is the exported JSON file is not correct and misses the name property. I'll open an issue for that on the newman Github repository. For now you'll have to add name property manually.