在 运行 NodeJS 中的 Postman Newman 时通过测试修改环境
Modify environment with tests while running Postman Newman in NodeJS
我是节点脚本中的 运行ning Newman。该集合具有环境变量,例如 {{claimNum}}
会随着测试的每个 post 递增。
示例:
我有这个集合请求正文
<ClaimIdentification>
<company>0001</company>
<office>100</office>
<claimNum>{{claimNum}}</claimNum>
</ClaimIdentification>
并且在全球环境 JSON:
{
"enabled": true,
"key": "claimNum",
"value": "15200",
"type": "text"
},
集合中有这个 test:
pm.globals.set("claimNum", + pm.globals.get("claimNum") + 1);
但是在脚本中运行时,global.json文件不会被修改,"value"会保持不变。当相同的参数在桌面应用程序中 运行 时,它起作用了。
是否有解决方案,是否可行?
更新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);"
]
}
}
],
您正在寻找的是这个选项:
--export-globals <path>
调用 newman 时添加此选项以更新您的 globals.json
。
上的以下近期问题
如果您将 global
变量更改为 environment
变量,则每次 运行 时您都可以看到更新后的值。我刚刚将您的 test
代码更改为 environment
而不是 global
.
pm.environment.set("claimNum", + pm.environment.get("claimNum") + 1)
我使用从 Postman 导出的集合和环境文件为 运行 Newman 创建了一个基本节点脚本:
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 }
})
我在 newman 脚本中引用的两个文件:
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"
}
我将 iterationCount: 10
添加到脚本中,以便我可以看到每次迭代都会增加 claimNum
。
在 Postman 中,我添加了一个简单的 console.log()
来在每个测试中写出 claimNum
运行 - 您可以在上图中看到输出。
console.log(pm.environment.get('claimNum'))
我的请求正文使用 JSON
,但 {{claimNum}}
与 XML
的工作方式相同,就像您的示例中一样。
这是解决问题的一个方法,但因为它没有使用 global
变量,我不确定你的要求 - 它可能不是你想要的。
更新 1
根据您的用例 - 可以更新环境文件以显示最后一个 运行 值,并可以在下一个请求中使用该值 - 这样就不会重复 运行对静态文件中的相同值进行测试。 运行以下代码将 运行 测试,然后更新 default_environment.json
文件中的值。
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
})
}
})
我的 environment
文件在测试后看起来像这样 运行:
{
"name": "test_environment",
"values": [
{
"enabled": true,
"key": "claimNum",
"value": 19,
"type": "text"
}
]
}
更新 2
使用新信息再次修改代码,这对我来说在本地有效:
纽曼运行剧本
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')
}
})
合集文件中的测试部分
"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()))"
]
}
}
]
邮递员请求正文
脚本 运行正在更新环境文件
虽然 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);
})
另请注意,由于某些奇怪的原因,其他事件侦听器如:.on('beforeItem',() => {})
和 .on('item',() => {})
没有 属性 summary.environment
我是节点脚本中的 运行ning Newman。该集合具有环境变量,例如 {{claimNum}}
会随着测试的每个 post 递增。
示例:
我有这个集合请求正文
<ClaimIdentification>
<company>0001</company>
<office>100</office>
<claimNum>{{claimNum}}</claimNum>
</ClaimIdentification>
并且在全球环境 JSON:
{
"enabled": true,
"key": "claimNum",
"value": "15200",
"type": "text"
},
集合中有这个 test:
pm.globals.set("claimNum", + pm.globals.get("claimNum") + 1);
但是在脚本中运行时,global.json文件不会被修改,"value"会保持不变。当相同的参数在桌面应用程序中 运行 时,它起作用了。
是否有解决方案,是否可行?
更新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);"
]
}
}
],
您正在寻找的是这个选项:
--export-globals <path>
调用 newman 时添加此选项以更新您的 globals.json
。
如果您将 global
变量更改为 environment
变量,则每次 运行 时您都可以看到更新后的值。我刚刚将您的 test
代码更改为 environment
而不是 global
.
pm.environment.set("claimNum", + pm.environment.get("claimNum") + 1)
我使用从 Postman 导出的集合和环境文件为 运行 Newman 创建了一个基本节点脚本:
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 }
})
我在 newman 脚本中引用的两个文件:
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"
}
我将 iterationCount: 10
添加到脚本中,以便我可以看到每次迭代都会增加 claimNum
。
在 Postman 中,我添加了一个简单的 console.log()
来在每个测试中写出 claimNum
运行 - 您可以在上图中看到输出。
console.log(pm.environment.get('claimNum'))
我的请求正文使用 JSON
,但 {{claimNum}}
与 XML
的工作方式相同,就像您的示例中一样。
这是解决问题的一个方法,但因为它没有使用 global
变量,我不确定你的要求 - 它可能不是你想要的。
更新 1
根据您的用例 - 可以更新环境文件以显示最后一个 运行 值,并可以在下一个请求中使用该值 - 这样就不会重复 运行对静态文件中的相同值进行测试。 运行以下代码将 运行 测试,然后更新 default_environment.json
文件中的值。
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
})
}
})
我的 environment
文件在测试后看起来像这样 运行:
{
"name": "test_environment",
"values": [
{
"enabled": true,
"key": "claimNum",
"value": 19,
"type": "text"
}
]
}
更新 2
使用新信息再次修改代码,这对我来说在本地有效:
纽曼运行剧本
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')
}
})
合集文件中的测试部分
"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()))"
]
}
}
]
邮递员请求正文
脚本 运行正在更新环境文件
虽然 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);
})
另请注意,由于某些奇怪的原因,其他事件侦听器如:.on('beforeItem',() => {})
和 .on('item',() => {})
没有 属性 summary.environment