0% found this document useful (0 votes)
54 views7 pages

A-Z of API Testing Practical

The document provides an introduction to using Postman for API development, including creating workspaces, collections, and requests. It covers essential tasks such as sending requests, using variables, and implementing authorization and automation scripts. Additionally, it discusses running collections with Postman Test Runner and using Newman for command-line execution and reporting.

Uploaded by

lgmeri765
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views7 pages

A-Z of API Testing Practical

The document provides an introduction to using Postman for API development, including creating workspaces, collections, and requests. It covers essential tasks such as sending requests, using variables, and implementing authorization and automation scripts. Additionally, it discusses running collections with Postman Test Runner and using Newman for command-line execution and reporting.

Uploaded by

lgmeri765
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lesson One: Introduction to Postman

Postman is an API platform for building and testing APIs

Task: Create a workspace

REST API allows you to CRUD (Create, Read, Update, Delete). Postman can be used as a desktop app
or a web browser.

Navigate to https://www.postman.com/

1.​ Sign in if you have an account, or sign up for free!

2.​ Workspaces dropdown > Create Workspace

3.​ Select Blank Workspace as Template and Name your workspace

4.​ Set the visibility to either 'public' or 'private'. Then click "Create Workspace."

Task: Create a collection

Collections are places to organize your API requests in Postman.

From the left pane, either click the plus ("+") icon or Create a collection.

1.​ Select Blank collection

2.​ Name your new Collection

Task: Create your first request

1.​ Create a new request by either clicking Add a request inside your new Collection or hovering on
your Collection, then clicking the three dots icon and "Add request."
2.​ Name your request "Get Book". Set the request method to POST, and the request URL to
https://library-api.postmanlabs.com/books
3.​ Send your request by clicking the Send button

Note: In addition to a request method, a request must include a request URL that indicates where to
make the API call. A request URL has three parts: a protocol (such as http:// or https://), host (location
of the server), and path (route on the server). In REST APIs, the path often points to a reference entity,
like "books".

Protocol Host Path

https:// library-api.postmanlabs.com /books


Variables in Postman

Postman allows you to save values as variables to reuse them and easily hide sensitive information like
API Keys.

Postman supports the following variable scopes:

•​ Global
•​ Collection
•​ Environment
•​ Data
•​ Local
Set the baseUrl in the collection variable

Where are my variables stored?

You can find Collection variables in your collection. Click on your collection, then the Variables tab.
Here you can view and edit your variables.
Query parameters

Query parameters are added to the end of the path. They start with a question mark ?, followed by the

key-value pairs in the format: <key>=<value>.

Task: Make your second request

1.​ Duplicate the Get Book request and rename it “Get all Fiction Books”
2.​ Click Params >Insert fiction in the Key
3.​ Save the request and hit send.

Fiction is the query parameter in this request.

Path Variable

Another way of passing request data to an API is via path variables (a.k.a. "path parameters"). A path
variable is a dynamic section of a path and is often used for IDs and entity names, such as usernames.

Task: Make your Third Request

1.​ Duplicate the Get Book request and rename it “Get Book by the ID”
2.​ Add /:id to the URL
3.​ Click Params >Fill the ID value with any book ID
4.​ Save the request and hit send.
ID is the path parameter in this request.

JSON Body

The Body tab in Postman enables you to specify the data you need to send with a request. You can
send different types of body data to suit your API.

Task: Make your Fourth Request

1.​ Hover over your Library Collection, click the three dots icon and select Add request. Name your
new request "Add Book."
2.​ Set the request URL to {{baseUrl}}/books
3.​ Click the Body tab of the request > select that data type raw > JSON
4.​ Copy this object and replace the values with details about your book.

{
"title": "A-Z of API Testing",
"author": "Akpos Best",
"genre": "fiction",
"yearPublished": 2025
}
5.​ Save the request and hit send.

Error: 400 Bad Request - This explains the importance of a valid Request Method.

Change the Request Method to POST, save and Resend the request.

Error: 401 Unauthorized - Dive into Authorization in Postman

Authorization

Some APIs require Authorization (aka Auth) for certain endpoints to permit a request.

Task: Add Auth to the Collection

1.​ Click on your collection > Select the Authorization (or Auth) tab
2.​ Select API Key as the auth Type
3.​ Enter the API Key details in the fields below.

Key: api-key, Value: postmanrulz, Add to: Header


4.​ Save and resend the Add Book request.

