Postman
Postman
com
9123820085
Postman
kasper-analytics
kasperanalytics.com
9123820085
TableofContents
1. Postman – Introduction ...................................................................................................................... 1
kasper-analytics
kasperanalytics.com
9123820085
Tests in Postman.............................................................................................................................................................................. 54
kasper-analytics
kasperanalytics.com
9123820085
kasper-analytics
kasperanalytics.com
9123820085
1. Postman – Introduction
Postman is an Application Programming Interface (API) testing tool. API acts like
an interface between a couple of applications and establishes a connection
between them.
Postman was designed in the year 2012 by software developer and entrepreneur
Abhinav Asthana to make API development and testing straightforward. It is a tool
for testing the software of an API. It can be used to design, document, verify,
create, and change APIs.
Postman has the feature of sending and observing the Hypertext Transfer Protocol
(HTTP) requests and responses. It has a graphical user interface (GUI) and can be
used in platforms like Linux, Windows and Mac. It can build multiple HTTP requests
– POST, PUT, GET, PATCH and translate them to code.
NeedofPostman
Postman has a huge user base and has become a very popular tool because of
the reasons listed below:
• Postman comes without any licensing cost and is suitable for use for the
• Postman can be accessed very easily by logging into your own account
• Postman allows easy maintenance of test suites with the help of collections.
[email protected]
1
kasper-analytics
kasperanalytics.com
9123820085
Users can make a collection of API calls which can have varied requests and
sub- folders.
• Postman is capable of building multiple API calls like SOAP, REST, and HTTP.
allows executing tests in much iteration. Thus we can avoid repeated tests.
• With Postman, we can create more than one environment. Thus, a single
collection can be used with various configurations.
WorkingwithPostman
• Header
• Response
• Sidebar
• Builder
[email protected]
2
kasper-analytics
kasperanalytics.com
9123820085
Header
Postman consists of New, Import, Runner (used to execute tests with Collection
Runner), Open New, Interceptor, Sync menus, and so on. It shows the workspace
name – My Workspace along with the option for Invite for sharing it among teams.
New menu is used to create a new Environment, Collection or request. The Import
menu helps to import an Environment/Collection.
[email protected]
3
kasper-analytics
kasperanalytics.com
9123820085
We can import from a File, Folder, Link, Raw text or from Code repository options
which are also available under Import.
Here, Open New is used to open a new tab, Postman or a Runner Window.
[email protected]
4
kasper-analytics
kasperanalytics.com
9123820085
Response
Response section shall have values populated only when a request is made. It
generally contains the Response details.
Sidebar
[email protected]
5
kasper-analytics
kasperanalytics.com
9123820085
Builder
Builder is the most important section of the Postman application. It has the
request tab and displays the current request name. By default, Untitled Request is
mentioned if no title is provided to a request.
The Builder section also contains the request type (GET, POST, PUT, and so on) and
URL. A request is executed with the Send button. If there are any modifications
done to a request, we can save it with the Save button.
[email protected]
6
kasper-analytics
kasperanalytics.com
9123820085
The Builder section has the tabs like Param, Authorization, Headers, Body, Pre-req.,
Tests and Settings. The parameters of a request in a key-value pair are
mentioned within the Params tab. The Authorization for an API with username,
password, tokens, and so on are within the Authorization tab.
The request headers, body are defined within the Headers and Body tab
respectively. Sometimes, there are pre-condition scripts to be executed prior to a
request. These are mentioned within the Pre-req. tab.
The Tests tab contains scripts that are run when a request is triggered. This helps
to validate if the API is working properly and the obtained data and Response
code is correct.
[email protected]
7
kasper-analytics
kasperanalytics.com
9123820085
Postman can be installed in operating systems like Mac, Windows and Linux. It is
basically an independent application which can be installed in the following ways:
StandaloneApplication
Then, click on Download the App button. As per the configuration of the operating
system, select either the Windows 32-bit or Windows 64-bit option.
The pop-up to save the executable file gets opened. Click on Save File.
[email protected]
8
kasper-analytics
kasperanalytics.com
9123820085
After installation, the Postman landing screen opens. Also, we have to sign up
here. There are two options to create a Postman account, which are as follows:
[email protected]
9
kasper-analytics
kasperanalytics.com
9123820085
Proceed with the steps of account creation and enter relevant details like name,
role, and so on.
[email protected]
10
kasper-analytics
kasperanalytics.com
9123820085
Finally, we shall land to the Start screen of Postman. The following screen will
appear on your computer:
[email protected]
11
kasper-analytics
kasperanalytics.com
9123820085
Chrome Extension
https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbn
cdddomo p?.
[email protected]
12
kasper-analytics
kasperanalytics.com
9123820085
Chrome Apps page gets launched, along with the Postman icon. Next, we have to
click on the Postman icon.
[email protected]
13
kasper-analytics
kasperanalytics.com
9123820085
[email protected]
14
kasper-analytics
kasperanalytics.com
9123820085
[email protected]
15
kasper-analytics
kasperanalytics.com
9123820085
Once we close the pop-up and move to the following page, we get the message -
[email protected]
16
kasper-analytics
kasperanalytics.com
9123820085
Variables give the option to hold and repeat parameters in the requests,
collections, scripts and so on. If we need to modify a value, we need to do it in only
one place. Thus, the variables help to minimise the chance of errors and increase
efficiency.
The key in the key-value pair in the environment is known as the Environment
variable. There can be multiple environments and each of them can also have
multiple variables. However, we can work with a single environment at one time.
Create Environment
[email protected]
17
kasper-analytics
kasperanalytics.com
9123820085
Step 3: The new Environment (ENV1) gets reflected as one of the items in the No
Environment dropdown.
Step 4: Select the ENV1 environment and enter {{u}} in the address bar. To utilise
an Environment variable in a request, we have to enclose it with double curly
braces ({{<Environment variable name>}}).
Step 5: Then, click on Send. This variable can be used instead of the actual URL. We
have received the Response code 200 0K (meaning the request is successful).
[email protected]
18
kasper-analytics
kasperanalytics.com
9123820085
The following screen shows the error, which we may get if we use the same
Environment variable u from another environment:
[email protected]
19
kasper-analytics
kasperanalytics.com
9123820085
[email protected]
20
kasper-analytics
kasperanalytics.com
9123820085
4. Postman — Authorization
The process of authorization is applied for the APIs which are required to be
secured. This authorization is done for identification and to verify, if the user is
entitled to access a server resource.
In the TYPE dropdown, there are various types of Authorization options, which are
as shown below:
[email protected]
21
kasper-analytics
kasperanalytics.com
9123820085
Let us now create a POST request with the APIs from GitHub Developer having an
endpoint https://www.api.github.com/user/repos. In the Postman, click the Body
tab and select the option raw and then choose the JSON format.
"name" : "Tutorialspoint"
}
[email protected]
22
kasper-analytics
kasperanalytics.com
9123820085
The Response code obtained is 401 Unauthorized. This means, we need to pass
authorization to use this resource. To authorize, select any option from the TYPE
dropdown within the Authorization tab.
Types of Authorization
Let us discuss some of the important authorization types namely Bearer Token
and Basic Authentication.
Bearer Token
For Bearer Token Authorization, we have to choose the option Bearer Token from
the TYPE dropdown. After this, the Token field gets displayed which needs to be
provided in order to complete the Authorization.
Step 1: To get the Token for the GitHub API, first login to the GitHub account by
clicking on the link given herewith: https://github.com/login.
Step 2: After logging in, click on the upper right corner of the screen and select the
Settings option.
[email protected]
23
kasper-analytics
kasperanalytics.com
9123820085
Provide a Note and select option repo. Then, click on Generate Token at the
bottom of the page.
[email protected]
24
kasper-analytics
kasperanalytics.com
9123820085
Copy the Token and paste it within the Token field under the Authorization tab in
Postman. Then, click on Send.
Please note: Here, the Token is unique to a particular GitHub account and should
not be shared.
Response
The Response code is 201 Created which means that the request is successful.
[email protected]
25
kasper-analytics
kasperanalytics.com
9123820085
Basic Authentication
For Basic Authentication Authorization, we have to choose the option Basic Auth
from the TYPE dropdown, so that the Username and Password fields get displayed.
First we shall send a GET request for an endpoint (https://postman-
echo.com/basic- auth) with the option No Auth selected from the TYPE dropdown.
Please note: The username for the above endpoint is postman and password is
password.
The Response Code obtained is 401 Unauthorized. This means that Authorization
did not pass for this API.
Now, let us select the option Basic Auth as the Authorization type, following which
the Username and Password fields get displayed.
Enter the postman for the Username and password for the Password field. Then,
click on
Send.
[email protected]
26
kasper-analytics
kasperanalytics.com
9123820085
The Response code obtained is now 200 OK, which means that our request has
been sent successfully.
No Auth
We can also carry out Basic Authentication using the request Header. First, we
have to choose the option as No Auth from the Authorization tab. Then in the
Headers tab, we have to add a key: value pair.
We shall have the key as Authorization and the value is the username and
password of the user in the format as basic <encoded credential>.
[email protected]
27
kasper-analytics
kasperanalytics.com
9123820085
Please note: The username for our endpoint here is postman and password is
password. Enter postman: password in the edit box and click on Encode. The
encoded value gets populated at the bottom.
[email protected]
28
kasper-analytics
kasperanalytics.com
9123820085
The Response code obtained is 200 OK, which means that our request has been
sent successfully.
[email protected]
29
kasper-analytics
kasperanalytics.com
9123820085
Authorization at Collections
Step 1: Click on the three dots beside the Collection name in Postman and select
the option Edit.
Step 2: The EDIT COLLECTION pop-up comes up. Move to the Authorization tab and
then select any option from the TYPE dropdown. Click on Update.
[email protected]
30
kasper-analytics
kasperanalytics.com
9123820085
[email protected]
31
kasper-analytics
kasperanalytics.com
9123820085
5. Postman — Workflows
In a Postman Collection, the requests are executed in the order in which they
appear. Every request is run first by the order of the folder followed by any request
at the Collection root.
Let us create a Collection (Collection1) with four requests. The details on how to
create a Collection is discussed in detail in the Chapter about Create Collections.
Step 1: Click on the arrow appearing to the right of the Collection name in the
sidebar. Then, click on Run button to trigger execution of requests within the
Collection.
Step 2: The Collection Runner pop-up comes up. The RUN ORDER section shows
the order in which the requests shall get executed from top to the bottom. (GET-
>POST->DEL->PUT). Click on the Run Collection1 button.
[email protected]
32
kasper-analytics
kasperanalytics.com
9123820085
Step 3: Execution Results show the GET request executed first, followed by POST,
then DEL and finally PUT, as mentioned in the RUN ORDER section in the Step 2.
[email protected]
33
kasper-analytics
kasperanalytics.com
9123820085
If we want to change the order of the request to be executed (for example, first
the Get Request shall run, followed by Create User, then Update Request and
finally the Delete Request). We have to take the help of the function
postman.setNextRequest().
This function has the feature to state which request shall execute next. The
request name to be executed next is passed as a parameter to this function. As
per the workflow, we have to add this function either in the Tests or Pre-request
Script tab under the endpoint address bar in Postman.
Implementation of a Workflow
Step 1: Add the below script under the Tests tab, for the request – Create User.
Step 2: Add the below script under the Tests tab, for the request – Update Request.
[email protected]
34
kasper-analytics
kasperanalytics.com
9123820085
Output of Workflow
The output shows that Update Request and Delete Request are running in an
infinite loop until we stop it by clicking the Stop Run button.
[email protected]
35
kasper-analytics
kasperanalytics.com
9123820085
If we want to stop the infinite Workflow loop via script, we have to add the below
script for the request – Delete Request.
The output shows the order of execution as Get Request, Create User, Update
Request and finally Delete Request.
[email protected]
36
kasper-analytics
kasperanalytics.com
9123820085
A GET request is used to obtain details from the server and does not have any
impact on the server. The GET request does not update any server data while it is
triggered. The server only sends its Response to the request.
Follow the steps given below to create a GET request successfully in Postman:
Step 1: Click on the New menu from the Postman application. The Create New
pop-up comes up. Then click on the Request link.
Step 2: SAVE REQUEST pop-up comes up. Enter the Request name then click on
Save.
[email protected]
37
kasper-analytics
kasperanalytics.com
9123820085
Step 3: The Request name (Test1) gets reflected on the Request tab. We shall then
select the option GET from the HTTP request dropdown.
[email protected]
38
kasper-analytics
kasperanalytics.com
9123820085
Response
Once a request has been sent, we can see the response code 200 OK populated
in the Response. This signifies a successful request and a correct endpoint. Also,
information on the time consumed to complete the request (223 ms) and
payload size (20.75 KB) are populated.
[email protected]
39
kasper-analytics
kasperanalytics.com
9123820085
On hovering over the response time, we can see the time taken by different
events like DNS Lookup, SSL Handshake and so on.
On hovering over the payload size, the details on the size of response, headers,
Body, and so on are displayed.
[email protected]
40
kasper-analytics
kasperanalytics.com
9123820085
The Response Body contains the sub-tabs – Pretty, Raw and Preview. The Pretty
format shows color formatting for keywords and indentation for easy reading. The
Raw format displays the same data displayed in the Pretty tab but without any
color or indentation.
[email protected]
41
kasper-analytics
kasperanalytics.com
9123820085
Raw tab
Preview tab
The Response also contains the Cookies, Headers and Test Results.
[email protected]
42
kasper-analytics
kasperanalytics.com
9123820085
Postman POST request allows appending data to the endpoint. This is a method
used to add information within the request body in the server. It is commonly used
for passing delicate information.
Once we send some the request body via POST method, the API in turn yields
certain information to us in Response. Thus, a POST request is always
accompanied with a body in a proper format.
Follow the steps given below to create a POST request successfully in Postman:
Step 1: Click on the New menu from the Postman application. The Create New
pop-up comes up. Then, click on the Request link.
Step 2: SAVE REQUEST pop-up comes up. Enter the Request name then click on
Save.
[email protected]
43
kasper-analytics
kasperanalytics.com
9123820085
Step 3: The Request name (Test1) gets reflected on the Request tab. Also, we shall
select the option POST from the HTTP request dropdown.
[email protected]
44
kasper-analytics
kasperanalytics.com
9123820085
Step 4: Move to the Body tab below the address bar and select the option raw.
[email protected]
45
kasper-analytics
kasperanalytics.com
9123820085
Step 6: Copy and paste the below information in the Postman Body tab.
The above data that is being sent via POST method is only applicable to the
endpoint: https://jsonplaceholder.typicode.com/users
To pass the data in the correct JSON format, we can use the Jsonformatter
available in the below link:
https://jsonformatter.curiousconcept.com/
[email protected]
46
kasper-analytics
kasperanalytics.com
9123820085
Response
Once a request has been sent, we can see the response code 201 Created
populated in the Response. This signifies a successful request and the request we
have sent has been accepted by the server.
[email protected]
47
kasper-analytics
kasperanalytics.com
9123820085
Also, information on the time consumed to complete the request (347 ms) and
payload size (1.61 KB) are populated.
We can see that the Response body is the same as the request body which we
have sent to the server.
[email protected]
48
kasper-analytics
kasperanalytics.com
9123820085
A Postman PUT request is used to pass data to the server for creation or
modification of a resource. The difference between POST and PUT is that POST
request is not idempotent.
This means invoking the same PUT request numerous times will always yield the
same output. But invoking the same POST request numerous times will create the
similar resource more than one time.
Before creating a PUT request, we shall first send a GET request to the server on an
endpoint: http://dummy.restapiexample.com/api/v1/employees. The details on
how to create a GET request is explained in detail in the Chapter – Postman GET
Requests.
Now, let us update the employee_salary and employee_age for the id 21 with the
help of the PUT request.
[email protected]
49
kasper-analytics
kasperanalytics.com
9123820085
Follow the steps given below to create a PUT request in Postman successfully:
Step 1: Click on the New menu from the Postman application. The Create New
pop-up comes up. Then, click on the Request link.
Step 2: SAVE REQUEST pop-up comes up. Enter the Request name then click on
Save.
[email protected]
50
kasper-analytics
kasperanalytics.com
9123820085
Step 3: The Request name (Test1) gets reflected on the Request tab. We shall
select the option PUT from the HTTP request dropdown.
It must be noted that in a PUT request, we have to mention the id of the resource
in the server which we want to update in the URL.
Step 4: Move to the Body tab below the address bar and select the option raw.
[email protected]
51
kasper-analytics
kasperanalytics.com
9123820085
Step 6: Copy and paste the below information in the Postman Body tab.
The overall parameters to be set for a PUT request are shown below:
[email protected]
52
kasper-analytics
kasperanalytics.com
9123820085
Response
Once a request has been sent, we can see the response code 200 OK populated
in the Response body. This signifies a successful request and the request we have
sent has been accepted by the server.
Also, information on the time consumed to complete the request (673 ms) and
payload size (705 B) are populated. The Response body shows the salary and age
got updated to 2000 and 15 respectively for the employee having id 21.
[email protected]
53
kasper-analytics
kasperanalytics.com
9123820085
Postman DELETE request deletes a resource already present in the server. The
DELETE method sends a request to the server for deleting the request mentioned
in the endpoint. Thus, it is capable of updating data on the server.
Before creating a DELETE request, we shall first send a GET request to the server on
the endpoint: http://dummy.restapiexample.com/api/v1/employees. The details
on how to create a GET request is explained in detail in the Chapter onGET
Requests.
[email protected]
54
kasper-analytics
kasperanalytics.com
9123820085
Follow the steps given below to create a DELETE request in Postman successfully:
Step 1: Click on the New menu from the Postman application. The Create New
pop-up comes up. Then, click on the Request link.
Step 2: SAVE REQUEST pop-up comes up. Enter the Request name then click on
Save.
[email protected]
55
kasper-analytics
kasperanalytics.com
9123820085
Step 3: The Request name (Test1) gets reflected on the Request tab. We shall
select the option DELETE from the HTTP request dropdown.
Here, in the DELETE request, we have mentioned the id of the resource in the server
which we want to delete in the URL.
Response
Once a request has been sent, we can see the Response code 200 OK populated
in the Response. This signifies a successful request and the request we have sent
has been accepted by the server.
Also, information on the time consumed to complete the request (734 ms) and
payload size (652 B) are populated. The Response shows the status as success.
The record id 2 gets deleted from the server.
[email protected]
56
kasper-analytics
kasperanalytics.com
9123820085
After deletion of the record with id 2, if we run the GET request on the endpoint:
http://dummy.restapiexample.com/api/v1/employee/2, we shall receive 401
Unauthorized status code.
[email protected]
57
kasper-analytics
kasperanalytics.com
9123820085
CRUD stands for Create, Retrieve, Update and Delete operations on any website
opened in a browser. Whenever we launch an application, the retrieve operation
is performed.
On creating data, for example, adding a new user for a website, the create
operation is performed. If we are modifying the information, for example,
changing details of an existing customer in a website, the update operation is
performed.
Finally, to eliminate any information, for example, deleting a user in a website, the
delete operation is carried out.
To retrieve a resource from the server, the HTTP method – GET is used (discussed
in details in the Chapter – Postman GET Requests). To create a resource in the
server, the HTTP method – POST is used (discussed in details in the Chapter –
Postman POST Requests).
To modify a resource in the server, the HTTP method – PUT is used (discussed in
details in the Chapter – Postman PUT Requests). To delete a resource in the
server, the HTTP method – DELETE is used (discussed in details in the Chapter –
Postman DELETE Requests).
Tests in Postman
In Postman, tests are developed in JavaScript and can be developed using the
JavaScript and Functional methods. Both the techniques are based on the
language JavaScript.
[email protected]
58
kasper-analytics
kasperanalytics.com
9123820085
JavaScript Method
Step 1: Tests developed in the JavaScript method are mentioned within the Tests
tab under the address bar.
Step 2: Add the below JavaScript verifications within the Tests tab:
We can add one or more than one test for a particular request.
Here, tests is a variable of type array which can hold data types- integer, string,
Boolean and so on. The Status Code should be 200 and Response time lesser than
10ms are the names of the tests. It is recommended to give meaningful names to
test.
The responseCode.code is the response code obtained in the Response and the
Step 3: Select the GET method and enter an endpoint then click on Send.
Response
[email protected]
59
kasper-analytics
kasperanalytics.com
9123820085
The Test Results tab shows the test which has passed in green and the test which
has failed in red. The Test Results (1/2) means one out of the two tests has passed.
Response shows the status as 200 OK and Response time as 129ms (the second
test checks if the Response time is less than 10ms).
Hence, the first test got passed and the second one failed along with the Assertion
error.
Functional Method
Follow the steps given below to develop a test in with functional method:
Step 1: Tests developed in the Functional method are mentioned within the Tests
tab under the address bar.
[email protected]
60
kasper-analytics
kasperanalytics.com
9123820085
Here, pm.test is the function for the test being performed. Status Code is 401 and it
is the name of the test which shall be visible in the Test Result after execution.
The pm.response is used for obtaining the response and adding assertions on it to
verify the header, code, status, and so on.
Step 3: Select the GET method and enter an endpoint then click on Send.
Response
The Test Results tab shows the test in red as the test has failed. The Test Results
(0/1) means zero out of the one test has passed. Response shows the status as
200 OK (the test checks if the response code is 401).
Hence the test shows failed along with the Assertion error.
[email protected]
61
kasper-analytics
kasperanalytics.com
9123820085
A group of requests that have been saved and organized into folders is known as
the Collections. It is similar to a repository. Thus, Collections help to maintain the
API tests and also split them easily with teams.
Step 1: Click on the New icon from the Postman application. The Create New pop-
up comes up. Then click on the Collection link.
[email protected]
62
kasper-analytics
kasperanalytics.com
9123820085
Step 2: CREATE A NEW COLLECTION pop-up comes up. Enter a Collection Name and
click on the Create button.
Step 3: The Collection name and the number of requests it contains are displayed
in the sidebar under the Collections tab
[email protected]
63
kasper-analytics
kasperanalytics.com
9123820085
Step 4: To the right of the Collection name, we have the options like Share, Run
and so on available. Click on the three dots to get more options to select.
Step 5: Click on Add Request. The SAVE REQUEST pop-up comes up. Enter Request
Name and select the Collection we have created. Then, click on the Save to
Collection1 button.
[email protected]
64
kasper-analytics
kasperanalytics.com
9123820085
Step 6: The Collection with its request gets displayed to the side bar under the
Collections tab.
[email protected]
65
kasper-analytics
kasperanalytics.com
9123820085
We can parameterize Postman requests to execute the same request with various
sets of data. This is done with the help of variables along with parameters. A
parameter is a part of the URL used to pass more information to the server.
The data can be used in the form of a data file or an Environment variable.
Parameterization is an important feature of Postman and helps to eliminate
redundant tests. Parameters are enclosed in double curly braces {{parameter}}.
Example
Step 1: Click on the eye icon to the right of the Environment dropdown in the top
right corner in the Postman application.
[email protected]
66
kasper-analytics
kasperanalytics.com
9123820085
Step 3: MANAGE ENVIRONMENTS pop-up comes up. Enter URL for the VARIABLE field
and https://www.tutorialspoint.com for INITIAL VALUE. Then, click on Save.
[email protected]
67
kasper-analytics
kasperanalytics.com
9123820085
Step 5: In the Http Request tab, enter {{url}}/index.htm in the address bar. Select
the GET method and click on Send.
Response
Once a request has been sent, we can see the response code 200 OK populated
in the Response. This signifies a successful request and a correct endpoint.
[email protected]
68
kasper-analytics
kasperanalytics.com
9123820085
The Collection Runner console displays the test results for individual requests. It is
mandatory to have more than one request within the Collection to work with
Collection Runner.
Follow the steps given below to execute the tests with Collection Runner in
Postman:
Step 1: Click on the Runner menu present at the top of the Postman application.
[email protected]
69
kasper-analytics
kasperanalytics.com
9123820085
[email protected]
70
kasper-analytics
kasperanalytics.com
9123820085
Step 5: If we have data in a file, then we have to choose the file type from Data.
Then, click on the Run Collection1 button.
Step 6: The Run Results page shall come up. Depending on the delay time
provided, the tests should get executed.
The test results (Pass/Fail) should be displayed for each iteration. The pass status
is represented in green and failed ones are represented in red. If there is no test
implemented for a particular request, then it shall display the message as - This
request does not have any tests.
This is the environment in which the tests are executed and the Collection names
are visible at the top of the Collection Runner. For each request, the status code,
time taken, payload size, and test verification are also displayed.
[email protected]
71
kasper-analytics
kasperanalytics.com
9123820085
[email protected]
72
kasper-analytics
kasperanalytics.com
9123820085
Assertions are used to verify if the actual and expected values have matched
after the execution of a test. If they are not matching, the test shall fail and we
shall get the reason for failure from the output of the test.
The Chai – Assertions are easily comprehensible as they are defined in a human
readable format. The Assertions in Postman are written within the Tests tab under
the address bar.
Writing Assertions
Output
[email protected]
73
kasper-analytics
kasperanalytics.com
9123820085
Output
[email protected]
74
kasper-analytics
kasperanalytics.com
9123820085
Assertionfor ObjectVerification
Let us write an Assertion for object verification with eql. It is used to compare the
properties of the object i and j in the below example.
Output
The property defined for object i is Postman while the property defined for j is
Cypress. Hence, not.eql Assertion got passed.
[email protected]
75
kasper-analytics
kasperanalytics.com
9123820085
Assertion Types
Status Code
The above assertion passes if the Response status code obtained is 401.
The above assertion is applied on the Response property – status having the
value Forbidden.
[email protected]
76
kasper-analytics
kasperanalytics.com
9123820085
Header of Response
Text of Response
The above assertion passes if the Response text contains the text Tutorialspoint.
[email protected]
77
kasper-analytics
kasperanalytics.com
9123820085
A mock server is not a real server and it is created to simulate and function as a
real server to verify APIs and their responses. These are commonly used if certain
responses need to be verified but are not available on the web servers due to
security concerns on the actual server.
[email protected]
78
kasper-analytics
kasperanalytics.com
9123820085
Follow the steps given below for creation of mock server in Postman:
Step 1: Click on the New icon from the Postman application. Then, click on Mock
Server.
Step 2: Select GET from the Method dropdown, enter a Request Path as
/user/home,
[email protected]
79
kasper-analytics
kasperanalytics.com
9123820085
Step 3: Enter a Mock Server name and click on the Create Mock Server button.
Step 4: The Mock Server gets created along with the Mock URL. The Copy Mock URL
button is used to copy the Mock link. Click on the Close button to proceed.
[email protected]
80
kasper-analytics
kasperanalytics.com
9123820085
Step 5: Select Mock Server as the Environment from the No Environment dropdown
and click on Send. The Response code obtained is 200 OK which means that the
request is successful.
Also the Response Body shows the message – This is Postman Tutorial for
Tutorialspoint which is the same as we passed as a Response Body in the Step 2.
Step 6: The value of URL can be obtained by clicking on the eye icon at the right
upper corner of the screen.
[email protected]
81
kasper-analytics
kasperanalytics.com
9123820085
Step 7: We have seen that the Response Body is in text format. We can get the
response in JSON format as well. To achieve this select the option Save as
example from the Save Response dropdown.
Step 8: Provide an Example name and select JSON from the Response Body
section.
[email protected]
82
kasper-analytics
kasperanalytics.com
9123820085
Step 9: Add the below Response Body in JSON format. Then click on Save Example.
Step 10: Finally, send the GET request on the same endpoint, and we shall receive
the same Response Body as we have passed in the Example request.
[email protected]
83
kasper-analytics
kasperanalytics.com
9123820085
Example Request
Follow the steps given below for Mock Server Creation by example request:
[email protected]
84
kasper-analytics
kasperanalytics.com
9123820085
Step 3: From Response Body, select the option Save as Example from the Save
Response dropdown.
Step 4: Give an Example name and click on the Save Example button.
[email protected]
85
kasper-analytics
kasperanalytics.com
9123820085
Step 5: Click on the Collection name Mock Server (that we have created) and click
on the Mock tab. Then, click on Create a mock server.
[email protected]
86
kasper-analytics
kasperanalytics.com
9123820085
Step 6: The Create mock server pop-up comes up. Provide a name to the Mock
Server and then click on the Create Mock Server button.
Please note: We can make a Mock Server private or public. To make a Mock Server
private, we have to check the checkbox Make this mock server private. Then, we
need to utilise the Postman API key.
Step 7: The message – Mock server created shall come up. Also, we shall get the
Mock URL. We can copy it with the Copy Mock URL button. Then, click on Close.
Step 8: The Mock Server which we have created gets reflected under the Mock tab
in the Collections sidebar. Click on the same.
[email protected]
87
kasper-analytics
kasperanalytics.com
9123820085
Step 9: We shall add a new request and paste the URL we have copied in Step 7.
To send a GET request, we shall append the value - /get at the end of the pasted
URL.
[email protected]
88
kasper-analytics
kasperanalytics.com
9123820085
The Response Body received by mocking the server is the same as the Example
request. Response obtained in the Example request is as follows:
[email protected]
89
kasper-analytics
kasperanalytics.com
9123820085
The cookies are information sent by the server and stored in the browser. As soon
as a request is sent, the cookies are returned by the server. In Postman, the
cookies are mentioned under the Headers and Cookies tab in the Response.
In the Headers tab, the cookie sent by the server is set with the key - set-cookie.
In the Cookies tab, the same cookie details will also get displayed.
[email protected]
90
kasper-analytics
kasperanalytics.com
9123820085
Cookies Management
Click on the Cookies link. MANAGE COOKIES pop-up shall open where all the
available cookies are present with the option to add and delete a cookie.
Cookies Addition
Step 1: Click on the Add Cookie button. A text box shall open up with pre-existing
values inside it. We can modify its values and then click on Save.
[email protected]
91
kasper-analytics
kasperanalytics.com
9123820085
The Response code obtained is 200 OK. Also, the Cookies tab in the Response now
shows the newly added cookie – Cookie_Postman.
[email protected]
92
kasper-analytics
kasperanalytics.com
9123820085
Cookie Creation
We can create a cookie with the .set() function. It accepts URL, name of cookie,
value of cookie as parameters.
Get Cookie
We can get a cookie with the .get() function. It accepts the URL, name of cookie as
parameters. It yields the cookie value.
[email protected]
93
kasper-analytics
kasperanalytics.com
9123820085
We can get all cookies of a specific URL within a Cookie jar with the .getAll()
function. It accepts a URL as a parameter. It yields all the cookie values for that
URL.
Delete Cookie
We can delete a cookie with the .unset() function. It accepts the URL, name of
cookie to be deleted as parameters.
We can delete all cookies of a specific URL with the .clear() function. It accepts a
URL as a parameter. It removes all the cookie values for that URL.
[email protected]
94
kasper-analytics
kasperanalytics.com
9123820085
A session is a temporary fold that stores values of variables. They are used for the
present instance and have a local scope. In Postman, we can modify the session
variable value to share workspace among teams.
Postman gives the feature of local session share. Even if a Collection can be
shared among teams, the sessions are never shared. Different tokens have to be
generated while a task is to be carried out in a team structure.
A session has a local scope for a user within his Workspace and any modifications
he makes shall not be reflected in the server. In Postman, a session can store
Environment variables, global variables and so on.
We can assign current values to Collection variables and to the global and
Environment variables. To assign a current value to the Collection, click on the
three dots appearing beside the Collection name, then click on Edit.
[email protected]
95
kasper-analytics
kasperanalytics.com
9123820085
The CURRENT VALUE is local to the user and never in sync with the server of
Postman. We can also replace or modify the INITIAL VALUE with CURRENT VALUE.
Also, it must be remembered that the INITIAL VALUE gets impacted only if we apply
the option Persist on a variable. After that, it gets in sync with the server of
Postman.
[email protected]
96
kasper-analytics
kasperanalytics.com
9123820085
We can run the requests within a Collection from Newman in the same way as in
the Collection Runner. Newman can occupy both the NPM registry and GitHub. We
can also perform Continuous Integration or Deployment with Newman.
A status code of 0 is thrown by Newman if all the execution is done without errors.
The Continuous Integration tools read the status code and accordingly fail/pass a
build.
We can add the flag --bail to the Newman to pause on an error encountered in a
test with a status code of 1. This can be interpreted by the CI tools. Newman is
based on node.js and uses npm as a package manager.
Newman Installation
The installation of Newman requires Node.js and npm. Follow the steps given
below to install Newman:
[email protected]
97
kasper-analytics
kasperanalytics.com
9123820085
Step 2: Once the download is completed, execute the below command to verify
that the Node.js is installed properly.
The below image shows the version v10.15.2 of the Node.js is installed in the
system.
[email protected]
98
kasper-analytics
kasperanalytics.com
9123820085
Step 3: The npm is allocated with Node.js so once we download the Node.js then
npm gets downloaded by default. To verify if npm is available in our system, run
the below command:
The below image shows the version 5.8.0 of the npm installed in the system:
Step 5: To verify the version of newman, run the below commands: The command
for verifying the installation in Windows is as follows:
newman --v
[email protected]
99
kasper-analytics
kasperanalytics.com
9123820085
To run Collections using Newman, we have to first launch the Postman application
and click on the three dots available beside the Collection name. The details on
how to create a Collection are discussed in detail in the Chapter – Postman
Create Collections.
Run Collections
[email protected]
100
kasper-analytics
kasperanalytics.com
9123820085
Step 4: Next, we shall export the Environment. Click on the eye icon to the right of
the No Environment dropdown.
[email protected]
101
kasper-analytics
kasperanalytics.com
9123820085
Step 6: MANAGE ENVIRONMENTS pop-up comes up. Enter URL for the VARIABLE field
and https://www.tutorialspoint.com for INITIAL VALUE. Then, click on Download as
JSON.
[email protected]
102
kasper-analytics
kasperanalytics.com
9123820085
Step 4: Next, we shall export the Environment. Click on the eye icon to the right of
the No Environment dropdown.
Step 6: MANAGE ENVIRONMENTS pop-up comes up. Enter URL for the VARIABLE field
and https://www.tutorialspoint.com for INITIAL VALUE. Then, click on Download as
JSON.
[email protected]
103
kasper-analytics
kasperanalytics.com
9123820085
Step 7: Export the Environment to the same location where the Collection resides.
Step 8: From the command-line move to the directory path where the Collection
and the Environment is stored. Then, execute the command given below:
The file name should always be in inverted quotes; else it shall be taken as a
directory name.
[email protected]
104
kasper-analytics
kasperanalytics.com
9123820085
[email protected]
105
kasper-analytics
kasperanalytics.com
9123820085
As the application allows the user access, it asks for an access token from the
server by providing user information. In turn, the server yields an access token.
The client can then access the secured data via the access token.
[email protected]
106
kasper-analytics