3

I'm running Newman in a Node Script. The collection has environment variables such as {{claimNum}} that would increment with each post by the test.

Example:

I have this collection request body

 <ClaimIdentification>
      <company>0001</company>
      <office>100</office>
      <claimNum>{{claimNum}}</claimNum>
 </ClaimIdentification>

And in the global environment JSON:

  {
  "enabled": true,
  "key": "claimNum",
  "value": "15200",
  "type": "text"
},

And the collection has this test:

pm.globals.set("claimNum", + pm.globals.get("claimNum") + 1);

But when ran in the script, the global.json file won't be modified and the "value" would stay the same. When this same parameters are run in the desktop app, it works.

Is there a solutions for this, should this work?

UPDATE1:

This is the newman script:

  collection: require('${__dirname}/ThirdServiceInjured.json'),
  reporters: 'json',
  globals: require('${__dirname}/globals.json')

}).on('done', function (err, summary) {
  if (err || summary.error) {
      console.error('>>> ERROR - Collection run failed.');
  }
  else {
    console.log(success('Collection run completed:'));  
    console.log(summary.run.executions[0].response.text());
  }
});

UPDATE 2:

Using this script and still not writing over the enviroment:

const newman = require('newman'); // require newman in your project
const fs = require('fs');
const envName = '${__dirname}/environment_qbe600.json';
const env = require('${__dirname}/environment_qbe600.json');

newman.run({
  collection: require('${__dirname}/ThirdServiceInjured.json'),
  reporters: 'cli',
  environment: envName,
  iterationCount: 3

}).on('done', function (err, summary) {
  if (err || summary.error) {
      console.error('>>> ERROR - Collection run could failed.');
}
else {
  const newmanValue = summary.environment.values.members[0].value;
  env.values[0].value = newmanValue;
  console.log(summary.run.executions[0].response.text());
  fs.writeFile(envName, JSON.stringify(env, null, 2), function (err) {
    if (err) return
  })
}
});

UPDATE 3:

This is the enviroment:

