Newman

Newman

Newman is a CLI tool for running Postman collections.

Setup

  • You need NodeJS (which includes the npm package manager) or NVM (Node Version Manager).

    1
    npm install -g newman
  • Check it was properly installed

    1
    newman -v

Run it

  • You may check it works properly by exporting a Postman Collection as a json file from the Postman App.

    1
    newman run examples/sample-collection.json
  • You may also run a remote json file.

    1
    newman run https://www.getpostman.com/collections/631643-f695cab7-6878-eb55-7943-ad88e1ccfd65-JsLv

Hints

HTML Reporters

  • Basic reporter

    1
    2
    npm install -g newman-reporter-html
    newman run examples/demo-collection.json -r html
  • Extended reporter

    1
    2
    npm install -g newman-reporter-htmlextra
    newman run examples/demo-collection.json -r htmlextra

Pre-request scripts

  • Pre-processing

    • setting variable values
    • parameters
    • headers
    • body data
    • pre-request scripts for debugging code (e.g. logging output)
      1
      pm.collectionVariables.set('methodName', 'myMethodName')
  • Learn with example:

    • Calculate hashed version of ${developerId}${methodName}${FORMAT}${authKey}${timeStamp}

    • Python solution (and manually paste on postman)

      1
      2
      from hashlib import md5
      md5(string_to_hash)
    • Postman solution

      • pre-request script tab on request

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        const moment = require("moment");
        const timeStamp = moment.utc.().format('YYYYMMDDHHmmss');

        const devId = pm.environmentVariables.get('devId');

        const authKey = pm.environmentVariables.get('authKey');

        const methodName = pm.environmentVariables.get('methodName');

        // use cryptoJS
        const signature = CryptoJS.MD5(`${devId}${methodName}${authKey}${timeStamp}`).toString();

        // set the new environment variable
        postman.setEnvironmentVariable('signature', signature);
        postman.setEnvironmentVariable('timeStamp', timeStamp);
      • On call

        1
        {{URL}}/{{methodName}}{{FORMAT}}/{{devID}}/{{signature}}/{{session}}/{{timeStamp}}/{{LANGUAGE_CODE}}

Integrations

Newman on Jenkins

Obtain graphical reports

  1. Create job/project “Postman collection”

  2. Go to configure/build

  3. You may set the value of execute batch command:

    1
    2
    3
    # there should be no spaces on the reporters arguments
    newman run https://www.getpostman.com/collections/631643-f695cab7-6878-eb55-7943-ad88e1ccfd65-JsLv \
    --reporters cli,junit --reporter-junit-export “newman/myreport.xml”
  4. When the project is built, you may go to Workspace/newman and check the xml report.

Publish the report

  1. Go to Configure
  2. Go to post-build actions
  3. Select publish Junit test result report
  4. Enter the path of the file in Test Report XMLs and save the changes
  5. Click on Build Now to build the project again
  6. In the Build history section, click on the date and time to open up the build results
  7. Click on Test Result
  1. Build the project a couple of times to set a general trend as sometimes Jenkins does not show the report with one build.
  2. Refresh the page.
  3. You will see the generalized report on the dashboard as Test Result Trend.