Duplicating and moving requests in Postman - Build an Automation Script
import fs from 'fs'; import { Collection } from 'postman-collection'; // Load the original collection JSON const sampleCollectionData = JSON.parse(fs.readFileSync('Sample_Collection.json', 'utf8')); const backupCollectionData = JSON.parse(fs.readFileSync('Backup_Collection.json', 'utf8')); const sampleCollection = new Collection(sampleCollectionData); const backupCollection = new Collection(backupCollectionData); // Find the request named 'Get User' const originalRequest = sampleCollection.items.one('Get User'); if (!originalRequest) { throw new Error("Original request 'Get User' not found in Sample Collection."); } // Duplicate the request const duplicatedRequest = originalRequest.clone(); duplicatedRequest.name = 'Get User Copy'; // Add duplicated request to Sample Collection sampleCollection.items.add(duplicatedRequest); // Assert duplicated request exists in Sample Collection if (!sampleCollection.items.one('Get User Copy')) { throw new Error("Duplicated request not found in Sample Collection after adding."); } // Remove duplicated request from Sample Collection sampleCollection.items.remove(duplicatedRequest); // Assert duplicated request removed from Sample Collection if (sampleCollection.items.one('Get User Copy')) { throw new Error("Duplicated request still exists in Sample Collection after removal."); } // Add duplicated request to Backup Collection backupCollection.items.add(duplicatedRequest); // Assert duplicated request exists in Backup Collection if (!backupCollection.items.one('Get User Copy')) { throw new Error("Duplicated request not found in Backup Collection after adding."); } // Save updated collections back to files fs.writeFileSync('Sample_Collection_Updated.json', JSON.stringify(sampleCollection.toJSON(), null, 2)); fs.writeFileSync('Backup_Collection_Updated.json', JSON.stringify(backupCollection.toJSON(), null, 2)); console.log('Request duplicated and moved successfully.');
This script uses the Postman Collection SDK to automate duplicating and moving a request between collections.
First, it loads the JSON files for the two collections: 'Sample Collection' and 'Backup Collection'.
It finds the original request named 'Get User' in the sample collection. If not found, it throws an error.
It clones this request and renames it to 'Get User Copy'.
The duplicated request is added to the sample collection, then verified to exist there.
Next, the duplicated request is removed from the sample collection and verified it no longer exists there.
Then, the duplicated request is added to the backup collection and verified to exist there.
Finally, the updated collections are saved back to JSON files.
This approach uses explicit checks after each step to ensure the duplication and move operations succeeded, following best practices for automation.
Now add data-driven testing to duplicate and move requests with three different request names.
Practice
Solution
Step 1: Understand duplicating requests
Duplicating means making a copy of an existing request to reuse its setup.Step 2: Identify the benefit
Reusing saves time because you don't have to create the request from scratch.Final Answer:
It saves time by reusing an existing request setup. -> Option BQuick Check:
Duplicating = Reuse setup [OK]
- Thinking it deletes the original request
- Confusing duplication with merging requests
- Assuming it exports the request
Solution
Step 1: Locate duplication method
In Postman, duplicating is done by right-clicking the request and choosing 'Duplicate'.Step 2: Eliminate wrong options
Dragging to trash deletes, clicking send sends request, renaming changes name only.Final Answer:
Right-click the request and select 'Duplicate'. -> Option DQuick Check:
Duplicate = Right-click + Duplicate [OK]
- Trying to duplicate by clicking send
- Dragging request to trash instead of duplicating
- Thinking renaming duplicates the request
Solution
Step 1: Understand duplication effect
Duplicating request B creates a copy but does not remove the original from the collection.Step 2: Understand moving the duplicate
Moving the duplicate to 'Tests' folder places only the copy there; original stays in the main collection.Final Answer:
Original collection: A, B, C; New folder: B (duplicate) -> Option AQuick Check:
Duplicate copy moved; original stays [OK]
- Assuming original is removed when duplicate is moved
- Thinking duplicate appears in both places
- Confusing original and duplicate requests
Solution
Step 1: Check Postman folder behavior
Postman allows moving requests by dragging them into folders.Step 2: Identify common mistake
Dragging outside any folder leaves the request in the original place, so it appears not moved.Final Answer:
You dragged the request outside any folder accidentally. -> Option AQuick Check:
Drag inside folder to move [OK]
- Confusing duplication with moving
- Assuming requests can be locked
- Believing Postman disallows moving requests
Solution
Step 1: Understand requirement
You want duplicates in the new folder and originals to remain in place.Step 2: Choose correct order
Duplicating first creates copies; moving duplicates places them in the folder without removing originals.Step 3: Eliminate wrong options
Moving first removes originals from collection; deleting loses data; renaming doesn't duplicate.Final Answer:
Duplicate the 3 requests, then move the duplicates into the new folder. -> Option CQuick Check:
Duplicate first, then move duplicates [OK]
- Moving first removes originals
- Deleting requests instead of duplicating
- Thinking renaming duplicates requests
Ready to prove it?
Pass the test and this pattern moves unknown
