You are on page 1of 2

Testing Sandbox

App
The Postman Sandbox is a JavaScript execution environment that is available to y
ou while writing pre-request scripts and test scripts for requests (both in Post
man and Newman). Whatever code you write in the pre-request/test script section
is executed in this sandbox.
Commonly used libraries and utilities
Lodash: JS utility library
cheerio: A fast, lean implementation of the core jQuery API (available in versio
ns 4.6.0 and up)
BackboneJS Deprecated: Provides simple models, views, and collections. This will
be removed in future versions of the sandbox.
SugarJS: Extends native JS objects with useful methods
tv4 JSON schema validator: Validates JSON objects against v4 of the json-schema
draft
CryptoJS: standard and secure cryptographic algorithms. Supported algorithms: AE
S, DES, EvpKDF, HMAC-MD5, HMAC-SHA1/3/256/512, MD5, PBKDF2, Rabbit, SHA1/3/224/2
56/512, TripleDES
xml2Json(xmlString): This function behaves the same in Newman and Postman
xmlToJson(xmlString) Deprecated: This function does NOT behave the same in Newma
n and Postman
postman.getResponseHeader(headerName) Test-only: returns the response header wit
h name headerName , if it exists. Returns null if no such header exists. Note: Acco
rding to W3C specifications, header names are case-insensitive. This method take
s care of this. postman.getResponseHeader("Content-type") and postman.getRespons
eHeader("content-Type") will return the same value.
Note: jQuery support has been discontinued since version 4.6.0, in favour of che
erio.
Environment and global variables
postman.setEnvironmentVariable(variableName, variableValue): Sets an environment
variable variableName , and assigns the string variableValue to it. You must have an
environment selected for this method to work. Note: Only strings can be stored.
Storing other types of data will result in unexpected behavior.
postman.getEnvironmentVariable(variableName): Returns the value of an environmen
t variable variableName , for use in pre-request & test scripts. You must have an e
nvironment selected for this method to work.
postman.setGlobalVariable(variableName, variableValue): Sets a global variable va
riableName , and assigns the string variableValue to it. Note: Only strings can be s
tored. Storing other types of data will result in unexpected behavior.
postman.getGlobalVariable(variableName): Returns the value of a global variable v
ariableName , for use in pre-request & test scripts.
postman.clearEnvironmentVariable(variableName): Clears the environment variable
named variableName . You must have an environment selected for this method to work.
postman.clearGlobalVariable(variableName): Clears the global variable named varia
bleName .
postman.clearEnvironmentVariables(): Clears all environment variables. You must
have an environment selected for this method to work.
postman.clearGlobalVariables(): Clears all global variables.
environment: A dictionary of variables in the current environment. Use environme
nt["foo"] to access the value of the foo environment variable. **Note: **This can
only be used to read the variable. Use setEnvironmentVariable to set a value.
globals: A dictionary of global variables. Use globals["bar"] to access the valu
e of the bar global variable. **Note: **This can only be used to read the variable
. Use setGlobalVariable to set a value
Dynamic variables
Postman also has a few dynamic variables which you can use in your requests. Thi
s is primarily an experiment right now. More functions would be added soon. Note
that dynamic variables cannot be used in the Sandbox. You can only use them in
the {{..}} format in the request URL / headers / body.
{{$guid}}: Adds a v4 style guid
{{$timestamp}}: Adds the current timestamp.
{{$randomInt}}: Adds a random integer between 0 and 1000
Cookies
responseCookies {array} Postman-only: Gets all cookies set for the domain. You w
ill need to enable the Interceptor for this to work.
postman.getResponseCookie(cookieName) Postman-only: Gets the response cookie wit
h the given name. You will need to enable the interceptor for this to work. Chec
k out the blog post.
Request/response related properties
request {object}: Postman makes the request object available to you while writin
g scripts. This object is read-only. Changing properties of this object will hav
e no effect. Note: Variables will NOT be resolved in the request object. The req
uest object is composed of the following:
data {object} - this is a dictionary of form data for the request. (request.data
[ key ]== value )
headers {object} - this is a dictionary of headers for the request (request.head
ers[ key ]== value )
method {string} - GET/POST/PUT etc.
url {string} - the url for the request.
responseHeaders {object} Deprecated, Test-only: This is a map of the response he
aders. This is case-sensitive, and should not be used. Check the postman.getResp
onseHeader() method listed above.
responseBody {string} Test-only: A string containing the raw response body text.
You can use this as an input to JSON.parse, or xml2Json.
responseTime {number} Test-only: The response time in milliseconds
responseCode {object} Test-only: Contains three properties:
code {number}: The response code (200 for OK, 404 for Not Found etc)
name {string}: The status code text
detail {string}: An explanation of the response code
tests {object} Test-only: This object is for you to populate. Postman will treat
each property of this object as a boolean test.
iteration {number}: Only available in the Collection Runner and Newman. Represen
ts the current test run index. Starts from 0.
Test-only: This object is only available in the test script section. Using this
in a pre-request script will throw an error.
Data files
If you re using data files in the Collection Runner or in Newman, you ll have access
to a data object, which is a dictionary of data values in the current test run.
For examples on how to use the sandbox for a number of common use cases, check o
ut the examples page.

You might also like