简体   繁体   中英

How to pass token value from one collection to another collection in POSTMAN - AUTOMATION | NEWMAN

I want to automate my unit test cases using POSTMAN Collections API & NEWMAN. I have created two test cases, which is working as expected. After exporting the collection from POSTMAN, I am generating the test report via NEWMAN.

But there some API which have some dependency on another, means need to pass one unique ID in parameter, which is coming from another API call. So how can I pass those parameter value in JSON.

Want to pass costID in my second API call/second test case.

 { "variables": [], "info": { "name": "UNIT TEST", "_postman_id": "e518f4c6-8cd0-2484-aff3-2f2b288f938e", "description": "", "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json" }, "item": [ { "name": "Price Testing", "description": "", "item": [ { "name": "PDP", "event": [ { "listen": "test", "script": { "type": "text/javascript", "exec": [ "var jsonData = JSON.parse(responseBody);", "var keyexist = \\"YES\\";", "var key = \\"**costID**\\" in jsonData ? \\"YES\\" : \\"NO\\";", "tests[\\"costIDEXIST\\"] = key === keyexist;" "" ] } } ], "request": { "url": { raw": "my api url1", "protocol": "https", "host": [ "www", "test", "com" ], "path": [ "jio", "v4", "l232" ], "query": [ { "key": "ispwa", "value": "true", "equals": true, "description": "" } ], "variable": [] }, "method": "GET", "header": [], "body": {}, "description": "" }, "response": [] } ] }, { "name": "TEST KEY CHECK", "description": "", "item": [ { "name": "kEY EXIST OR NOT", "event": [ { "listen": "test", "script": { "type": "text/javascript", "exec": [ "var jsonData = JSON.parse(responseBody);", "var keyexist = \\"YES\\";", "var key = \\"ishEligible\\" in jsonData ? \\"YES\\" : \\"NO\\";", "tests[\\"ishEligible EXIST\\"] = key === keyexist;" ] } } ], "request": { "url": { "raw": "my api url2", "host": [ "TEST", "com" ], "path": [ "jio", "v4", "l232" ], "query": [ { "key": "**costID**", "value": "true", "equals": true, "description": "" } ], "variable": [] }, "method": "GET", "header": [], "body": {}, "description": "" }, "response": [] } ] } ] }

Yes, I got one solution by setting the costID in EnvironmentVariable. So that I can use that costID in subsequent API calls. postman.setEnvironmentVariable(\\"costID \\",jsonData.costID);

use

pm.collectionVariables.set("variable_key","variable_value");
instead of

pm.environment.set("variable_key", "variable_value"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM