newman -v): 5.1.2I'd like to see a function that sets environment variables while using the environment key in options. This is good for secrets and other things.
Hey @jef
As you're using it as a library could you include something like dotenv as a workaround?
Once the dotenv module is installed, you can create a .env file and add in your secrets. This can then be used in a Newman run like this:
const newman = require('newman');
require('dotenv').config();
newman.run({
collection: '< Collection File/URL >',
environment: {
"values": [
{
"key": "password",
"value": `${process.env.PASSWORD}`,
"type": "text",
"enabled": true
}
]
},
reporters: "cli"
})
I do have something similar as your example, but I was hoping that we could have another option that abstracts that and potentially merges two environments.
For example, a Postman collection that has a commonKey that is fine to be not a secret. And then lets say you have secrets that need to be loaded into the environment per the run.
I'd think it'd be nice to do something like this:
const newman = require('newman');
require('dotenv').config();
newman.run({
collection: '< Collection File/URL >',
environment: '< Environment File/URL >',
environment-variables: {
"secret": "secretValue",
"another_secret": process.env.ANOTHER_SECRET,
}
reporters: "cli"
})
The only reason I'm suggesting something like this is so that we don't have to repeat ourselves in the environment. CLI makes this possible (at least loading from a json) and then adding env-var.
Hey @jef
Just coming back to this and trying it out again. This _should_ work in your code to use the 2 different types of variables:
const newman = require('newman');
require('dotenv').config();
newman.run({
collection: 'collection.json',
environment: 'environment.json',
envVar: [
{ "key":"secret", "value":"secretValue" },
{ "key":"another_secret", "value":`${process.env.ANOTHER_SECRET}`}
],
reporters: ["cli","htmlextra"],
reporter: {
htmlextra: {
showEnvironmentData: true
}
}
})
This is just an output image showing it using the 2 different ways to inject the variables:

Ahh, okay! I'll give that a go. I'll update the example in the README to reflect.
Thank you very much!
Hey @jef,
As this is just waiting on the docs to be updated, I'm going to close this issue out.
Thank you so much for supporting the Newman and submitting those PRs 馃弳
Hello, is the doc updated already?
Today I read the document, did this wrong thing:
newman.run({
envVar: [
{ "secret": "secretValue" }
]
})
Is the current document clear enough to let users know the correct structure? To me the {"key": ..., "value": ...} format is not that institutive.