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);"
]
}
}
],