如何解决从Postman转换而来的运行k6负载测试失败
我使用Postman将k6集合转换为负载测试工具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
要使负载测试正常工作,我需要更改什么?
// 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}}",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")
);
});
},Var) {
config.headers.Authorization = `Bearer ${pm[Var]("accesstoken")}`;
}
});
...
postman[Request]({
name: "Run a search",id: "9a66875b-6a03-4662-b287-7311632a0e27",address: "{{baseUrl}}{{searchId}}",function() {
pm.response.to.have.status(200);
});
},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}}",Var) {
config.headers.Authorization = `Bearer ${pm[Var]("accesstoken")}`;
}
});
postman[Request]({
name: "Delete the unit",id: "6657b858-18a8-4271-bd54-6084ee876d2f",function() {
pm.response.to.have.status(204);
});
},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\"," 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","script": {
"id": "1366b020-d215-49e1-903d-12a710fc6dad"," pm.response.to.have.status(200);","pm.test(\"Returned unit has the correct values\"," pm.expect(jsonData.id).to.equal(pm.collectionVariables.get(\"unitId\"));"," pm.expect(jsonData.name).to.equal(pm.collectionVariables.get(\"unitName\"));","});"
],"request": {
"method": "GET","url": {
"raw": "{{baseUrl}}/units/:id","path": [
"units",":id"
],"variable": [
{
"key": "id","value": "{{unitId}}","description": "(required) "
}
]
}
},"response": [
{
"name": "Unit loaded","originalRequest": {
"method": "GET","url": {
"raw": "{{baseUrl}}/units/:id","path": [
"units",":id"
],"variable": [
{
"key": "id"
}
]
}
},"status": "OK","code": 200,"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","status": "Not Found","code": 404,"_postman_previewlanguage": "text","value": "*/*"
}
],"body": ""
}
]
},{
"name": "Load the asset","script": {
"id": "c7b133bc-997b-409b-b9e4-79817462c9a7","pm.test(\"Returned asset has the correct values\"," 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');",""
],"url": {
"raw": "{{baseUrl}}/assets/:id","path": [
"assets","value": "{{assetId}}","response": [
{
"name": "Asset loaded","url": {
"raw": "{{baseUrl}}/assets/:id","path": [
"assets",\n \"name\": \"Lock doors\",\n \"owner\": {\n \"displayName\": \"My Unit\",\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 \"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}}'));"
],{
"listen": "test","script": {
"id": "00c46943-969a-4599-9f3e-1524c6aab374","pm.collectionVariables.set(\"processIdveo271\","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","path": [
"processes"
]
}
},"response": [
{
"name": "default response","raw": "{\n \"name\": \"<add example here>\",\n \"owner\": {\n \"displayName\": \"My Unit\",\n \"abbreviation\": \"<add example here>\",\n \"description\": \"<add example here>\",\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 \"assets\": [\n {\n \"displayName\": \"My Unit\",\n \"extinguishingTime\": \"<add example here>\"\n}"
},"url": {
"raw": "{{baseUrl}}/processes","path": [
"processes"
]
}
},{
"name": "Load the process VEO-271","script": {
"id": "d883576e-b7e3-46e5-b897-52557422236d","pm.test(\"Returned process has the correct values\"," pm.expect(jsonData.id).to.equal(pm.collectionVariables.get(\"processIdveo271\"));"," pm.expect(jsonData.name).to.equal(\"Verarbeitungstätigkeit 1\")","","url": {
"raw": "{{baseUrl}}/processes/:id","path": [
"processes","value": "{{processIdveo271}}","url": {
"raw": "{{baseUrl}}/processes/:id","path": [
"processes","body": ""
}
]
}
],"description": "Basic CRUD operations","protocolProfileBehavior": {
}
}
],"auth": {
"type": "oauth2","oauth2": [
{
"key": "accesstoken","value": "{{accesstoken}}","type": "string"
},{
"key": "tokenType","value": "bearer",{
"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","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",{
"id": "7a38da8f-0870-4ef8-a9ed-db61ade1bb1e","key": "processName",{
"id": "0717d2f5-9673-483c-b8cd-febb6b115ad5","key": "unitName",{
"id": "83eed492-e881-4ac4-b2f7-54560a65ad1d","key": "accesstoken",{
"id": "0628bdb2-94b9-41ff-a062-91c4c7fb2134","key": "assetId",{
"id": "63381cb9-7d3d-4535-84b5-0c33fcd15947","key": "assetName",{
"id": "354bcf34-860e-4d67-9b9d-fa87e00211d3","key": "personId",{
"id": "1202dd72-fd93-4e28-8181-a2a6f8b1937f","key": "personName",{
"id": "1e17139b-c605-4641-9b09-1bd88f62bb2a","key": "controlId",{
"id": "b92fc81c-2573-4f37-857e-6b59a26b3765","key": "controlName",{
"id": "bfe6276d-96f0-4a4f-bd96-4214e9360e1b","key": "processIdveo271",{
"id": "0af17639-8862-43ff-824a-4d691f19ad1c","key": "subUnitName",{
"id": "074a4920-43d2-4fc1-9bb9-5ad5f0adedfb","key": "subUnitId",{
"id": "1468cf81-6110-44a9-8258-104214f4b48b","key": "searchId","value": ""
}
],"protocolProfileBehavior": {
}
}
解决方法
我也无法使集合级别变量起作用。
相反,你应该改变
pm.collectionVariables.set
到
postman.setEnvironmentVariable
我注意到这会导致您的 Collection.baseUrl
和您的 Environment.baseUrl
发生冲突。
如果您分别上调环境和全局,那应该可以解决您的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。