Postman Cheat Sheet


This Postman Cheat Sheet is based on the official documentation page of Postman (which is available in the below link) and from the overall knowledge on Postman −

https://learning.postman.com/docs/getting-started/introduction/

a. Variables

All the variables can be set up manually from the GUI of Postman and they have a defined scope. The values of the variables can also be set with the help of scripts written under the Pre-request Script or Tests tab.

The variables can be added in the request URL, Headers and Body in the format as {{<variable name>}}.

Usage in request URL −

https://{{domain}}about/{{id}}
Usage in request Headers(key-value):
X-{{key}}:value

Usage in request Body −

{"registration_id": "{{Id}}", "firstname": "Postman"}

b. Global Variables

The Global variables are used when we need to send data to other requests. The script to add a Global variable can be included either in the Tests or Pre-request Script tab in Postman.

To set a Global variable −

pm.globals.set('<name of Global variable>', '<value of variable>')

To get the value of a Global variable −

pm.globals.get('<name of Global variable>')

To delete a Global variable, the script is −

pm.globals.unset('<name of Global variable>')

To delete all Global variable, the script is −

pm.globals.clear()

c. Collection Variables

Collection variables are a good alternative to the Global and Environment variables. They can also be used for URLs/ authentication credentials in case there is only one Environment. The script to add a Collection variable can be included either in the Tests or Pre-request Script tab in Postman.

To set a Collection variable −

pm.CollectionVariables.set('<name of variable>', '<value of variable>')

To get the value of a Collection variable −

pm.CollectionVariables.get('<name of Collection variable>')

To delete a Collection variable, the script is−

pm.CollectionVariables.unset('<name of Collection variable>')

d. Environment Variables

The Environment variables are used for a particular Environment. They are a good alternative to the Global variables as they have a limited scope. The script to add a Global variable can be included either in the Tests or Pre-request Script tab in Postman.

Environment variables are used to hold variables specific for an Environment, URLs, and to send data to other requests.

To set an Environment variable −

pm.environment.set('<name of Environment variable>', '<value of variable>')

To get an Environment variable −

pm.environment.get('<name of Environment variable>')

To delete an Environment variable −

pm.environment.unset('<name of Environment variable>')

To delete all Environment variables, the script is −

pm.environment.clear()

To get the name of the active Environment, the script is −

pm.environment.name

e. Data Variables

Data variables are used for execution of a particular iteration in a Collection Runner or Newman. They are mainly used where there are multiple data-sets from a CSV/JSON file.

To get the value of a Data variable −

pm.iterationData.get('<name of Data variable>')

f. Local Variables

Local variables can be accessed within a request or while executing via Collection Runner/Newman. These variables are removed by default once the request has been executed.

To set a Local variable −

pm.variables.set('<name of Local variable>', '<value of variable>')

To get a Local variable −

pm.variables.get('<name of Local variable>')

g. Dynamic Variables

Dynamic variables can be used with strings to generate dynamic and distinct data.

Example of dynamic variable in JSON body −

{"email": "test.{{$timestamp}}@gmail.com"}

h. Debugging Variables

Launch the Postman Console and add console.log in the scripts under the Pre-request Scripts or Tests tab to debug a variable.

console.log(pm.globals.get('< name of Global variable >')

i. Assertions

Assertions are added under the block with pm.test callback.

pm.test("Response Status Code", function () {
pm.response.to.have.status(201)
})

j. Skipping Tests

To skip a test, we have to add pm.test.skip. The skipped tests are visible on the reports.

pm.test.skip("Response Status Code", function () {
pm.response.to.have.status(201)
})

k. Failing Tests

In Postman, we can explicitly fail a test without adding an Assertion.

pm.expect.fail('Scenario is failed');

Updated on: 25-Jun-2021

829 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements