Unable to update the environment variable file via newman on NodeJS while running multiple collections

313 views Asked by At

I am running multiple collections in NodeJs via postman,I am trying to share the clientHandshakeToken variable set in the first collection across multiple Postman collections using Newman but when second collection runs, it doesn't get the values of first collection instead, it takes the initial value

const newman = require('newman');

const collections = [
    './IndividualCollection/SignIn.postman_collection.json',
    './IndividualCollection/WorkSpace.postman_collection.json',

];
const environmentFile = './FeatureTesting_Variables.postman_environment.json'


collections.forEach((collections) => {

    newman.run( {
      collection: collections,
      environment: environmentFile,
     
      reporters: ['cli', 'htmlextra', 'json'],
      reporter: {
  
      }, 
      color: 'auto', // For colored output
    })    
})

I tried by assigning the clientHandshakeToken as global variable , but I am unable to achieve it since when second collection run, it isn't taking the global variable, instead it is taking the initial value from the environment variable.

2

There are 2 answers

0
Shahroz Ahmad On BEST ANSWER

I found the solution by making the asynchronous code synchronous:

const newman = require("newman");

const collections = [
 "./IndividualCollection/SignIn.postman_collection.json",
 "./IndividualCollection/WorkSpace.postman_collection.json",
 "./IndividualCollection/Project.postman_collection.json",
 "./IndividualCollection/Bug.postman_collection.json",
 ];
  (async () => {
  let environmentData = JSON.parse(
   await fs.readFile(
  "./FeatureTesting_Variables.postman_environment.json",
  "utf8"
    )
   );

  for (const collection of collections) {
const summary = await runCollection(collection, environmentData);
if (summary) {
  environmentData = updateEnvironmentData(environmentData, summary);
    }
   }

  await fs.writeFile(
   "./FeatureTesting_Variables.postman_environment.json",
  JSON.stringify(environmentData, null, 2)
   );
    console.log("All collections executed and environment file 
     updated.");
     })();

  function runCollection(collection, environmentData) {
  return new Promise((resolve, reject) => {
   newman.run(
  {
    collection: collection,
    environment: environmentData,
    reporters: ["cli", "htmlextra", "json"],
    reporter: {},
    color: "auto",
  },
  (err, summary) => {
    if (err) {
      console.error(`Error running collection: ${err}`);
      reject(err);
    } else {
      resolve(summary);
       }
      }
     );
   });
  }

  function updateEnvironmentData(environmentData, summary) {
  const collectionEnvironment = summary.environment.values;
  environmentData.values = collectionEnvironment.map((variable) => ({
   key: variable.key,
   value: variable.value,
  enabled: true,
  }));
   console.log(
   "Environment variables updated for collection:",
    summary.collection.name
    );
   return environmentData;
   }

      `
0
mikee On

In this scenario, the environment files that you are passing to Newman are for all intents and purposes read only.

They will not persist between the Newman calls in your forEach loop.

If you update an environment variable in your collection code, it will only apply to that instance of Newman.

When you call Newman the second time, the environment file will not have changed and will still have the initial values.

It's the same issue if you want to persist environment variables if you call Newman through the command line.

This is why its happening. Therefore what you are describing is expected behaviour.

I don't really have an alternative way of doing this. There is an API for the Postman Cloud that allows you to update Environments variables but that probably won't help in this scenario.