jel
jel

Reputation: 11

How can a Postman environment variable value be passed into an nodejs script executing Newman?

Example: When executing newman on a command line the user would do:

newman run CollectionName.json --env-var baseurl="myhost/url" --env-var user="admin" --env-var password="admin123"

How would can a parameter such as --env-var user="admin" be passed to newman when using a script like this?

// myNodeScript.js
const newman = require('newman');
const fs = require('fs');
xml2js = require('xml2js');
newman.run({
  collection: require("./PostmanCollections/CollectionName.json"),
  folder: "Sanity",
  globals: require("./postmanVariables/QAWorkspace.postman_globals.json"), 
  environment: require("./postmanVariables/A1Env.postman_environment.json"),
  exportEnvironment: "myEnv.json",
  exportGlobals: "myGlobals.json",
  reporters: "cli",
}).on('start', function (error, summary) { // ...

Upvotes: 1

Views: 2593

Answers (2)

the_real_one
the_real_one

Reputation: 467

The previous answer from @the_ccalderon is correct but to make this answer more complete, and for those like me that stumbled in here, you would do something like this:

// myNodeScript.js
const newman = require('newman');

environmentVariables = [];
environmentVariables.push(
  {
    enabled: true,
    key: "foo",
    value: "bar",
    type: 'any'
  }
);
newman.run({
  collection: require("./PostmanCollections/CollectionName.json"),
  // ...
  envVar: environmentVariables,
  // ...
});

I am writing a wrapper script around Newman that parses some of my own CLI parameters before doing my Newman runs (using yargs to do so) and can verify that this works.

Upvotes: 1

the_ccalderon
the_ccalderon

Reputation: 2084

In your postman_environment.json you can specify the following:

{
  "id": "a14232bb-48d3-3494-7e6f-f4f34e6331a4",
  "name": "testEnv",
  "values": [
    {
      "enabled": true,
      "key": "baseurl",
      "value": "myhost/url",
      "type": "text"
    },
    {
      "enabled": true,
      "key": "user",
      "value": "admin",
      "type": "text"
    }
  ],
  "timestamp": 1504039485918,
  "_postman_variable_scope": "environment",
  "_postman_exported_at": "2017-08-29T20:44:53.396Z",
  "_postman_exported_using": "Postman/5.1.3"
}

Upvotes: 1

Related Questions