How can I validate an object’s properties in response after I updated/created properties in PUT request using variables?
I have updated the object (employee):
[
"id": 123
"externalId": "Employee1",
"code": "AAPI",
"description": "John Smith",
"isValid": true
}
]
Now I request the list of all employees and I need to check if the employee was updated successfully with all his properties I will use this test in future many times as the regression test so I need to save variables
I’ve tried two ways but I’m not sure if they both are correct:
- I save the employee id as a variable using the chain method
bodyData = JSON.parse(responseBody),
employeeId = bodyData.items[0].id;
pm.environment.set("employeeId", employeeId);
and the rest properties values I don’t save I just validate them using
pm.test("Employee's body is correct", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.items[0].code).to.eql("AAPI")
- I saved all properties’ values to variables and used these variables in validating but it doesn’t seem correct as I validate what I’ve just saved to variables
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
bodyData = JSON.parse(responseBody),
employeeId = bodyData.items[0].id;
pm.environment.set("employeeId", employeeId);
bodyData = JSON.parse(responseBody),
employeeDescription = bodyData.items[0].description;
pm.environment.set("employeeDescription", employeeDescription);
bodyData = JSON.parse(responseBody),
employeeIsValid = bodyData.items[0].isValid;
pm.environment.set("employeeIsValid", employeeIsValid);
bodyData = JSON.parse(responseBody),
employeeExternalId = bodyData.items[0].externalId;
pm.environment.set("employeeExternalId", employeeExternalId);
bodyData = JSON.parse(responseBody),
employeeCode= bodyData.items[0].code;
pm.environment.set("employeeCode", employeeCode);
pm.test("Employee's body is correct", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.items[0].description).to.eql(pm.environment.get("employeeDescription"));
pm.expect(jsonData.items[0].isValid).to.eql(pm.environment.get("employeeIsValid"));
pm.expect(jsonData.items[0].id).to.eql(pm.environment.get("employeeId"));
pm.expect(jsonData.items[0].externalId).to.eql(pm.environment.get("employeeExternalId"));
pm.expect(jsonData.items[0].code).to.eql(pm.environment.get("employeeCode"));
});
I would appreciate any help. Thank you