Reputation: 27
I want to access environmental variables in tests. For ex. check if values are signed to the correct fields.
I've tried to get variable values different way and the only one working is to set a variable inside the test but then I won't be able to edit it in bulk to run tests with other presets.
pm.test("Check if caregiver information is correct", function () {
pm.expect(jsonData.caregivers[0].first_name).to.equal("{{caregiverName}}");
});
code above returns AssertionError: expected 'adam' to equal '{{caregiverName}}'
console.log(pm.variables.get("{{caregiverName}}"));
returns null
console.log("{{caregiverName}}");
returns {{caregiverName}}
I would expect the value of {{caregiverName}}
to equal what I have set as in environmental variables.
Upvotes: 1
Views: 213
Reputation: 7866
As caregiverName
is an environment variable and set before, you need to get using following syntax:
pm.environment.get("variable_key");
Refactor your code as follows,
pm.test("Check if caregiver information is correct", function () {
pm.expect(jsonData.caregivers[0].first_name).to.equal(pm.environment.get("caregiverName"));
});
Learn more about variables: Variables - Postman
Upvotes: 2