0
0
Postmantesting~15 mins

Folder hierarchy in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify folder hierarchy creation and request organization in Postman
Preconditions (2)
Step 1: Click on 'New' button and select 'Folder' to create a new folder named 'User APIs'
Step 2: Inside 'User APIs' folder, create two subfolders named 'Authentication' and 'Profile'
Step 3: Add a GET request named 'Login Status' inside 'Authentication' folder with URL 'https://api.example.com/login/status'
Step 4: Add a GET request named 'Get Profile' inside 'Profile' folder with URL 'https://api.example.com/user/profile'
Step 5: Verify that the folder 'User APIs' contains the two subfolders 'Authentication' and 'Profile'
Step 6: Verify that 'Authentication' folder contains the 'Login Status' request
Step 7: Verify that 'Profile' folder contains the 'Get Profile' request
✅ Expected Result: Folder 'User APIs' is created with subfolders 'Authentication' and 'Profile'. Each subfolder contains the correct requests as named and with correct URLs.
Automation Requirements - Postman Collection Runner with pm scripting
Assertions Needed:
Verify folder structure exists as created
Verify requests exist in correct folders
Verify request URLs are correct
Best Practices:
Use Postman pm API to access folder and request details
Use descriptive assertion messages
Organize tests logically by folder
Avoid hardcoding folder IDs; use folder names
Automated Solution
Postman
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.

Common Mistakes - 3 Pitfalls
Hardcoding folder IDs instead of using folder names
Not verifying the request URLs, only checking request names
Using pm.test inside loops without proper closure
Bonus Challenge

Now add data-driven testing to verify folder hierarchy and requests for 3 different collections with different folder names and requests.

Show Hint