我在节点脚本中运行 Newman。该集合具有环境变量,例如随着{{claimNum}}
测试的每个帖子而增加。
示例:
我有这个收集请求正文
<ClaimIdentification>
<company>0001</company>
<office>100</office>
<claimNum>{{claimNum}}</claimNum>
</ClaimIdentification>
而在全局环境JSON 中:
{
"enabled": true,
"key": "claimNum",
"value": "15200",
"type": "text"
},
该系列有这个测试:
pm.globals.set("claimNum", + pm.globals.get("claimNum") + 1);
但是在脚本中运行时,global.json 文件不会被修改,“值”将保持不变。在桌面应用程序中运行相同的参数时,它可以工作。
有没有解决方案,这应该工作吗?
更新1:
这是纽曼脚本:
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());
}
});
更新 2:
使用这个脚本并且仍然没有覆盖环境:
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
})
}
});
更新 3:
这是环境:
{
"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"
以及集合中的测试部分:
"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);"
]
}
}
],