0
0
Postmantesting~15 mins

Newman installation in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify Newman installation and version
Preconditions (2)
Step 1: Open the command line interface
Step 2: Run the command 'npm install -g newman' to install Newman globally
Step 3: Wait for the installation to complete
Step 4: Run the command 'newman -v' to check the installed Newman version
✅ Expected Result: Newman installs successfully without errors and the version number is displayed correctly
Automation Requirements - Node.js with child_process module
Assertions Needed:
Verify that 'npm install -g newman' command completes without errors
Verify that 'newman -v' command outputs a valid version string
Best Practices:
Use child_process.exec to run CLI commands asynchronously
Check for errors in command execution
Validate output format with regex for version number
Use clear and descriptive assertion messages
Automated Solution
Postman
import { exec } from 'child_process';

function runCommand(command) {
  return new Promise((resolve, reject) => {
    exec(command, (error, stdout, stderr) => {
      if (error) {
        reject(new Error(`Command failed: ${command}\n${stderr}`));
      } else {
        resolve(stdout.trim());
      }
    });
  });
}

(async () => {
  try {
    console.log('Installing Newman globally...');
    const installOutput = await runCommand('npm install -g newman');
    console.log('Installation output:', installOutput);

    console.log('Checking Newman version...');
    const versionOutput = await runCommand('newman -v');
    console.log('Newman version:', versionOutput);

    const versionRegex = /^\d+\.\d+\.\d+$/;
    if (!versionRegex.test(versionOutput)) {
      throw new Error('Newman version output is not a valid version string');
    }

    console.log('Test passed: Newman installed and version verified successfully.');
  } catch (error) {
    console.error('Test failed:', error.message);
    process.exit(1);
  }
})();

This script uses Node.js child_process.exec to run shell commands asynchronously.

First, it runs npm install -g newman to install Newman globally. It waits for the command to finish and checks for errors.

Next, it runs newman -v to get the installed version. It verifies the output matches a version pattern like 1.2.3.

If any command fails or the version output is invalid, the script throws an error and exits with failure.

This approach ensures the installation and version check are automated and validated properly.

Common Mistakes - 3 Pitfalls
Not checking for errors after running the install command
Assuming the version output is always valid without validation
Running commands synchronously blocking the event loop
Bonus Challenge

Now add data-driven testing to install Newman with different npm versions and verify installation each time

Show Hint