合并 env JSON 文件以进行 newman run

问题描述 投票:0回答:1

在我的项目中,我们为集合的每个 post 请求维护多个 JSON 文件。请求方面,我们需要这样维护以保存环境变量。我们将通过新人来运行。

我的要求是想要合并所有环境json文件并将其作为单个文件。然后,用源码(集合文件)执行它

有什么方法可以通过任何cmd注释和任何方法来合并文件

postman-newman
1个回答
0
投票

这个

demo.js
可以合并多个环境变量文件

合并的 JSON 文件('demo.postman_environment.json') 复制第一个 JSON 的属性(

id
name
_postman_variable_scope
_postman_exported_at
_postman_exported_using

从第 2 个到第 n 个 JSON,只需将键/值对复制到合并的 JSON 文件中。

enter image description here

const fs = require('fs');

// Function to merge multiple JSON objects
function mergeJSONObjects(jsonObjects) {
    const baseJSON = jsonObjects[0]; // Use the first JSON as the base structure

    // Loop over remaining JSON objects and merge their 'values' arrays into the base JSON
    jsonObjects.slice(1).forEach(json => {
        baseJSON.values = [...baseJSON.values, ...json.values];
    });

    return baseJSON;
}

// Read JSON data from files specified in command-line arguments
const jsonFiles = process.argv.slice(2); // Skip the first two args (node and script name)
const jsonObjects = jsonFiles.map(file => JSON.parse(fs.readFileSync(file, 'utf8')));

// Perform the merge
const mergedJSON = mergeJSONObjects(jsonObjects);

// Output the merged JSON
console.log(JSON.stringify(mergedJSON, null, 4));

// Optionally, save the merged JSON to a file
fs.writeFileSync('demo.postman_environment.json', JSON.stringify(mergedJSON, null, 4), 'utf8');

使用文件名参数运行它

node demo.js first.json second.json third.json

结果

demo.postman_environment.json

示例

demo1.postman_environment.json

{
    "id": "efdf42ef-3df9-45cb-a6a7-b8f8a40b2712",
    "name": "demo1",
    "values": [
        {
            "key": "Animal1",
            "value": "Lion",
            "type": "default",
            "enabled": true
        }
    ],
    "_postman_variable_scope": "environment",
    "_postman_exported_at": "2024-04-27T16:26:09.645Z",
    "_postman_exported_using": "Postman/10.24.24"
}

demo2.postman_environment.json

{
    "id": "d85b8da5-2a00-4daa-beba-ba00f55b46b1",
    "name": "demo2",
    "values": [
        {
            "key": "Animal2",
            "value": "Elephant",
            "type": "default",
            "enabled": true
        }
    ],
    "_postman_variable_scope": "environment",
    "_postman_exported_at": "2024-04-27T16:26:29.585Z",
    "_postman_exported_using": "Postman/10.24.24"
}

demo3.postman_environment.json

{
    "id": "59fe0bfa-5071-4108-987e-e30a0345af69",
    "name": "demo3",
    "values": [
        {
            "key": "Animal3",
            "value": "Tiger",
            "type": "default",
            "enabled": true
        }
    ],
    "_postman_variable_scope": "environment",
    "_postman_exported_at": "2024-04-27T16:26:58.829Z",
    "_postman_exported_using": "Postman/10.24.24"
}

结果

{
    "id": "efdf42ef-3df9-45cb-a6a7-b8f8a40b2712",
    "name": "demo1",
    "values": [
        {
            "key": "Animal1",
            "value": "Lion",
            "type": "default",
            "enabled": true
        },
        {
            "key": "Animal2",
            "value": "Elephant",
            "type": "default",
            "enabled": true
        },
        {
            "key": "Animal3",
            "value": "Tiger",
            "type": "default",
            "enabled": true
        }
    ],
    "_postman_variable_scope": "environment",
    "_postman_exported_at": "2024-04-27T16:26:09.645Z",
    "_postman_exported_using": "Postman/10.24.24"
}

测试将 JSON 与集合合并

enter image description here

1-demo.postman_collection.json

{
    "info": {
        "_postman_id": "7fab6ad2-98f1-429e-9943-fac7f5b9e491",
        "name": "1-demo",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
        "_exporter_id": "1826150"
    },
    "item": [
        {
            "name": "Get Tiger",
            "event": [
                {
                    "listen": "prerequest",
                    "script": {
                        "exec": [
                            ""
                        ],
                        "type": "text/javascript",
                        "packages": {}
                    }
                },
                {
                    "listen": "test",
                    "script": {
                        "exec": [
                            "const jsonData = JSON.parse(responseBody);\r",
                            "console.log(jsonData[0].name);\r",
                            "pm.test('Animal Name = ' + jsonData[0].name, function () {\r",
                            "    pm.expect(jsonData[0].name).to.eql(\"Lion\");\r",
                            "});\r",
                            ""
                        ],
                        "type": "text/javascript",
                        "packages": {}
                    }
                }
            ],
            "request": {
                "auth": {
                    "type": "noauth"
                },
                "method": "GET",
                "header": [],
                "url": {
                    "raw": "https://freetestapi.com/api/v1/animals?search={{Animal1}}",
                    "protocol": "https",
                    "host": [
                        "freetestapi",
                        "com"
                    ],
                    "path": [
                        "api",
                        "v1",
                        "animals"
                    ],
                    "query": [
                        {
                            "key": "search",
                            "value": "{{Animal1}}"
                        }
                    ]
                }
            },
            "response": []
        },
        {
            "name": "Get Elephant",
            "event": [
                {
                    "listen": "prerequest",
                    "script": {
                        "exec": [
                            ""
                        ],
                        "type": "text/javascript",
                        "packages": {}
                    }
                },
                {
                    "listen": "test",
                    "script": {
                        "exec": [
                            "const jsonData = JSON.parse(responseBody);\r",
                            "console.log(jsonData[0].name);\r",
                            "pm.test('Animal Name = ' + jsonData[0].name, function () {\r",
                            "    pm.expect(jsonData[0].name).to.eql(\"Elephant\");\r",
                            "});\r",
                            ""
                        ],
                        "type": "text/javascript",
                        "packages": {}
                    }
                }
            ],
            "request": {
                "auth": {
                    "type": "noauth"
                },
                "method": "GET",
                "header": [],
                "url": {
                    "raw": "https://freetestapi.com/api/v1/animals?search={{Animal2}}",
                    "protocol": "https",
                    "host": [
                        "freetestapi",
                        "com"
                    ],
                    "path": [
                        "api",
                        "v1",
                        "animals"
                    ],
                    "query": [
                        {
                            "key": "search",
                            "value": "{{Animal2}}"
                        }
                    ]
                }
            },
            "response": []
        },
        {
            "name": "Get Tiger",
            "event": [
                {
                    "listen": "prerequest",
                    "script": {
                        "exec": [
                            ""
                        ],
                        "type": "text/javascript",
                        "packages": {}
                    }
                },
                {
                    "listen": "test",
                    "script": {
                        "exec": [
                            "const jsonData = JSON.parse(responseBody);\r",
                            "console.log(jsonData[0].name);\r",
                            "pm.test('Animal Name = ' + jsonData[0].name, function () {\r",
                            "    pm.expect(jsonData[0].name).to.eql(\"Tiger\");\r",
                            "});\r",
                            ""
                        ],
                        "type": "text/javascript",
                        "packages": {}
                    }
                }
            ],
            "request": {
                "auth": {
                    "type": "noauth"
                },
                "method": "GET",
                "header": [],
                "url": {
                    "raw": "https://freetestapi.com/api/v1/animals?search={{Animal3}}",
                    "protocol": "https",
                    "host": [
                        "freetestapi",
                        "com"
                    ],
                    "path": [
                        "api",
                        "v1",
                        "animals"
                    ],
                    "query": [
                        {
                            "key": "search",
                            "value": "{{Animal3}}"
                        }
                    ]
                }
            },
            "response": []
        }
    ],
    "event": [
        {
            "listen": "prerequest",
            "script": {
                "type": "text/javascript",
                "exec": [
                    ""
                ]
            }
        },
        {
            "listen": "test",
            "script": {
                "type": "text/javascript",
                "exec": [
                    ""
                ]
            }
        }
    ],
    "variable": [
        {
            "key": "priductIds",
            "value": "[\"1001\", \"1002\", \"1003\", \"1004\", \"3002\"]",
            "type": "string"
        },
        {
            "key": "priductId",
            "value": "1001",
            "type": "string"
        },
        {
            "key": "baseUrl",
            "value": "https://valentinos-coffee.herokuapp.com",
            "type": "string"
        },
        {
            "key": "AnimalItemLength",
            "value": ""
        }
    ]
}

newman
奔跑

newman run 1-demo.postman_collection.json -e demo.postman_environment.json

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.