Importing and exporting collections in Postman - Build an Automation Script
import fs from 'fs'; import { execSync } from 'child_process'; // Paths for collection files const importCollectionPath = './test_collections/sample_collection.json'; const exportCollectionPath = './test_collections/exported_collection.json'; // Step 1: Import collection using Newman (simulate by running a simple run) try { execSync(`newman run ${importCollectionPath} --disable-unicode`); console.log('Collection imported and run successfully'); } catch (error) { throw new Error('Failed to import or run collection: ' + error.message); } // Step 2: Export collection - Newman does not support export directly, so simulate export by copying file try { fs.copyFileSync(importCollectionPath, exportCollectionPath); console.log('Collection exported successfully'); } catch (error) { throw new Error('Failed to export collection: ' + error.message); } // Step 3: Validate exported collection file exists and is valid JSON if (!fs.existsSync(exportCollectionPath)) { throw new Error('Exported collection file does not exist'); } const exportedContent = fs.readFileSync(exportCollectionPath, 'utf-8'); try { const json = JSON.parse(exportedContent); if (!json.info || !json.item) { throw new Error('Exported collection JSON missing required fields'); } console.log('Exported collection JSON is valid'); } catch (error) { throw new Error('Exported collection JSON is invalid: ' + error.message); } // Step 4: Verify imported collection contains expected requests // For simplicity, check if collection has at least one request const importedContent = fs.readFileSync(importCollectionPath, 'utf-8'); const importedJson = JSON.parse(importedContent); if (!importedJson.item || importedJson.item.length === 0) { throw new Error('Imported collection has no requests'); } console.log('Imported collection contains requests');
This script uses Node.js to automate the import and export verification of a Postman collection.
First, it runs the collection using Newman CLI to simulate import and execution, ensuring no errors occur.
Since Newman does not support exporting collections, the script simulates export by copying the original collection file to a new location.
Then, it checks if the exported file exists and parses it as JSON to verify it has the required structure.
Finally, it verifies the imported collection contains at least one request to confirm it is valid.
This approach uses file system operations and Newman CLI commands to automate the manual test steps effectively.
Now add data-driven testing with 3 different Postman collection files to import and export
Practice
Solution
Step 1: Understand importing collections
Importing collections means bringing existing API tests into Postman so you can use or modify them.Step 2: Differentiate from exporting and creating
Exporting saves tests out of Postman, and creating new APIs is different from importing.Final Answer:
To add existing API tests into Postman for use -> Option AQuick Check:
Import = Add existing tests [OK]
- Confusing import with export
- Thinking import deletes tests
- Assuming import creates new APIs
Solution
Step 1: Identify export file format
Postman exports collections as JSON files, which store structured data about API tests.Step 2: Exclude other formats
XML, TXT, and CSV are not used by Postman for exporting collections.Final Answer:
.json -> Option CQuick Check:
Exported collections use JSON files [OK]
- Choosing XML or CSV instead of JSON
- Confusing export format with import options
- Assuming text files are used
Solution
Step 1: Check file integrity
If the imported collection shows no requests, the JSON file might be empty or corrupted, causing no data to load.Step 2: Verify import support and format
Postman supports JSON imports, and restarting is not required. Exporting in XML is not possible, so that is invalid.Final Answer:
The JSON file was empty or corrupted -> Option BQuick Check:
Empty/corrupt JSON causes no requests on import [OK]
- Thinking Postman can't import JSON
- Assuming restart is needed after import
- Believing XML export is possible
Solution
Step 1: Verify collection selection
The export button is disabled if no collection is selected. Selecting a collection enables export.Step 2: Exclude unnecessary actions
Restarting, changing to XML (unsupported), or reinstalling are not needed to enable export.Final Answer:
Ensure a collection is selected in Postman -> Option AQuick Check:
Export button needs a selected collection [OK]
- Trying unsupported XML format
- Restarting unnecessarily
- Reinstalling Postman without checking selection
Solution
Step 1: Export collection as JSON
Postman exports collections only as JSON files for sharing or backup.Step 2: Teammate imports JSON file
The teammate imports the JSON file into their Postman app to get the same tests.Final Answer:
Export the collection as JSON, send the file, teammate imports JSON in Postman -> Option DQuick Check:
Export and import use JSON files [OK]
- Using CSV or XML formats
- Thinking URL copy imports collections
- Skipping export step
Ready to prove it?
Pass the test and this pattern moves unknown
