postmanlabs / newman

Newman is a command-line collection runner for Postman
https://www.postman.com
Apache License 2.0
6.88k stars 1.16k forks source link

feat: `--env-var` for library usage #2449

Closed jef closed 4 years ago

jef commented 4 years ago
  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.

DannyDainton commented 4 years ago

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"
})
jef commented 4 years ago

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.

DannyDainton commented 4 years ago

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
jef commented 4 years ago

Ahh, okay! I'll give that a go. I'll update the example in the README to reflect.

Thank you very much!

DannyDainton commented 4 years ago

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 🏆

urakagi commented 4 years ago

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.

jasonsrogers commented 9 months ago

@urakagi Thank you for your comment

In between creating the right string, ensuring github has the right secrets set, checking that github actions could talk to the deployed server

it hadn't crossed my mind that the envVar that I was sure where set correctly had to be like that

I would suggest docs to be updated to something like:

 - uses: matt-ball/newman-action@master
        with:
          collection: postman/collection.json
          environment: postman/environment.json
          envVar: '[{"key": "VAR_NAME_1", "value": "$AN_ENV_VAR"}, {"key": "VAR_NAME_2", "value": "$AN_ENV_VAR2"}]'