I am running a collection from Newman using environment variables stored with Postman and trying to update the variables in the tests, after one of the requests, but it does not update the initial value and the value used in the next collection run is retrieved from the **initial values". Is that the proper behavior or am I doing something wrong?
Hey @ConstantinMalii
Welcome to the Postman community!
Would you be able to provide some information about your Newman run please? More specifically, how are you accessing the Collection and Environment files.
For all variables, the initial value is the one that’s syncd to the Postman servers and can be seen by you and your team members.
The current value is only seen by you and is not syncd to the Postman servers.
If you had variables only as the current value, this wouldn’t be part of any exported file so Newman wouldn’t be able to pick that up and use it for the run.
It would be interesting to see your set up and how you’re running the collections before I go into more detail though
Hey @danny-dainton
Thanks for checking this out!
I have setup Newman using the JS package https://www.npmjs.com/package/newman and the collection and env var are retrived by their GUID.
newman.run(
{
collection: `https://api.getpostman.com/collections/${constants.MCC_COLLECTION_GUID}?apikey=${apiKey}`,
folder: requests,
iterationData: constants.ITERATION_DATA_FILE_NAME,
bail: true,
insecure: true,
reporters: ["cli"],
environment: `https://api.getpostman.com/environments/${constants.MCC_ENVIRONMENT_GUID}?apikey=${apiKey}`,
}
we run this script from bash or powershell command line.
The env vars are being updated in the Tests part of the request like so:
postman.setEnvironmentVariable("varName1", jsonData.data.Value1);
postman.setEnvironmentVariable("varName2", jsonData.data.Value2);
All these variables have default values so that they are exported, but I can see the value being updated only for current, but not initial.
Side note - this is separate collection runs. First collection run should update the vars and the next collection run should use those values.
As you’re grabbing that from the Postman API - That would only know about the value set in initial, as the current value doesn’t get pushed to the Postman server.
I created a reporter that might give you a few about the variables used during the run
I’ve included a flag that displays the environment variables on the report.
Not sure it’s going to give you the full answer but might highlight what’s happening maybe
oh, i see. so those vars are not being tracked in any way. that would make sense.
is there another way load those env vars so that they are tracked?
It depends what you mean by ‘tracked’
You can load environment vars, that are separate to the environment URL, in the Newman object like this:
envVar: [
{ "key":"var_name_1", "value":"var_value_1" },
{ "key":"another_secret", "value":`${process.env.ANOTHER_SECRET}`}
],
That would also allow you to use system level environment variables that you want to keep hidden, like a Password or an API Key.
hi @danny-dainton , all the exported environment variables are stored in json file. When I put the environment variable in the way you proposed
envVar: [
{ "key":"var_name_1", "value":"var_value_1" },
{ "key":"another_secret", "value":`${process.env.ANOTHER_SECRET}`}
]
the ${process.env.ANOTHER_SECRET}
is throwing syntax error cuz json doesn’t recognize the way we define variable here. What is the way you usually handle this? Thanks!!!
Best
Can you show the error message please?
Have you added this to a node script, using Newman as a library?
Hi @danny-dainton thank you for getting back to me. The issue was solved. But I would love to share what’s happened:
I was trying to add this environment variable to the json file which is generated by exporting the postman environment variable. For security reason, I shouldn’t store password in json file and need to leverage system/CI environment variable.
Initially I just tried this way (mentioned in earlier comment)
envVar: [
{ "key":"var_name_1", "value":"var_value_1" },
{ "key":"another_secret", "value":`${process.env.ANOTHER_SECRET}`}
]
however, json file doesn’t notion variable so it shows like this in my VS code.
I searched online and it’s proposing to add it to node module, which I think inconvenient.
Then I noticed description on --env-var to specify the environment variable from newman cli. (from newman npm page)
* `--env-var "<environment-variable-name>=<environment-variable-value>"`
Allows the specification of environment variables via the command line, in a key=value format. Multiple CLI environment variables can be added by using `--env-var` multiple times, like so: `--env-var "foo=bar" --env-var "alpha=beta"` .
So basically no need to define system env variable in that way ( ${process.env.ANOTHER_SECRET}
) we just need to append this --env-var “xxx=variable(CI/environment)” to newman cli command where we trigger the test. Problem solved. Hope this is helpful for other people.
That code snippet was never intended to be used/copied into an exported environment file
, it just wouldn’t work and it would always think that it’s a string value. The JSON syntax error that you saw would be because the { }
characters were not escaped.
The envVar
property is used with a node.js script when using Newman as a library:
const newman = require('newman');
newman.run({
collection: 'collection.json',
envVar: [
{
"key":"secret",
"value":`${process.env.SECRET}`
}
]
})
The --env-var
flag is used with Newman from the command line:
newman run collection.json --env-var "secret=$SECRET"