{
  "id": "ecabb925-829e-69f8-2348-f71dc76c0e87",
  "name": "Test",
  "values": [
    {
      "enabled": true,
      "key": "host",
      "value": "${___server}",
      "type": "text"
    },
    {
      "enabled": true,
      "key": "company",
      "value": "0001",
      "type": "text"
    },
    {
      "enabled": true,
      "key": "claimNbr",
      "value": "14600",
      "type": "text"
    },
    {
      "enabled": true,
      "key": "dni",
      "value": "150",
      "type": "text"
    },
    {
      "enabled": true,
      "key": "cost",
      "value": "107000",
      "type": "text"
    },
    {
      "enabled": true,
      "key": "testNum",
      "value": "157",
      "type": "text"
    }
  ],
  "timestamp": 1515789551332,
  "_postman_variable_scope": "environment",
  "_postman_exported_at": "2018-01-12T20:39:14.795Z",
  "_postman_exported_using": "Postman/5.5.0"

And the test section from the collection:

"event": [
    {
        "listen": "test",
        "script": {
            "type": "text/javascript",
            "exec": [
                "pm.environment.set(\"claimNbr\", +pm.environment.get(\"claimNbr\") + 1);",
                "pm.environment.set(\"testNum\", +pm.environment.get(\"testNum\") + 1);",
                "pm.environment.set(\"dni\", +pm.environment.get(\"dni\") + 1);",
                "pm.environment.set(\"cost\", +pm.environment.get(\"cost\") + 1000);"
            ]
        }
    }
],
Nicolás A.
  • 523
  • 4
  • 10
  • Is that valid syntax for the variable? Would `pm.globals.set("claimNbr", pm.globals.get("claimNbr") + 1)` not be what you need? – Danny Dainton Jan 11 '18 at 15:10
  • Mine is the syntax from the JSON when exported. But yes, on the desktop app is: `pm.globals.set("claimNum", +pm.globals.get("claimNum") + 1);` – Nicolás A. Jan 11 '18 at 18:42
  • Are you able to update the question with your node script using Newman please? – Danny Dainton Jan 11 '18 at 19:15
  • @DannyDainton done, sorry for the delay! – Nicolás A. Jan 12 '18 at 18:24
  • Thank you. I added an answer with something basic that worked for me. – Danny Dainton Jan 12 '18 at 18:31
  • Re-reading your question again - when you run the test using those files, the globals.json file won’t change or be updated. It’s a static file so it will always be a specific value, the test would just work from that value each time and increase the number of the variable, only during the execution of the script. – Danny Dainton Jan 13 '18 at 08:26

3 Answers3

1

What you are looking for is this option :

--export-globals <path>

When calling newman add this option to have your globals.json to be updated.

Sergej Lopatkin
  • 1,071
  • 9
  • 11
  • 1
    Depending on the newman implementation being used - The `--export-globals` flag *might* not work as this is only used on the CLI. This is the link to the [Newman API](https://github.com/postmanlabs/newman#api-reference) where you may need to use either the `options` or `summary` object in your script. – Danny Dainton Jan 12 '18 at 10:04
1

Having looked at the following recent issue on the Postman Github account

If you change the global variable to be a environment variable that would allow you to see the updated value each time it's run. I just changed your test code to environment rather than global.

pm.environment.set("claimNum", + pm.environment.get("claimNum") + 1)

I created a basic node script to run Newman with the Collection and Environment files exported from Postman:

var newman = require('newman')

newman.run({
    collection: `${__dirname}/default_collection.json`,
    environment: `${__dirname}/default_environment.json`,
    reporters: 'cli',
    iterationCount: 10
  }, (err) => {
    if (err) { throw err }
  })

My two files referenced in the newman script:

default_collection.json

{
    "info": {
        "name": "Test_Collection",
        "_postman_id": "d08f1b36-591a-f25f-aaaa-4368ca966fb4",
        "description": "",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
        {
            "name": "Test_Request",
            "event": [
                {
                    "listen": "test",
                    "script": {
                        "id": "068b6634-e310-46e9-86fc-265376c65ff6",
                        "type": "text/javascript",
                        "exec": [
                            "console.log(pm.environment.get('claimNum'))"
                        ]
                    }
                }
            ],
            "request": {
                "method": "POST",
                "header": [
                    {
                        "key": "Content-Type",
                        "value": "application/json"
                    }
                ],
                "body": {
                    "mode": "raw",
                    "raw": "{\n\t\"id\": {{claimNum}}\n}"
                },
                "url": {
                    "raw": "localhost:3000/test",
                    "host": [
                        "localhost"
                    ],
                    "port": "3000",
                    "path": [
                        "test"
                    ]
                },
                "description": ""
            },
            "response": []
        }
    ],
    "event": [
        {
            "listen": "prerequest",
            "script": {
                "id": "4c1d562f-e4d5-4017-8f25-48ac0b6aa3fc",
                "type": "text/javascript",
                "exec": [
                    ""
                ]
            }
        },
        {
            "listen": "test",
            "script": {
                "id": "026c029e-a427-4624-b196-3d8982a103f1",
                "type": "text/javascript",
                "exec": [
                    "pm.environment.set(\"claimNum\", + pm.environment.get(\"claimNum\") + 1)"
                ]
            }
        }
    ]
}

default_environment.json

{
  "id": "539073e6-adab-f812-95b0-82d4d82ce4b2",
  "name": "test_environment",
  "values": [
    {
      "enabled": true,
      "key": "claimNum",
      "value": "9",
      "type": "text"
    }
  ],
  "timestamp": 1515782483636,
  "_postman_variable_scope": "environment",
  "_postman_exported_at": "2018-01-12T18:42:04.661Z",
  "_postman_exported_using": "Postman/5.5.0"
}

I added iterationCount: 10 to the script so that I could see the claimNum increase on each iteration.

Newman Test Run

In Postman, I added a simple console.log() to write out the claimNum on each test run - You can see the output in the image above.

console.log(pm.environment.get('claimNum'))

My request body in using JSON but the {{claimNum}} would work the same with XML like in your example.

This one solution to the problem but as it's using not using a global variable and I'm unsure of your requirement - It might not be what you're after.


UPDATE 1

Depending on the use case that you have - The environment file can be updated to show the last run value and can use this value in the next request - So that it's not repeatedly running a test against the same value in the static file. Running the following code would run the test and then update the value in the default_environment.json file.

const newman   = require('newman')
const fs       = require('fs');
const fileName = `${__dirname}/default_environment.json`;
const file     = require(fileName);

newman.run({
    collection: `${__dirname}/default_collection.json`,
    environment: `${__dirname}/default_environment.json`,
    reporters: 'cli',
    iterationCount: 3
}).on('done', function (err, summary) {
  if (err || summary.error) {
    console.error('>>> ERROR - Collection run failed.');
  }
  else {
    const newmanValue = summary.environment.values.members[0].value
    file.values[0].value = newmanValue

    fs.writeFile(fileName, JSON.stringify(file, null, 2), function (err) {
      if (err) return
    })
  }
}) 

My environment file would look something like this following the test run:

{
  "name": "test_environment",
  "values": [
    {
      "enabled": true,
      "key": "claimNum",
      "value": 19,
      "type": "text"
    }
  ]
}

File Update


UPDATE 2

Having modified the code again with the new information this is working locally for me:

Newman Run Script

const newman  = require('newman')
const fs      = require('fs')
const envName = `${__dirname}/default_environment.json`
const env     = require(envName)

newman.run({
  collection: require(`${__dirname}/default_collection.json`),
  reporters: 'cli',
  environment: envName,
  iterationCount: 3

}).on('done', function (err, summary) {
  if (err || summary.error) {
      console.error('ERROR - Collection run could failed')
}
else {
  const claimNbr  = summary.environment.values.members[0].value
  const testNum   = summary.environment.values.members[1].value
  const dni       = summary.environment.values.members[2].value
  const cost      = summary.environment.values.members[3].value

  env.values[0].value = claimNbr
  env.values[1].value = testNum
  env.values[2].value = dni
  env.values[3].value = cost

  fs.writeFile(envName, JSON.stringify(env, null, 2), function (err) {
    if (err) return
  })
  console.log('Collection run complete') 
}
})

The test section in the Collection file

    "name": "Test_Request",
    "event": [
        {
            "listen": "test",
            "script": {
                "id": "a407e1e2-4961-4c65-af1b-22190b1ab0cc",
                "type": "text/javascript",
                "exec": [
                    "pm.environment.set(\"claimNbr\", + pm.environment.get(\"claimNbr\") + 1)",
                    "pm.environment.set(\"testNum\", + pm.environment.get(\"testNum\") + 1)",
                    "pm.environment.set(\"dni\", + pm.environment.get(\"dni\") + 1)",
                    "pm.environment.set(\"cost\", + pm.environment.get(\"cost\") + 1000)",
                    "",
                    "console.log(JSON.stringify(pm.response.json()))"
                ]
            }
        }
    ]

Postman Request Body

Postman Request Body

Script Running and Updating the environment file

Newman Run

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • I'm sorry, how can I call the `--export-enviroment` in the script? – Nicolás A. Jan 12 '18 at 21:27
  • I’m not sure that you would need too. Those flags are only useful if you’re running this from the command line, you’re running it from a node script. In your script you have `globals:` change that for `environment:` and also add an environment.json file where your global.json file. Ensure that you do the same for your test too. – Danny Dainton Jan 12 '18 at 21:39
  • Check your syntax in the test it should read `pm.environment` and not `pm.globals` – Danny Dainton Jan 12 '18 at 21:49
  • Adding an update to change the value in the environment file after each run. – Danny Dainton Jan 15 '18 at 12:08
  • Thanks for the patience. I followed the instructions but the enviroment is still not being written (I'm not using globals anymore). Also the body response is the only useful thing I need, is there any way to log it better than: `console.log(summary.run.executions[0].response.text());` ? – Nicolás A. Jan 15 '18 at 13:38
  • Depending on what you're trying to do - Try adding `console.log(pm.response.text())` or use `.json()` to the `Tests` tab of the request in the collection. That would show the response body for each request in the console. I'm not sure that information is contained in the newman summary object. – Danny Dainton Jan 15 '18 at 13:58
  • In your recent update you need to ensure that the `env` , `envName` and `collection:` strings are all using backticks ( ` ) rather than single quotes ( ' ) apart from that, I ran it locally and it worked. – Danny Dainton Jan 15 '18 at 14:26
  • Just to confirm, Have you changed the test syntax in the request to be `pm.environment.set` rather than `pm.globals.set`? – Danny Dainton Jan 16 '18 at 11:14
  • Yes Danny, I changed the syntax in everything, even using the backticks and still no luck. I'll update with the enviroment and tests from the collection. – Nicolás A. Jan 16 '18 at 12:57
  • Added a final update - The code to get and update the values is horrible but it will work and would need to be refactored. If this doesn't work for you, i'm out of ideas. – Danny Dainton Jan 16 '18 at 15:10
  • Works wonders, thanks a lot! I had to re-arrange the enviroments in order for some values to work, but now it's perfect. Thanks for the patience and godspeed – Nicolás A. Jan 16 '18 at 16:46
  • Yay! Ah yeah, you have the `host` value too so the numbers would need to be moved down. It was an ordeal but I learnt a lot too. Glad it's doing what you want or at least something that you can work with. :) – Danny Dainton Jan 16 '18 at 17:12
0

Although the Newman API Ref doesn't mention it, the options object you pass to newman.run does have support for writing out both global and environment vars in the same manner as the newman cli. The tip was found in the newman issues list.

newman.run({
    collection: '/path/to/collection.json',
    environment: require('/path/to/env'),
    exportEnvironment: '/path/to/env.json',
    globals: require('/path/to/global'),
    exportGlobals: '/path/to/global.json'
})
    .on('done', (err, summary) => {
        console.log(summary.environment.values);
    })

Also Note, for some weird reason other event listeners like: .on('beforeItem',() => {}) and .on('item',() => {}) don't have the property summary.environment

sansSpoon
  • 2,115
  • 2
  • 24
  • 43
  • Is this something you've tried (late to the party, not sure if you are still active sansSpoon) or is it documentation knowledge only? – BenPen Dec 13 '21 at 15:03