To set environment variable from API response. #647
Replies: 3 comments 6 replies
-
Hi, This might not be the best answer to this question, but one way to do this is to run an external script, using the Here's an example of what your config might look like: version: 1.0
tests:
integrationTests:
- name: /categories tests
operations:
- openApiOperationId: getAllCategories
openApiResponse: '200'
variations:
- name: SetUpEnvironment
tests:
extendTests:
- tests:
- file:./scripts/post-setEnvironmentVars.js Then your script could looks something like this: pm.test('Ensure we got an array of categories in result', function () {
pm.expect(categories,
"GET /categories endpoint did not return an object with a 'categories' array"
).to.be.an('array');
});
const categories = pm.response.json().categories;
pm.test("Set environment variable 'numCategories' to the length of the 'categories' list", function () {
pm.environment.set("numCategories", categories.length.toString());
}); When working with external scripts, I find it easiest to write something in the script file, generate the test collection and export it to Postman (skipping the newman run using the |
Beta Was this translation helpful? Give feedback.
-
The standard way is to use assignVariables, which sets the variables on the collection itself so that the requests can run standalone, without any need for environment variables. Out of curiosity: why would you want to update the environment variables instead of the collection variables? |
Beta Was this translation helpful? Give feedback.
-
Is there any way to set global or environment variable with newman run? |
Beta Was this translation helpful? Give feedback.
-
How to set environment variable from API response in Portman?
We are having "collectionVariables" property to set variable at collection level but I want to use variable in multiple collections so I need to set as a environment variable.
Example: I have two swagger json 1) Register and 2) Risk.
When create register then I got RegisterId that I need to use in Create Risk API.
Since these are separate collections, the "collectionVariables" property won't work for sharing variables between them.
Thanks..
Beta Was this translation helpful? Give feedback.
All reactions