Automation QA Testing Course Content

Run Postman Collection in Command Prompt

 Running a Postman collection from the command prompt can be accomplished using Newman, which is a command-line collection runner for Postman. Here's a step-by-step guide on how to do it:

Prerequisites

  1. Install Node.js: Ensure Node.js is installed on your machine. You can download it from Node.js official website. https://nodejs.org/en

  2. Install Newman: Newman is a command-line collection runner for Postman. Install it globally using npm:

    sh
    npm install -g newman

Export Your Postman Collection

  1. Open Postman: Launch Postman and open the collection you want to run.

  2. Export Collection:

    • Click on the ellipsis (...) next to the collection name.
    • Select "Export".
    • Choose "Collection v2.1" and click "Export".
    • Save the exported JSON file to your desired location.

Run the Collection Using Newman

  1. Open Command Prompt: Open the command prompt or terminal.

  2. Run the Collection: Use the following command to run the collection with Newman:

    sh
    newman run path/to/your/collection.json

Additional Options

Newman provides various options to customize the execution of your collection:

  1. Specify Environment Variables: If your collection requires environment variables, you can export your Postman environment and include it in the command:

    sh
    newman run path/to/your/collection.json -e path/to/your/environment.json
  2. Data File: If you want to run the collection with a data file (for data-driven testing), use the -d flag:

    sh
    newman run path/to/your/collection.json -d path/to/your/datafile.json
  3. Reporters: Generate different types of reports (e.g., HTML, JSON) using the -r flag:

    sh
    newman run path/to/your/collection.json -r html
  4. Iteration Count: Specify the number of iterations to run the collection:

    sh
    newman run path/to/your/collection.json -n 10

Example Command

Here’s an example command that includes the collection, environment variables, data file, and generates an HTML report:

sh
newman run path/to/your/collection.json -e path/to/your/environment.json -d path/to/your/datafile.json -r html

Summary

By using Newman, you can easily run your Postman collections from the command prompt, enabling automated testing and integration into CI/CD pipelines. The flexibility provided by Newman’s options allows you to customize the execution to fit your specific needs.

==========================================================

when you run the command 'newman' in commandprompt The error 'newman' is not recognized as an internal or external command, operable program or batch file


Solution for above error is as below :


The error 'newman' is not recognized as an internal or external command, operable program or batch file indicates that the system cannot find the newman command. This typically happens because Newman is not installed globally or the global npm bin directory is not in your system's PATH.

Here are the steps to resolve this issue:

Step 1: Verify Node.js and npm Installation

Ensure Node.js and npm are installed and properly set up on your system. You can verify this by running the following commands in the command prompt:

sh
node -v npm -v

If these commands do not return a version number, you need to install Node.js from the official website.

Step 2: Install Newman Globally

Install Newman globally using npm:

sh
npm install -g newman

Step 3: Verify Newman Installation

After installing, verify that Newman is installed correctly by running:

sh
newman -v

This should return the version number of Newman.

Step 4: Update System PATH

If you still encounter the issue, it might be because the npm global bin directory is not in your system's PATH. To add it:

  1. Locate npm global bin directory: Run the following command to find out where npm installs global packages:

    sh
    npm config get prefix

    This will return a directory path (e.g., C:\Users\YourUsername\AppData\Roaming\npm).

  2. Add the npm global bin directory to PATH:

    • Right-click on This PC or Computer on your desktop or in File Explorer, and select Properties.
    • Click on Advanced system settings.
    • Click on the Environment Variables button.
    • In the System variables section, find the Path variable, select it, and click Edit.
    • Add the npm global bin directory (e.g., C:\Users\YourUsername\AppData\Roaming\npm) to the list. Each entry should be separated by a semicolon (;).
    • Click OK to close all dialog boxes.
  3. Restart Command Prompt: After updating the PATH, restart your command prompt to apply the changes.

Step 5: Run Newman Command

Navigate to your project directory and run the Newman command again:

sh
cd C:\javaprograms\APITestingINfo\NewManTests newman run PostmanAPIMay162024.postman_collection.json -e pmqaMay1624.postman_environment.json

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.