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.