Note: To use this auth in any of your requests, select ‘inherit from parent’ in the auth Type

Lesson Two: Automation in Postman

Scripts

Postman allows you to add automation and dynamic behaviors to your collections with scripting.

1.​ Pre-request: Runs before your actual test


2.​ Post-response: Runs after your test.

The pm object in Postman gives you access to data about your Postman environment, requests,
responses, variables and testing utilities.

Task: Create a new collection and request

1.​ Create a new collection > Create a new request > Request Method: POST
2.​ Use https://rahulshettyacademy.com/Library/Addbook.php as the request URL
3.​ Set the baseURL variable in the new collection.
4.​ Copy and paste this as the request body. Remember to modify the data

​ {
"name": "A-Z of API Testing",
"Isbn": "dnd",
"Aisle": "900",
"Author": "Akpos Best"
}
Success status code 201 created.

Rerun the request > Error: "Book Already Exists"

Let’s combine variables and scripting in our Pre-request so that each time we send the “Add
Book” request, your request picks new data.

Task: Your first automation script

pm.collectionVariables.set("variableName", value). To set a collection variable, use the .set()


pm.variables.replaceIn('{{$randomFirstName}}'). To use dynamic variables in pre-request or
post-response scripts, use pm.variables.replaceIn()

Once a variable is defined, you can access its value using double curly brace syntax like this:
{{variableName}}

https://learning.postman.com/docs/tests-and-scripts/write-scripts/variables-list/

1.​ Click on your new collection > Click Variables > Set the following variables;

bookName
isbn
aisle
authorName
2.​ Leave the values empty > Click Save
3.​ Click on the Scripts tab > Click Pre-request
4.​ Type pm.collectionVariables.set("bookName”, pm.variables.replaceIn('{{$randomWords}}').
Do this for all the other variables.
5.​ Replace the values with the variable name: {{bookName}}
6.​ Save and Send

Task: Your second automation script

To get a collection variable use the .get()

In JavaScript, we use const or let keywords to create local variables in our script.

1.​ Create a new request to “Get Book by ID” > add /Library/GetBook.php/?ID to the baseUrl

In this request, we will use Post-response scripts to grab the ID and store it in a variable, then
use it in our test.

2.​ Create a variable in your collection named ID > Click on the Scripts tab > Click Post-response
3.​ const bookID = pm.response.json().id

// save the "ID" value from the response to a variable named "bookID"

4.​ pm.collectionVariables.set("ID", bookID)


// save the ID as a collection variable named "bookID"

5.​ In your Get Book by ID request, fill the query param value with the variable name {{ID}}
6.​ Save and Send

Task: Create a Delete Book request

1.​ Create a new request to “Get Book by ID” > add Library/DeleteBook.php to the baseUrl
2.​ Set the request Body with the below

{
"ID" : "{{ID}}"
}
7.​ Save and Send

Assertions in Postman

Assertions in API testing are checks or validations that you write to confirm whether your API
response meets the expected outcome. In Postman, assertions are written using JavaScript in the
Scripts tab.

Task: Your third automation script

Common assertions in Postman

1.​ Add to your post-response scripts.

pm.test("Status code is 200", function () {

pm.response.to.have.status(200);

}); //Checks if response code is 200

2.​ pm.test("Verifies the response message", function () {

const responseData= pm.response.json();

pm.expect(responseData.Msg).to.eql("successfully added");

}); //Checks if the value of your response body

3.​ pm.test("Verifies the book ID", function () {


pm.expect(pm.response.json().ID).to.eql(pm.collectionVariables.get("ID"));

}); //Checks the response body matches the variables

Test Runner

The Postman Test Runner is a feature that allows you to run a collection of API requests sequentially.

​ Task: Run your collection

1.​ Click on your collection > Click Runs > Click Run

Newman in Postman

Newman is Postman's command-line tool that allows you to run collections from the terminal, while
newman-reporter-htmlextra generates a detailed HTML report of your test results.

1.​ Install Newman

Run this command in your terminal (Node.js must be installed):

npm install -g newman

2.​ Install the HTML Extra Reporter

npm install -g newman-reporter-htmlextra

3.​ Export Your Postman Collection and Environment

In Postman, go to your Collection → Click Export.

4.​ Run Your Collection with HTML Report

newman run Library.json -r htmlextra

newman run MyCollection.json -e MyEnvironment.json -r htmlextra //If you have an


environment.

You might also like