Skip to content

Newman Run Events

Udit Vasu edited this page Mar 14, 2021 · 2 revisions

Newman's run method triggers a bunch of events during the run. These events allow building complex workflows in a collection execution lifecycle.

Adding Event Listeners

newman.run returns an event emitter instance where you can attach listeners using the .on or .once methods.

Example:

const newman = require('newman');

newman
  .run({ collection: require('/path/to/collection.json') })
  .on('done', (err, args) => { /* result: args.run */ })
  .on('console', (err, args) => { /* logs: args.messages */ })
  //.on('<event-name>', (err, args) => { /* event handler */ })

Every event handler (callback function) is called with two arguments:

  1. err - The first argument is an Error instance, if an error occurred, otherwise it's always null
  2. args - The second argument is an Object having event-specific properties

Events

The list below describes the events and their args properties in detail.

start

Triggered once at the start of a collection run

Property Description
cursor Cursor Object

beforeIteration

Before an iteration commences

Property Description
cursor Cursor Object

beforeItem

Before an item execution begins (the set of prerequest->request->test)

Property Description
cursor Cursor Object
item Item instance

beforePrerequest

Before pre-request script execution starts

Property Description
cursor Cursor Object
events An array of Event
item Item instance

prerequest

After pre-request script execution completes

Property Description
cursor Cursor Object
executions An array of executions object
item Item instance

beforeRequest

Before an HTTP request is sent

Property Description
cursor Cursor Object
request Request instance
item Item instance

request

After the response of the request is received

Property Description
cursor Cursor Object
response Response instance
request Request instance
item Item instance
cookies CookieList instance

beforeTest

Before test script execution starts

Property Description
cursor Cursor Object
events An array of Event
item Item instance

test

After test script execution completes

Property Description
cursor Cursor Object
executions An array of executions object
item Item instance

beforeScript

Before any script (of type test or prerequest) is executed

Property Description
cursor Cursor Object
script Script instance
event Event instance
item Item instance

script

After any script (of type test or prerequest) is executed

Property Description
cursor Cursor Object
execution Executions object
script Script instance
event Event instance
item Item instance

item

When an item (the whole set of prerequest->request->test) completes

Property Description
cursor Cursor Object
item Item instance

iteration

After an iteration completes

Property Description
cursor Cursor Object

assertion

When test assertion is done within the scripts

Property Description
cursor Cursor Object
assertion Assertion name or message
skipped Is the assertion skipped
error Assertion error on failure
item Item instance

console

When a console function is called within the scripts

Property Description
cursor Cursor Object
level Console log level type
messages Array of messages

exception

When an asynchronous error happens during script execution

Property Description
cursor Cursor Object
error Error Object

beforeDone

Before the completion of the run

Property Description
cursor Cursor Object
summary Summary Object

done

After the completion of the run, with or without errors

Property Description
collection Collection used in the run
environment VariableScope instance with the environment variables used in the run
globals VariableScope instance with the global variables used in the run
run Run summary object