0
0
Postmantesting~15 mins

Importing and exporting collections in Postman - Build an Automation Script

Choose your learning style9 modes available
Import and Export Postman Collection
Preconditions (2)
Step 1: Click on the 'Import' button in the Postman app
Step 2: Select the collection JSON file from the local system
Step 3: Confirm the import and wait for the collection to appear in the sidebar
Step 4: Select the imported collection
Step 5: Click on the three dots next to the collection name to open the menu
Step 6: Choose 'Export' option
Step 7: Select the export format (e.g., Collection v2.1)
Step 8: Save the exported collection file to the local system
✅ Expected Result: The collection is successfully imported and visible in Postman. The exported collection file is saved correctly and matches the imported collection content.
Automation Requirements - Postman Newman CLI
Assertions Needed:
Verify collection is imported without errors
Verify collection contains expected requests
Verify exported collection file exists and is valid JSON
Best Practices:
Use Newman CLI commands for import and export simulation
Validate JSON schema of collection files
Use file system checks for exported file existence
Keep test data files organized and reusable
Automated Solution
Postman
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.

Common Mistakes - 3 Pitfalls
Using Newman CLI without checking for errors
Hardcoding file paths without verifying existence
{'mistake': 'Not validating JSON structure of imported/exported collections', 'why_bad': 'Invalid or corrupted collections may pass unnoticed, causing failures later.', 'correct_approach': "Parse JSON and verify required fields like 'info' and 'item' are present."}
Bonus Challenge

Now add data-driven testing with 3 different Postman collection files to import and export

Show Hint