Variable Scopes ⭐

Scope Set Using Typical Usage
Global pm.globals.set() Shared values across all workspaces (use sparingly)
Collection pm.collectionVariables.set() Values shared across an entire collection
Environment pm.environment.set() Base URLs, authentication tokens, environment-specific values
Local pm.variables.set() Temporary values available only during the current request or run

Referencing Variables

{{variableName}}

Variables can be used in:

  • URLs
  • Headers
  • Request Body
  • Authorization
  • Query Parameters

Variable Precedence

Local
   ↓
Data
   ↓
Environment
   ↓
Collection
   ↓
Global

The narrowest scope takes precedence.


Tests Tab — Core Assertions ⭐

Validate Status Code

pm.test("Status is 200", function () {
    pm.response.to.have.status(200);
});

Validate Response Time

pm.test("Under 2000ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(2000);
});

Validate Response Body

pm.test("ID is correct", function () {

    var jsonData = pm.response.json();

    pm.expect(jsonData.id).to.eql(101);

});

Validate Response Header

pm.test("Content-Type is JSON", function () {

    pm.response.to.have.header("Content-Type");

});

Validate Response Text

pm.test("Has success message", function () {

    pm.expect(pm.response.text()).to.include("success");

});

Assertion Reference

Assertion Purpose
pm.response.to.have.status(200) Validate status code
pm.response.to.have.header("Header") Verify header exists
pm.expect(x).to.eql(y) Deep equality
pm.expect(x).to.be.a("string") Type validation
pm.expect(array).to.have.lengthOf(n) Array size
pm.expect(x).to.be.null Null validation
pm.expect(x).to.not.be.empty Empty validation
pm.expect(x).to.be.above(n) Numeric comparison
pm.expect(text).to.include("value") Substring validation
pm.expect(x).to.match(/regex/) Regular expression validation

Chaining Requests ⭐

Step 1 — Extract and Store Values

pm.environment.set("token",
                   pm.response.json().token);

pm.environment.set("bookingId",
                   pm.response.json().bookingid);

Step 2 — Reuse Stored Values

Header

Advertisement
Cookie: token={{token}}

URL

/booking/{{bookingId}}

Typical End-to-End Flow

Login
   ↓
Extract Token
   ↓
Create Resource
   ↓
Extract Resource ID
   ↓
Retrieve Resource
   ↓
Update Resource
   ↓
Delete Resource

Getting & Parsing the Response

var jsonData = pm.response.json();

pm.response.text();

pm.response.code;

pm.response.responseTime;

pm.response.headers.get("Content-Type");

Working with Nested JSON

jsonData.user.address.city;

jsonData.users[0].name;

jsonData.users.length;

Pre-request Scripts

Generate Dynamic Values

pm.environment.set("timestamp", Date.now());

pm.environment.set(
    "randomEmail",
    "user" + Date.now() + "@test.com"
);

Built-in Dynamic Variables

  • {{$randomFirstName}}
  • {{$randomEmail}}
  • {{$guid}}
  • {{$timestamp}}

Collection Runner

Feature Purpose
Environment Select DEV, QA, or Production configuration
Iterations Execute the collection multiple times
Data File CSV or JSON driven execution
Delay Pause between requests
Run Order Executes requests from top to bottom

Data-Driven Execution

Provide a CSV or JSON file where column names match your Postman variables. Each row is executed as a separate iteration.


Newman (CI/CD) ⭐

Installation

npm install -g newman

npm install -g newman-reporter-htmlextra

Execute a Collection

newman run collection.json

Execute with Environment & HTML Report

newman run collection.json \
-e qa-environment.json \
-r htmlextra \
--reporter-htmlextra-export report.html

Data-Driven Execution

newman run collection.json -d testdata.csv

Multiple Iterations

newman run collection.json -n 5

Newman in Jenkins

Typical workflow:

Execute Newman
        ↓
Collection Runs
        ↓
Build Passes or Fails
        ↓
Archive HTML Report

Go Deeper

Continue learning with: