Postman provides an execution environment where you can write JavaScript scripts — the so-called “Postman Sandbox”. You can write code in the section Scripts.
You can call Postman’s special functions by accessing the methods and properties of the object named pm.
1. pm.environment
Using the
pm object, you can access the
environment property and dynamically get and set
environment variables.
|
Accessing the environment
object and calling the
get function allows you to get the value of an environment variable by passing the variable name as a parameter.
|
pm.environment.get("variableName")
|
|
Accessing the environment
object and calling the
set function allows you to set the value of an environment variable by passing the variable name and value.
|
pm.environment.set("variableName", "variableValue")
|
2. pm.globals
Using the
pm object, you can access the
globals property and work dynamically with global variables (available across all environments).
|
Accessing the
globals object and calling the
get function allows you to get the value of a global variable by passing the variable name as a parameter.
|
pm.globals.get("variableName")
|
|
Accessing the
globals object and calling the
set function allows you to set the value of a global variable by passing the variable name and value.
|
pm.globals.set("variableName", "variableValue")
|
3. pm.test
Using the
pm object, you can call the
test method to write your own test.
|
Calling the test
method with two parameters — the test name and a validation function. If the function runs without errors, the test passes; otherwise, it fails.
|
pm.test("Let's check if code is 200",
function () {
pm.response.to.have.status(200);
}
);
|