postmanframework

Node Application to run the Postman Collection and generate Newman Reports

View on GitHub

Quick API Automation Framework using Postman and Newman

Node Application to run the Postman Collection and respective Environments and generate Newman Reports

Build Status

Configuration

  1. Postman - Version 6.1.2
  2. Collection - Version 2.1
  3. Newman - Version 3.1
  4. Node - Version 10.1.0
  5. NPM - Version 5.6.0
  6. Reports - Default Newman Report and Custom HBS Template

Contents

  1. Pre-Requisites
    1. Install NodeJS and NPM
    2. Newman
    3. Postman Collection
    4. Assertions - Postman Test Scripts
    5. Postman Environments
    6. Configure app.js and package.json
  2. Command Line Execution of Collections
    1. Run the Postman Collection
    2. Run Multiple Postman Collections
    3. Run the Postman Collection with Environment
    4. Run the Collection with Environment and Generate Newman Report
    5. Run the Collection with Environment and Generate Custom Report
    6. Report Configuration
    7. Command Line Options
  3. Node Application Execution of Collections
    1. Run the Collection as Node app

Pre Requisites

To run Newman, ensure that you have NodeJS >= v4. A copy of the NodeJS installable can be downloaded from https://nodejs.org/en/download/package-manager.

Install NodeJS and NPM

Windows

http://blog.teamtreehouse.com/install-node-js-npm-windows

MacOS

http://blog.teamtreehouse.com/install-node-js-npm-mac

Newman

Open you Node Terminal and install globally

$ npm install newman --global;

Postman Collection

Assertion Postman Test Scripts

Postman Environments

Configure app.js and package.json


Command line Execution of Collections

Run the Postman Collection

$ newman run <collection-file-source>

Run Multiple Postman Collections

$ for collection in ./PostmanCollections/*; do newman run "$collection" --environment ./PostmanEnvironments/Test.postman_environment.json' -r cli; done

Run the Postman Collection with Environment

$ newman run <collection-file-source> -e <environment-file-source>

Run the Postman Collection with Environment and Generate Newman Report

$ newman run <collection-file-source> -e <environment-file-source> -r report.html

Run the Postman Collection with Environment and Generate Custom Report

$ newman run <collection-file-source> -e <environment-file-source> --reporters cli,html --reporter-html-template <path to the template> --reporter-html-exporter <path to export>

Report Configuration

Reporters provide information about the current collection run in a format that is easy to both: disseminate and assimilate.

Note: Sample collection reports have been provided in examples/reports.

CLI reporter options

These options are supported by the CLI reporter, use them with appropriate argument switch prefix. For example, the option no-summary can be passed as --reporter-no-summary or --reporter-cli-no-summary.

CLI reporter is enabled by default, you do not need to specifically provide the same as part of --reporters option. However, enabling one or more of the other reporters will result in no CLI output. Explicitly enable the CLI option in such a scenario.

CLI Option Description
--reporter-cli-silent The CLI reporter is internally disabled and you see no output to terminal.
--reporter-cli-no-summary The statistical summary table is not shown.
--reporter-cli-no-failures This prevents the run failures from being separately printed.
--reporter-cli-no-assertions This turns off the output for request-wise assertions as they happen.
--reporter-cli-no-success-assertions This turns off the output for successful assertions as they happen.
--reporter-cli-no-console This turns off the output of console.log (and other console calls) from collection’s scripts.
JSON reporter options

The built-in JSON reporter is useful in producing a comprehensive output of the run summary. It takes the path to the file where to write the file. The content of this file is exactly same as the summary parameter sent to the callback when Newman is used as a library.

To enable JSON reporter, provide --reporters json as a CLI option.

CLI Option Description
--reporter-json-export <path> Specify a path where the output JSON file will be written to disk. If not specified, the file will be written to newman/ in the current working directory.

HTML reporter options

The built-in HTML reporter produces and HTML output file outlining the summary and report of the Newman run. To enable the HTML reporter, provide --reporters html as a CLI option.

CLI Option Description
--reporter-html-export <path> Specify a path where the output HTML file will be written to disk. If not specified, the file will be written to newman/ in the current working directory.
--reporter-html-template <path> Specify a path to the custom template which will be used to render the HTML report. This option depends on --reporter html and --reporter-html-export being present in the run command. If this option is not specified, the default template is used

Custom templates (currently handlebars only) can be passed to the HTML reporter via --reporter-html-template <path> with --reporters html and --reporter-html-export. The default template is used in all other cases.

JUNIT/XML reporter options

Newman can output a summary of the collection run to a JUnit compatible XML file. To enable the JUNIT reporter, provide --reporters junit as a CLI option.

CLI Option Description
--reporter-junit-export <path> Specify a path where the output XML file will be written to disk. If not specified, the file will be written to newman/ in the current working directory.

Older command line options are supported, but are deprecated in favour of the newer v3 options and will soon be discontinued. For documentation on the older command options, refer to README.md for Newman v2.X.

Creating and using custom reporters

Newman also supports custom reporters, provided that the reporter works with Newman’s event sequence. Working examples on how Newman reporters work can be found in lib/reporters. For instance, to use the Newman teamcity reporter:

Command Line Options

(Source: https://www.npmjs.com/package/newman)

newman run <collection-file-source> [options]

newman.run({ collection: ‘/path/to/collection.json’, reporters: [‘cli’, ‘teamcity’] }, process.exit);


---

## Node Application Execution of Collections

### Configure the JavaScript and the package

Create a new js file(app.js) and you can pass your options as below

```javascript
var newman = require('newman'); 
// require newman in your project

// call newman.run to pass `options` object and wait for callback
newman.run({
    collection: require('./PostmanCollection/Sample.postman_collection.json'),
    environment: require('./PostmanEnvironment/Test.postman_environment.json'),
    reporters: ['html','cli'],
    reporter : { html : { export : './report/htmlReport.html'}},
    insecure: true, // allow self-signed certs, required in postman too
    timeout: 180000  // set time out
}).on('start', function (err, args) { // on start of run, log to console
    console.log('running a collection...');
}).on('done', function (err, summary) {
    if (err || summary.error) {
        console.error('collection run encountered an error.');
    }
    else {
        console.log('collection run completed.');
    }
});

Create a package file and you can configure the dependencies as below

{
  "name": "SampleCollectionAssertionsAndNewmanReport",
  "version": "1.0.0",
  "description": "Services",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {},
  "author": "",
  "license": "ISC",
  "dependencies": {
    "newman": "^3.9.3"
  }
}

Run the Collection as Node app

To run the Collection as Node application. Open the Node terminal and run the app.js

$ npm install && node app.js