从Postman转换而来的运行k6负载测试失败

时间:2020-09-28 09:59:39

标签: rest postman k6

我使用Postmank6集合转换为负载测试工具postman-to-k6的脚本。运行k6测试失败,并显示以下错误消息:

ERRO[0016] TypeError: Cannot read property 'set' of undefined or null
Run     at executePrerequest (file:///home/dm/temp/39/k6/libs/shim/core.js:1187:203(89))
default at executeRequest (file:///home/dm/temp/39/k6/libs/shim/core.js:969:22(73))
        at executeRequest (file:///home/dm/temp/39/k6/libs/shim/core.js:968:5(57))
        at file:///home/dm/temp/39/k6/libs/shim/core.js:307:46(35)
        at file:///home/dm/temp/39/k6/k6-script.js:38:21(26)
        at github.com/loadimpact/k6/js/common.Bind.func1 (native)
        at file:///home/dm/temp/39/k6/k6-script.js:37:17(5)  executor=per-vu-iterations scenario=default source=stacktrace

要使负载测试正常工作,我需要更改什么?

从文件k6-script.js中提取:

// Auto-generated by the Load Impact converter

import "./libs/shim/core.js";
import "./libs/shim/expect.js";
import "./libs/shim/urijs.js";
import { group } from "k6";

export let options = { maxRedirects: 4 };

const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
options,
collection: {
    baseUrl: "http://localhost:8070",
    unitId: "",
    processId: "",
    processName: "",
    unitName: "",
    accessToken: "",
    assetId: "",
    assetName: "",
    personId: "",
    personName: "",
    controlId: "",
    controlName: "",
    processIdveo271: "",
    subUnitName: "",
    subUnitId: "",
    searchId: ""
},
environment: {
    baseUrl: "https://veo.develop.verinice.com"
}
});

export default function() {
group("Basic CRUD operations", function() {
    postman[Request]({
    name: "Create a unit",
    id: "f3a7bc74-372e-4811-b077-ceed662613a4",
    method: "POST",
    address: "{{baseUrl}}/units",
    data: '{\n    "name": "{{unitName}}"\n}',
    headers: {
        "Content-Type": "application/json"
    },
    pre() {
        pm.collectionVariables.set("unitName", "CRUD test unit");
    },
    post(response) {
        pm.test("Validate status code", function() {
        pm.response.to.have.status(201);
        });
        var jsonData = pm.response.json();
        var resourceId = jsonData.resourceId;
        pm.test("Resource ID is a string", function() {
        pm.expect(resourceId).to.be.a("string");
        });
        pm.collectionVariables.set("unitId", resourceId);
    },
    auth(config, Var) {
        config.headers.Authorization = `Bearer ${pm[Var]("accessToken")}`;
    }
    });

    postman[Request]({
    name: "Load the unit",
    id: "70a71fdb-6d67-405d-895a-4f2ca998c92c",
    method: "GET",
    address: "{{baseUrl}}/units/{{unitId}}",
    post(response) {
        pm.test("Validate status code", function() {
        pm.response.to.have.status(200);
        });
        var jsonData = pm.response.json();
        pm.test("Returned unit has the correct values", function() {
        pm.expect(jsonData.id).to.equal(pm.collectionVariables.get("unitId"));
        pm.expect(jsonData.name).to.equal(
            pm.collectionVariables.get("unitName")
        );
        });
    },
    auth(config, Var) {
        config.headers.Authorization = `Bearer ${pm[Var]("accessToken")}`;
    }
    });

    ...

    postman[Request]({
    name: "Run a search",
    id: "9a66875b-6a03-4662-b287-7311632a0e27",
    method: "GET",
    address: "{{baseUrl}}{{searchId}}",
    post(response) {
        pm.test("Validate status code", function() {
        pm.response.to.have.status(200);
        });
    },
    auth(config, Var) {
        config.headers.Authorization = `Bearer ${pm[Var]("accessToken")}`;
    }
    });

    postman[Request]({
    name: "Delete the asset",
    id: "90367e36-0718-4fd1-b483-f37b17b554f6",
    method: "DELETE",
    address: "{{baseUrl}}/assets/{{assetId}}",
    post(response) {
        pm.test("Validate status code", function() {
        pm.response.to.have.status(200);
        });
    },
    auth(config, Var) {
        config.headers.Authorization = `Bearer ${pm[Var]("accessToken")}`;
    }
    });

    postman[Request]({
    name: "Delete the unit",
    id: "6657b858-18a8-4271-bd54-6084ee876d2f",
    method: "DELETE",
    address: "{{baseUrl}}/units/{{unitId}}",
    post(response) {
        pm.test("Validate status code", function() {
        pm.response.to.have.status(204);
        });
    },
    auth(config, Var) {
        config.headers.Authorization = `Bearer ${pm[Var]("accessToken")}`;
    }
    });
});
}

从Postman馆藏中提取

{
"info": {
    "_postman_id": "43d0a18e-1631-4124-8a15-c84edd1c5eb4",
    "name": "verinice.VEO REST API",
    "description": "OpenAPI documentation for verinice.VEO.\n\nContact Support:\n Email: verinice@sernet.de",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
    {
    "name": "Basic CRUD operations",
    "item": [
        {
        "name": "Create a unit",
        "event": [
            {
            "listen": "test",
            "script": {
                "id": "69edb44e-a7da-4b87-b05f-55927d2af355",
                "exec": [
                "pm.test(\"Validate status code\", function () {",
                "    pm.response.to.have.status(201);",
                "});",
                "var jsonData = pm.response.json();",
                "var resourceId = jsonData.resourceId;",
                "pm.test(\"Resource ID is a string\", function () {",
                "    pm.expect(resourceId).to.be.a('string')",
                "});",
                "pm.collectionVariables.set(\"unitId\", resourceId);"
                ],
                "type": "text/javascript"
            }
            },
            {
            "listen": "prerequest",
            "script": {
                "id": "d619b1ae-b51d-4d06-b831-4129d8d69d79",
                "exec": [
                "pm.collectionVariables.set('unitName', 'CRUD test unit');"
                ],
                "type": "text/javascript"
            }
            }
        ],
        "request": {
            "method": "POST",
            "header": [
            {
                "key": "Content-Type",
                "value": "application/json"
            }
            ],
            "body": {
            "mode": "raw",
            "raw": "{\n    \"name\": \"{{unitName}}\"\n}"
            },
            "url": {
            "raw": "{{baseUrl}}/units",
            "host": [
                "{{baseUrl}}"
            ],
            "path": [
                "units"
            ]
            }
        },
        "response": [
            {
            "name": "Unit created",
            "originalRequest": {
                "method": "POST",
                "header": [],
                "body": {
                "mode": "raw",
                "raw": "{\n    \"name\": \"My unit\",\n    \"id\": \"adf037f1-0089-48ad-9177-92269918758b\",\n    \"validFrom\": \"1990-12-31T23:59:60Z\",\n    \"validUntil\": \"1990-12-31T23:59:60Z\",\n    \"abbreviation\": \"U-96\",\n    \"description\": \"This is currently the main and only unit for our organization.\",\n    \"riskvalues\": \"<add example here>\",\n    \"objects\": \"<add example here>\",\n    \"units\": \"<add example here>\",\n    \"parent\": {\n        \"displayName\": \"My Unit\",\n        \"targetUri\": \"http://<api.example.org>/api/v1/unit/<00000000-0000-0000-0000-000000000000>\"\n    },\n    \"domains\": [\n        {\n            \"displayName\": \"My Unit\",\n            \"targetUri\": \"http://<api.example.org>/api/v1/unit/<00000000-0000-0000-0000-000000000000>\"\n        },\n        {\n            \"displayName\": \"My Unit\",\n            \"targetUri\": \"http://<api.example.org>/api/v1/unit/<00000000-0000-0000-0000-000000000000>\"\n        }\n    ]\n}"
                },
                "url": {
                "raw": "{{baseUrl}}/units",
                "host": [
                    "{{baseUrl}}"
                ],
                "path": [
                    "units"
                ]
                }
            },
            "status": "Created",
            "code": 201,
            "_postman_previewlanguage": "json",
            "header": [
                {
                "key": "Content-Type",
                "value": "application/json"
                }
            ],
            "cookie": [],
            "body": "\"<object>\""
            }
        ]
        },
        {
        "name": "Load the unit",
        "event": [
            {
            "listen": "test",
            "script": {
                "id": "1366b020-d215-49e1-903d-12a710fc6dad",
                "exec": [
                "pm.test(\"Validate status code\", function () {",
                "    pm.response.to.have.status(200);",
                "});",
                "var jsonData = pm.response.json();",
                "pm.test(\"Returned unit has the correct values\", function () {",
                "    pm.expect(jsonData.id).to.equal(pm.collectionVariables.get(\"unitId\"));",
                "    pm.expect(jsonData.name).to.equal(pm.collectionVariables.get(\"unitName\"));",
                "});"
                ],
                "type": "text/javascript"
            }
            }
        ],
        "request": {
            "method": "GET",
            "header": [],
            "url": {
            "raw": "{{baseUrl}}/units/:id",
            "host": [
                "{{baseUrl}}"
            ],
            "path": [
                "units",
                ":id"
            ],
            "variable": [
                {
                "key": "id",
                "value": "{{unitId}}",
                "description": "(Required) "
                }
            ]
            }
        },
        "response": [
            {
            "name": "Unit loaded",
            "originalRequest": {
                "method": "GET",
                "header": [],
                "url": {
                "raw": "{{baseUrl}}/units/:id",
                "host": [
                    "{{baseUrl}}"
                ],
                "path": [
                    "units",
                    ":id"
                ],
                "variable": [
                    {
                    "key": "id"
                    }
                ]
                }
            },
            "status": "OK",
            "code": 200,
            "_postman_previewlanguage": "json",
            "header": [
                {
                "key": "Content-Type",
                "value": "application/json"
                }
            ],
            "cookie": [],
            "body": "{\n \"id\": \"adf037f1-0089-48ad-9177-92269918758b\",\n \"name\": \"<add example here>\",\n \"validFrom\": \"1990-12-31T23:59:60Z\",\n \"abbreviation\": \"<add example here>\",\n \"description\": \"<add example here>\",\n \"riskvalues\": \"<add example here>\",\n \"objects\": \"<add example here>\",\n \"units\": \"<add example here>\",\n \"parent\": {\n  \"displayName\": \"My Unit\",\n  \"targetUri\": \"http://<api.example.org>/api/v1/unit/<00000000-0000-0000-0000-000000000000>\"\n },\n \"domains\": [\n  {\n   \"displayName\": \"My Unit\",\n   \"targetUri\": \"http://<api.example.org>/api/v1/unit/<00000000-0000-0000-0000-000000000000>\"\n  },\n  {\n   \"displayName\": \"My Unit\",\n   \"targetUri\": \"http://<api.example.org>/api/v1/unit/<00000000-0000-0000-0000-000000000000>\"\n  }\n ]\n}"
            },
            {
            "name": "Unit not found",
            "originalRequest": {
                "method": "GET",
                "header": [],
                "url": {
                "raw": "{{baseUrl}}/units/:id",
                "host": [
                    "{{baseUrl}}"
                ],
                "path": [
                    "units",
                    ":id"
                ],
                "variable": [
                    {
                    "key": "id"
                    }
                ]
                }
            },
            "status": "Not Found",
            "code": 404,
            "_postman_previewlanguage": "text",
            "header": [
                {
                "key": "Content-Type",
                "value": "*/*"
                }
            ],
            "cookie": [],
            "body": ""
            }
        ]
        },
        {
        "name": "Load the asset",
        "event": [
            {
            "listen": "test",
            "script": {
                "id": "c7b133bc-997b-409b-b9e4-79817462c9a7",
                "exec": [
                "pm.test(\"Validate status code\", function () {",
                "    pm.response.to.have.status(200);",
                "});",
                "var jsonData = pm.response.json();",
                "pm.test(\"Returned asset has the correct values\", function () {",
                "    pm.expect(jsonData.id).to.equal(pm.collectionVariables.get(\"assetId\"));",
                "    pm.expect(jsonData.name).to.equal(pm.collectionVariables.get(\"assetName\"))",
                "    pm.expect(jsonData.owner.displayName).to.equal(pm.collectionVariables.get(\"unitName\"));",
                "    pm.expect(jsonData.owner.targetUri).to.equal(pm.collectionVariables.get(\"baseUrl\")+'/units/'+pm.collectionVariables.get(\"unitId\"));",
                "    pm.expect(jsonData.customAspects.myaspecttest1.type).to.equal('myaspecttest1');",
                "    pm.expect(jsonData.links.A1[0].type).to.equal('A1');",
                "});",
                ""
                ],
                "type": "text/javascript"
            }
            }
        ],
        "request": {
            "method": "GET",
            "header": [],
            "url": {
            "raw": "{{baseUrl}}/assets/:id",
            "host": [
                "{{baseUrl}}"
            ],
            "path": [
                "assets",
                ":id"
            ],
            "variable": [
                {
                "key": "id",
                "value": "{{assetId}}",
                "description": "(Required) "
                }
            ]
            }
        },
        "response": [
            {
            "name": "Asset loaded",
            "originalRequest": {
                "method": "GET",
                "header": [],
                "url": {
                "raw": "{{baseUrl}}/assets/:id",
                "host": [
                    "{{baseUrl}}"
                ],
                "path": [
                    "assets",
                    ":id"
                ],
                "variable": [
                    {
                    "key": "id"
                    }
                ]
                }
            },
            "status": "OK",
            "code": 200,
            "_postman_previewlanguage": "json",
            "header": [
                {
                "key": "Content-Type",
                "value": "application/json"
                }
            ],
            "cookie": [],
            "body": "{\n \"id\": \"adf037f1-0089-48ad-9177-92269918758b\",\n \"name\": \"Lock doors\",\n \"owner\": {\n  \"displayName\": \"My Unit\",\n  \"targetUri\": \"http://<api.example.org>/api/v1/unit/<00000000-0000-0000-0000-000000000000>\"\n },\n \"validFrom\": \"1990-12-31T23:59:60Z\",\n \"abbreviation\": \"Lock doors\",\n \"description\": \"Lock doors\",\n \"domains\": [\n  {\n   \"displayName\": \"EU GDPR 2016-05-04\",\n   \"targetUri\": \"http://<api.example.org>/api/v1/domain/<00000000-0000-0000-0000-000000000000>\"\n  },\n  {\n   \"displayName\": \"EU GDPR 2016-05-04\",\n   \"targetUri\": \"http://<api.example.org>/api/v1/domain/<00000000-0000-0000-0000-000000000000>\"\n  }\n ],\n \"riskvalues\": \"<add example here>\",\n \"customAspects\": \"<object>\",\n \"links\": \"<object>\"\n}"
            }
        ]
        },
        {
        "name": "Create a process VEO-271",
        "event": [
            {
            "listen": "prerequest",
            "script": {
                "id": "0c80d09d-d411-45b0-848a-3c214099b514",
                "exec": [
                "pm.collectionVariables.set(\"processName\", \"Process of \" + pm.variables.replaceIn('{{$randomFirstName}}'));"
                ],
                "type": "text/javascript"
            }
            },
            {
            "listen": "test",
            "script": {
                "id": "00c46943-969a-4599-9f3e-1524c6aab374",
                "exec": [
                "pm.test(\"Validate status code\", function () {",
                "    pm.response.to.have.status(201);",
                "});",
                "var jsonData = pm.response.json();",
                "var resourceId = jsonData.resourceId;",
                "pm.test(\"Resource ID is a string\", function () {",
                "    pm.expect(resourceId).to.be.a('string')",
                "});",
                "pm.collectionVariables.set(\"processIdveo271\", resourceId);"
                ],
                "type": "text/javascript"
            }
            }
        ],
        "request": {
            "method": "POST",
            "header": [
            {
                "key": "Content-Type",
                "value": "application/json"
            }
            ],
            "body": {
            "mode": "raw",
            "raw": "{\n    \"name\": \"Verarbeitungstätigkeit 1\",\n    \"customAspects\": {\n        \"ProcessDvGroupRechtsgrundlage\": {\n            \"attributes\": {\n                \"processDvRechtsgrundlage\": [\n                    \"processDvRechtsgrundlage1\",\n                    \"processDvRechtsgrundlage5\",\n                    \"processDvRechtsgrundlage2\",\n                    \"processDvRechtsgrundlage3\",\n                    \"processDvRechtsgrundlage4\"\n                ]\n            },\n            \"type\": \"ProcessDvGroupRechtsgrundlage\"\n        }\n    },\n    \"owner\": {\n        \"targetUri\": \"/units/{{unitId}}\"\n    }\n}"
            },
            "url": {
            "raw": "{{baseUrl}}/processes",
            "host": [
                "{{baseUrl}}"
            ],
            "path": [
                "processes"
            ]
            }
        },
        "response": [
            {
            "name": "default response",
            "originalRequest": {
                "method": "POST",
                "header": [],
                "body": {
                "mode": "raw",
                "raw": "{\n    \"name\": \"<add example here>\",\n    \"owner\": {\n        \"displayName\": \"My Unit\",\n        \"targetUri\": \"http://<api.example.org>/api/v1/unit/<00000000-0000-0000-0000-000000000000>\"\n    },\n    \"id\": \"adf037f1-0089-48ad-9177-92269918758b\",\n    \"validFrom\": \"1990-12-31T23:59:60Z\",\n    \"validUntil\": \"1990-12-31T23:59:60Z\",\n    \"abbreviation\": \"<add example here>\",\n    \"description\": \"<add example here>\",\n    \"domains\": [\n        {\n            \"displayName\": \"My Unit\",\n            \"targetUri\": \"http://<api.example.org>/api/v1/unit/<00000000-0000-0000-0000-000000000000>\"\n        },\n        {\n            \"displayName\": \"My Unit\",\n            \"targetUri\": \"http://<api.example.org>/api/v1/unit/<00000000-0000-0000-0000-000000000000>\"\n        }\n    ],\n    \"links\": \"<add example here>\",\n    \"customAspects\": \"<add example here>\",\n    \"riskvalues\": \"<add example here>\",\n    \"assets\": [\n        {\n            \"displayName\": \"My Unit\",\n            \"targetUri\": \"http://<api.example.org>/api/v1/unit/<00000000-0000-0000-0000-000000000000>\"\n        },\n        {\n            \"displayName\": \"My Unit\",\n            \"targetUri\": \"http://<api.example.org>/api/v1/unit/<00000000-0000-0000-0000-000000000000>\"\n        }\n    ],\n    \"extinguishingTime\": \"<add example here>\"\n}"
                },
                "url": {
                "raw": "{{baseUrl}}/processes",
                "host": [
                    "{{baseUrl}}"
                ],
                "path": [
                    "processes"
                ]
                }
            },
            "status": "Created",
            "code": 201,
            "_postman_previewlanguage": "text",
            "header": [
                {
                "key": "Content-Type",
                "value": "*/*"
                }
            ],
            "cookie": [],
            "body": ""
            }
        ]
        },
        {
        "name": "Load the process VEO-271",
        "event": [
            {
            "listen": "test",
            "script": {
                "id": "d883576e-b7e3-46e5-b897-52557422236d",
                "exec": [
                "pm.test(\"Validate status code\", function () {",
                "    pm.response.to.have.status(200);",
                "});",
                "var jsonData = pm.response.json();",
                "pm.test(\"Returned process has the correct values\", function () {",
                "    pm.expect(jsonData.id).to.equal(pm.collectionVariables.get(\"processIdveo271\"));",
                "    pm.expect(jsonData.name).to.equal(\"Verarbeitungstätigkeit 1\")",
                "});",
                "",
                ""
                ],
                "type": "text/javascript"
            }
            }
        ],
        "request": {
            "method": "GET",
            "header": [],
            "url": {
            "raw": "{{baseUrl}}/processes/:id",
            "host": [
                "{{baseUrl}}"
            ],
            "path": [
                "processes",
                ":id"
            ],
            "variable": [
                {
                "key": "id",
                "value": "{{processIdveo271}}",
                "description": "(Required) "
                }
            ]
            }
        },
        "response": [
            {
            "name": "default response",
            "originalRequest": {
                "method": "GET",
                "header": [],
                "url": {
                "raw": "{{baseUrl}}/processes/:id",
                "host": [
                    "{{baseUrl}}"
                ],
                "path": [
                    "processes",
                    ":id"
                ],
                "variable": [
                    {
                    "key": "id"
                    }
                ]
                }
            },
            "status": "OK",
            "code": 200,
            "_postman_previewlanguage": "text",
            "header": [
                {
                "key": "Content-Type",
                "value": "*/*"
                }
            ],
            "cookie": [],
            "body": ""
            }
        ]
        }
    ],
    "description": "Basic CRUD operations",
    "protocolProfileBehavior": {
        
    }
    }
],
"auth": {
    "type": "oauth2",
    "oauth2": [
    {
        "key": "accessToken",
        "value": "{{accessToken}}",
        "type": "string"
    },
    {
        "key": "tokenType",
        "value": "bearer",
        "type": "string"
    },
    {
        "key": "addTokenTo",
        "value": "header",
        "type": "string"
    }
    ]
},
"event": [
    {
    "listen": "prerequest",
    "script": {
        "id": "8b2028b3-ea03-415b-bdcf-4e7a0df39d4e",
        "type": "text/javascript",
        "exec": [
        ""
        ]
    }
    },
    {
    "listen": "test",
    "script": {
        "id": "f18a8e83-d6c3-4155-aa24-ecf10f252354",
        "type": "text/javascript",
        "exec": [
        ""
        ]
    }
    }
],
"variable": [
    {
    "id": "823938c1-f3ac-4ad6-b2b7-c11f634fac51",
    "key": "baseUrl",
    "value": "http://localhost:8070"
    },
    {
    "id": "8134e904-4436-4801-ab18-f59169865344",
    "key": "unitId",
    "value": ""
    },
    {
    "id": "ae618ae5-3b5c-449e-a452-3b9e8ec2abde",
    "key": "processId",
    "value": ""
    },
    {
    "id": "7a38da8f-0870-4ef8-a9ed-db61ade1bb1e",
    "key": "processName",
    "value": ""
    },
    {
    "id": "0717d2f5-9673-483c-b8cd-febb6b115ad5",
    "key": "unitName",
    "value": ""
    },
    {
    "id": "83eed492-e881-4ac4-b2f7-54560a65ad1d",
    "key": "accessToken",
    "value": ""
    },
    {
    "id": "0628bdb2-94b9-41ff-a062-91c4c7fb2134",
    "key": "assetId",
    "value": ""
    },
    {
    "id": "63381cb9-7d3d-4535-84b5-0c33fcd15947",
    "key": "assetName",
    "value": ""
    },
    {
    "id": "354bcf34-860e-4d67-9b9d-fa87e00211d3",
    "key": "personId",
    "value": ""
    },
    {
    "id": "1202dd72-fd93-4e28-8181-a2a6f8b1937f",
    "key": "personName",
    "value": ""
    },
    {
    "id": "1e17139b-c605-4641-9b09-1bd88f62bb2a",
    "key": "controlId",
    "value": ""
    },
    {
    "id": "b92fc81c-2573-4f37-857e-6b59a26b3765",
    "key": "controlName",
    "value": ""
    },
    {
    "id": "bfe6276d-96f0-4a4f-bd96-4214e9360e1b",
    "key": "processIdveo271",
    "value": ""
    },
    {
    "id": "0af17639-8862-43ff-824a-4d691f19ad1c",
    "key": "subUnitName",
    "value": ""
    },
    {
    "id": "074a4920-43d2-4fc1-9bb9-5ad5f0adedfb",
    "key": "subUnitId",
    "value": ""
    },
    {
    "id": "1468cf81-6110-44a9-8258-104214f4b48b",
    "key": "searchId",
    "value": ""
    }
],
"protocolProfileBehavior": {
    
}
}

1 个答案:

答案 0 :(得分:0)

我也无法使集合级别变量起作用。

相反,你应该改变

pm.collectionVariables.set

postman.setEnvironmentVariable

我注意到这会导致您的 Collection.baseUrl 和您的 Environment.baseUrl 发生冲突。

如果您分别上调环境和全局,那应该可以解决您的问题。