Folder hierarchy in Postman - Build an Automation Script
const collection = pm.collectionVariables.toObject();
// Function to find folder by name
function findFolderByName(folders, name) {
return folders.find(folder => folder.name === name);
}
// Access folders from collection
const folders = pm.collection.folders.toObject();
// Verify 'User APIs' folder exists
const userApisFolder = findFolderByName(folders, 'User APIs');
pm.test('User APIs folder exists', () => {
pm.expect(userApisFolder).to.not.be.undefined;
});
// Verify subfolders
const subfolders = userApisFolder ? userApisFolder.folders : [];
const authFolder = findFolderByName(subfolders, 'Authentication');
const profileFolder = findFolderByName(subfolders, 'Profile');
pm.test('Authentication subfolder exists', () => {
pm.expect(authFolder).to.not.be.undefined;
});
pm.test('Profile subfolder exists', () => {
pm.expect(profileFolder).to.not.be.undefined;
});
// Verify requests in Authentication folder
const authRequests = authFolder ? authFolder.requests : [];
const loginStatusRequest = authRequests.find(req => req.name === 'Login Status');
pm.test('Login Status request exists in Authentication folder', () => {
pm.expect(loginStatusRequest).to.not.be.undefined;
pm.expect(loginStatusRequest.request.url.toString()).to.eql('https://api.example.com/login/status');
});
// Verify requests in Profile folder
const profileRequests = profileFolder ? profileFolder.requests : [];
const getProfileRequest = profileRequests.find(req => req.name === 'Get Profile');
pm.test('Get Profile request exists in Profile folder', () => {
pm.expect(getProfileRequest).to.not.be.undefined;
pm.expect(getProfileRequest.request.url.toString()).to.eql('https://api.example.com/user/profile');
});This script uses Postman's pm API to check the folder hierarchy and requests inside the collection.
First, it finds the main folder 'User APIs'. Then it checks for the two subfolders 'Authentication' and 'Profile'.
For each subfolder, it verifies that the expected requests exist and that their URLs match the expected values.
Assertions use pm.expect with clear messages to confirm the structure and data are correct.
This approach avoids hardcoding folder IDs by searching folders by name, making the test more maintainable.
Now add data-driven testing to verify folder hierarchy and requests for 3 different collections with different folder names and requests.
Practice
folders in Postman collections?Solution
Step 1: Understand folder usage in Postman
Folders group related API requests inside a collection to keep things organized.Step 2: Identify the correct purpose
Folders help manage and run related tests easily by grouping requests logically.Final Answer:
To organize API requests into groups for better management -> Option DQuick Check:
Folder purpose = Organize requests [OK]
- Thinking folders are for coding API logic
- Confusing folders with environment settings
- Assuming folders change server URLs
Solution
Step 1: Recall Postman UI actions
To create a folder, you right-click the collection and choose 'Add Folder'.Step 2: Eliminate incorrect options
Options B, C, and D do not create folders; they relate to environments, editing URLs, or invalid drag actions.Final Answer:
Right-click the collection and select 'Add Folder' -> Option AQuick Check:
Create folder = Right-click collection + Add Folder [OK]
- Choosing environment creation instead of folder
- Trying to create folder by editing URLs
- Dragging requests outside collections
Collection
├─ Folder A
│ ├─ Request 1
│ └─ Request 2
└─ Folder B
└─ Request 3
If you run Folder A, which requests will execute?Solution
Step 1: Understand folder execution scope
Running a folder executes only the requests inside that folder, not others.Step 2: Identify requests inside Folder A
Folder A contains Request 1 and Request 2, so only these run.Final Answer:
Request 1 and Request 2 -> Option AQuick Check:
Run folder = only its requests [OK]
- Assuming all collection requests run
- Including requests from other folders
- Thinking no requests run when folder runs
Solution
Step 1: Analyze folder content
If a folder runs but no requests execute, it likely has no requests inside.Step 2: Check other options
Collection deletion or Postman offline would cause errors, not empty runs. Not saving collection would prevent folder creation.Final Answer:
The folder is empty and has no requests inside -> Option CQuick Check:
Empty folder = no requests run [OK]
- Assuming collection deletion causes empty folder run
- Blaming offline status without error
- Forgetting to add requests to folder
Solution
Step 1: Understand grouping for selective runs
Folders group related requests so you can run them together easily.Step 2: Evaluate other options
Multiple collections for each request is inefficient; no folders means no grouping; environment variables do not filter requests.Final Answer:
Create a folder inside the collection and add those requests to it -> Option BQuick Check:
Group requests in folder for selective runs [OK]
- Creating too many collections for single requests
- Not using folders and running all requests
- Misusing environment variables for request grouping
Ready to prove it?
Pass the test and this pattern moves unknown
