Master Automated API Testing in Postman: From Basics to Advanced Scenarios

This guide walks you through using Postman to automate API testing, covering result validation, batch execution, variable handling, environment management, and dependency resolution with practical JavaScript snippets and step‑by‑step instructions.

Java Interview Crash Guide
Java Interview Crash Guide
Java Interview Crash Guide
Master Automated API Testing in Postman: From Basics to Advanced Scenarios

Background

This article targets readers who have mastered the basic usage of Postman, i.e., they understand API concepts and can simulate requests with Postman.

Current environment:

Windows 7 – 64

Postman version (free): Chrome App v5.5.3

UI differences between versions are minor.

To achieve automated API testing, we need to extend basic request simulation with three key questions:

How to determine whether an interface request succeeded?

How to perform batch and scheduled testing?

How to handle dependent interfaces (e.g., login required before placing an order)?

We will address these in three sections.

Interface Result Evaluation

Automation requires the tool or code to judge whether the result meets expectations. Two main approaches are:

Check if the returned code matches the expected value.

Check if the response body contains expected keywords.

Postman provides a Tests panel where JavaScript can be used for these checks.

Feature Area

The Tests feature uses JavaScript. No additional context or runtime setup is needed; you write the validation logic directly.

Postman also offers ready‑made code snippets in the SNIPPETS panel.

Script Related

Postman exposes several built‑in variables: responseCode: contains the status information (e.g., code). responseBody: the response data as a string. tests: a key‑value map that records test results shown in Test Results. key: a descriptive name for a test (e.g., "code 200"). value: a boolean where true means the test passed.

Additional useful variables include: responseTime: request duration. postman functions such as postman.getResponseHeader("") and postman.setGlobalVariable("variable_key", "variable_value").

Code Templates

Postman’s SNIPPETS provide templates for common checks:

// Status code is 200
tests["Status code is 200"] = responseCode.code === 200;
// Body contains a keyword
tests["Body matches string"] = responseBody.has("<your keyword>");
// Example: check for access_token
tests["has access_token"] = responseBody.has("access_token");
// Body equals expected string
tests["Body is correct"] = responseBody === "<expected content>";
// JSON value check
var jsonData = JSON.parse(responseBody);
tests["Your test name"] = jsonData.value === 100;
// Response time less than 200 ms
tests["Response time is less than 200ms"] = responseTime < 200;

These snippets cover most single‑request test scenarios.

Collection (Batch) Testing

To run multiple interfaces together, save them in a single Collection (similar to a folder).

For simplicity, we judge success by checking that the response code equals 200:

tests["Status code is 200"] = responseCode.code === 200;

Batch Execution

Click Run to open the runner window.

The runner shows options such as: Environment: switch the environment for the run. Iteration: number of times to repeat the collection. Delay: pause (ms) between iterations. Data File: upload a CSV or JSON file to supply variable data.

Variable Parameter Data

When the same request is executed repeatedly, the parameters remain static. To test different credentials, use variables.

Replace fixed values with {{username}} and {{password}}. Assign values in the Pre‑request Script panel:

// Set global variables
postman.setGlobalVariable("username", "test1");
postman.setGlobalVariable("password", "123456");

Alternatively, upload a CSV file:

username,password
test1,123456
test2,222222
test3,123456
test4,444444

Or a JSON file:

[
  {"username":"test1","password":"123456"},
  {"username":"test2","password":"222222"},
  {"username":"test3","password":"123456"},
  {"username":"test4","password":"444444"}
]

Pre‑request Script

Scripts in this panel run before the request, allowing you to set variables dynamically.

Multiple Environment Differentiation and Switching

Environments let you store different sets of variables (e.g., host URLs). You can create and edit them visually or via scripts:

// Set an environment variable
postman.setEnvironmentVariable("variable_key", "variable_value");

Use the variables in requests with {{host}} etc., and switch environments in the runner.

Solving Dependency Issues

When an interface depends on data from a previous call (e.g., a token from a login request), follow these steps:

Ensure the login request runs before the dependent request.

Extract the token in the login request’s Tests script and store it in an environment variable.

Reference the token in the dependent request.

Example Tests script for the login request:

if (responseCode.code === 200 && responseBody.has("access_token")) {
    tests["login"] = true;
    var jsonData = JSON.parse(responseBody);
    postman.setEnvironmentVariable("token", jsonData.result.access_token);
    postman.setNextRequest("Request3");
} else {
    tests["login"] = false;
}

In the dependent request, include the token (e.g., in the header) using {{token}}.

Running the collection yields the expected outcome: Request1 and Request3 pass, Request2 is skipped, and Request4 continues.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptAutomationAPI testingPostmandependency handlingEnvironment Variables
Java Interview Crash Guide
Written by

Java Interview Crash Guide

Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.