node.jsautomationpostmannewman

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


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.


Solution

  • 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;
       }
    
          `