I am wondering how to work with global / environment variables in tests converted from postman to k6.
In this test, we are doing a Post, setting the pm.globals . We want that variable to be accessible in the address of the Delete request.
What I am looking for is we do not support this yet or a workaround / best practice
group('post then delete', function() {
postman[Request]({
name: "Create (register) a Gateway",
id: "4ef66998-e8c6-4848-9e09-2c8bfcdb80ae",
method: "POST",
address: `http://${pa_api_host}/api/gateways`, // getting a value from a constant works
data:
'{\r\n "gatewayId": "Created-This-With-Postman-({{$guid}})",\r\n ...}',
headers: {
"Content-Type": "text/json"
},
post(response) {
pm.test("Create new Gateway returns Status code 201", function() {
pm.response.to.have.status(201);
});
pm.test("Response contains gatewayId", function() {
pm
.expect(pm.response.json().gatewayId)
.to.be.an("string").that.is.not.empty;
var gatewayIdFromResponse = pm.response.json().gatewayId;
pm.globals.set("gatewayId", gatewayIdFromResponse); // pm.globals.set is not working
});
}
});
// pm.globals.set does not work, get does not seem to work either
let gatewayId = pm.globals.get('gatewayId');
console.log(JSON.stringify(pm.globals)); // empty object
postman[Request]({
name: "Utility: Delete a gateway from the Protocol Adatper",
id: "969028fa-7401-4e9d-ac7f-12e5e207c08c",
method: "DELETE",
address: `http://${pa_api_host}/api/gateways/${gatewayId}`, // doesn't work
data: {},
headers: {
"Content-Type": "application/json"
},
post(response) {
pm.test("Delete Gateway used for Postman testing", function() {
pm.expect(pm.response.code).to.eq(200);
});
}
});
});