Newman: feat: `--env-var` for library usage

Created on 30 Jul 2020  路  6Comments  路  Source: postmanlabs/newman

  1. Newman Version (can be found via newman -v): 5.1.2
  2. OS details (type, version, and architecture): N/A
  3. Are you using Newman as a library, or via the CLI? Library

I'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.

documentation

All 6 comments

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:

Screenshot 2020-08-08 at 07 24 09

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.

Was this page helpful?
0 / 5 - 0 ratings