API Test Cases in Postman using JavaScript

API Test Cases In Postman Using JavaScript

In the previous articles on Postman Tutorial, we have covered “How To Run Collections Using Newman

In this “API Test Cases in Postman using JavaScript” article, I will be demonstrating as to how you can implement this concept and get a tight grip over this.

With Postman, we can add scripts to our request and write tests.

Code added under the Pre-request Script tab will execute before the request is sent, and code added under the Tests tab will execute after the response is received.

Postman Prerequest Scripts Test Script

We use pre-request scripts for all the things we need to do before the execution of the request such as setting variables, clearing the variables, or set the environment variable in the environment.

We write Test scripts to validate API’s error handling by sending a request with incomplete data. Through Test scripts, we can use dynamic variables and can carry out test assertions on response data.

Postman Prerequest Scripts Test Scripts

Postman has a PM API (known as the pm.*API) which is the powerful way to write tests in Test tab.

pm.test():

The pm.test() function is used to write test specifications. It accepts 2 parameters, the name of the test (as a string) and a function to return a boolean value. It can be used only in the Tests tab after the primary Postman request has been sent.

Example:

pm.expect():

The pm.expect() assertion function makes it easy to write readable tests, and we can deal with assertions of data from a response or variables.

Example:

pm.response.to.be.* :

The pm.resonse.to.be object provides tests for response status types and body variations.

Example:

Test Cases:

We need to add any of the assertions inside a pm.test callback.

Example:

1) Status Code:

1.1 Check if the status code is 200:

1.2 Checking multiple status codes:

Postman Status Code

2) Response time:

2.1 Response time below 100ms:

Postman Response Time

3) Headers:

3.1 Header exists:

3.2 Header has value:

Postman Header

4) Cookies:

4.1 Cookie exists:

4.2 Cookie has value:

Postman Cookies

5) Body (Any content-type / HTML responses):

5.1 Exact body match:

5.2 Partial body match/body contains:

Postman Body

6) Body (JSON responses):

Parse body (need for all assertions)

const response = pm.response.json();

6.1 Simple value check:

6.2 Nested value check:

Postman JSon Body

7) Body (XML responses):

Convert XML body to JSON (need for all assertions):

const response = xml2Json(responseBody);

7.1 Simple value check:

7.2 Nested value check:

Postman XML Body

Next steps:

Learn “Generate Random/Dynamic data in Requests” in the next tutorial.

Author Bio: This article is written by Harsha Mittal an ISTQB-CTFL Certified Software Test Engineer having 3.5+ years of experience in Software Testing.

Related posts:

API Test Cases In Postman Using JavaScript

Get our latest blog posts delivered to your inbox

Subscribe and get popular blog posts about software testing industry.

Rajkumar

Leave a Comment

Share via
Copy link
Powered by Social Snap