API Document L
API Document L
Table of Contents
1. Introduction to Postman
Key Benefits
Initial Configuration
Key Components
● Collections pane
● Request builder
● Response viewer
● Environment selector
● Console
● Test scripts area
Collection Structure
my-api-tests/
├── auth/
│ ├── login
│ └── logout
├── users/
│ ├── create
│ ├── read
│ ├── update
│ └── delete
└── products/
├── list
└── search
Best Practices for Organization
// Schema validation
const schema = {
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"email": { "type": "string" }
},
"required": ["id", "name", "email"]
};
pm.test("Schema validation", function () {
pm.response.to.have.jsonSchema(schema);
});
// Data validation
pm.test("User data is correct", function () {
const responseData = pm.response.json();
pm.expect(responseData.name).to.eql("John Doe");
pm.expect(responseData.email).to.match(/@.*\./);
});
6. Automation Fundamentals
Pre-request Scripts
// Setting dynamic variables
pm.environment.set("timestamp", Date.now());
Newman CLI
8. CI/CD Integration
Jenkins Integration
pipeline {
agent any
stages {
stage('API Tests') {
steps {
sh 'newman run collection.json -e env.json'
}
}
}
}
GitHub Actions
name: API Tests
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Newman
run: npm install -g newman
- name: Run API Tests
run: newman run collection.json -e env.json
Code Organization
Performance Optimization
generateRandomString: function(length) {
return Array(length)
.fill('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')
.map(x => x[Math.floor(Math.random() * x.length)])
.join('');
},
validateStatusCode: function(expectedStatus) {
pm.test(`Status code is ${expectedStatus}`, () => {
pm.response.to.have.status(expectedStatus);
});
}
};
pm.environment.set(
"performanceMetrics",
JSON.stringify(performanceMetrics)
);
});
requiredHeaders.forEach(header => {
pm.response.to.have.header(header);
});
});
Authentication Testing
Workspace Management
Metrics Collection
// Test pagination
pm.expect(response.pagination).to.deep.include({
currentPage: Number(pm.request.url.query.get('page')),
itemsPerPage: 20,
totalItems: pm.expect.any(Number)
});
});
pm.sendRequest({
url: pm.environment.get("base_url") + "/cart",
method: "POST",
header: {
"Content-Type": "application/json",
"Authorization": pm.environment.get("token")
},
body: {
mode: "raw",
raw: JSON.stringify(item)
}
}, (err, response) => {
pm.test("Item added to cart", () => {
pm.expect(response.code).to.equal(200);
const cartItem = response.json().items.find(
i => i.productId === item.productId
);
pm.expect(cartItem).to.deep.include(item);
});
});
}
};
// Transaction processing
const bankingTests = {
validateTransaction: () => {
const transaction = pm.response.json();
// Amount validation
pm.expect(transaction.amount).to.be.above(0);
pm.expect(transaction.amount).to.be.a('number');
// Balance check
const newBalance = transaction.resultingBalance;
const oldBalance = pm.environment.get("previousBalance");
const expectedBalance = transaction.type === "CREDIT"
? oldBalance + transaction.amount
: oldBalance - transaction.amount;
pm.expect(newBalance).to.equal(expectedBalance);
// Compliance checks
pm.test("Transaction compliance", () => {
if (transaction.amount > 10000) {
pm.expect(transaction.complianceChecks).to.include({
largeTransactionReported: true,
customerVerified: true
});
}
});
}
};
Data-Driven Testing
testData.userScenarios.forEach(scenario => {
pm.test(scenario.description, () => {
// Send request with scenario data
pm.sendRequest({
url: pm.environment.get("base_url") + "/users",
method: "POST",
header: { "Content-Type": "application/json" },
body: {
mode: "raw",
raw: JSON.stringify(scenario.input)
}
}, (err, response) => {
pm.expect(response.code).to.equal(scenario.expectedStatus);
pm.expect(response.json()).to.deep.include(
scenario.expectedResponse
);
});
});
});
Workflow Automation
sequenceDiagram
participant C as Client
participant P as Postman
participant A as API
participant D as Database
Conclusion
This guide covers the essential aspects of using Postman for API automation testing. By
following these practices and guidelines, you'll be able to create robust, maintainable, and
efficient API tests.
Follow https://www.linkedin.com/in/guneetsinghbali/