run = function (options, callback) {
// validate all options. it is to be noted that `options` parameter is option and is polymorphic
(!callback && _.isFunction(options)) && (
(callback = options),
(options = {})
);
!_.isFunction(callback) && (callback = _.noop);
var emitter = new EventEmitter(), // @todo: create a new inherited constructor
runner = new runtime.Runner();
// get the configuration from various sources
getOptions(options, function (err, options) {
if (err) {
return callback(err);
}
// ensure that the collection option is present before starting a run
if (!_.isObject(options.collection)) {
return callback(new Error('newman: expecting a collection to run'));
}
// store summary object and other relevant information inside the emitter
emitter.summary = new RunSummary(emitter, options);
// to store the exported content from reporters
emitter.exports = [];
// now start the run!
runner.run(options.collection, {
stopOnFailure: options.bail, // LOL, you just got trolled ¯\_(ツ)_/¯
abortOnFailure: options.abortOnFailure, // used in integration tests, to be considered for a future release
iterationCount: options.iterationCount,
environment: options.environment,
globals: options.globals,
entrypoint: options.folder,
data: options.iterationData,
delay: {
item: options.delayRequest
},
// todo: add support for more types of timeouts, currently only request is supported
timeout: options.timeoutRequest ? { request: options.timeoutRequest } : undefined,
fileResolver: fs,
requester: {
cookieJar: request.jar(),
followRedirects: _.has(options, 'ignoreRedirects') ? !options.ignoreRedirects : undefined,
strictSSL: _.has(options, 'insecure') ? !options.insecure : undefined
},
certificates: options.sslClientCert && new sdk.CertificateList({}, [{
name: 'client-cert',
matches: [sdk.UrlMatchPattern.MATCH_ALL_URLS],
key: { src: options.sslClientKey },
cert: { src: options.sslClientCert },
passphrase: options.sslClientPassphrase
}])
}, function (err, run) {
var callbacks = {},
// ensure that the reporter option type polymorphism is handled
reporters = _.isString(options.reporters) ? [options.reporters] : options.reporters;
// emit events for all the callbacks triggered by the runtime
_.forEach(runtimeEvents, function (definition, eventName) {
// intercept each runtime.* callback and expose a global object based event
callbacks[eventName] = function (err, cursor) {
var args = arguments,
obj = { cursor: cursor };
// convert the arguments into an object by taking the key name reference from the definition
// object
_.forEach(definition, function (key, index) {
obj[key] = args[index + 2]; // first two are err, cursor
});
args = [eventName, err, obj];
emitter.emit.apply(emitter, args); // eslint-disable-line prefer-spread
};
});
// add non generic callback handling
_.assignIn(callbacks, {
/**
* Bubbles up console messages.
*
* @param {Object} cursor - The run cursor instance.
* @param {String} level - The level of console logging [error, silent, etc].
* @returns {*}
*/
console: function (cursor, level) {
emitter.emit('console', null, { ...
...
Newman can be easily used within your JavaScript projects as a NodeJS module. The entire set of Newman CLI functionality is available
for programmatic use as well. The following example runs a collection by reading a JSON collection file stored on disk.
```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('./sample-collection.json'),
reporters: 'cli'
}, function (err) {
if (err) { throw err; }
console.log('collection run complete!');
});
```
...
version = function () { console.info(version); }
n/a
fetch = function (location, options, callback) { !callback && _.isFunction(options) && (callback = options, options = {}); return (/^https?:\/\/.*/).test(location) ? // Load from URL request.get({ url: location }, (err, response, body) => { if (err) { return callback(err); } return callback(null, body); }) : fs.readFile(location, function (err, value) { if (err) { return callback(err); } return callback(null, value.toString()); }); }
n/a
fetchJson = function (location, options, callback) { !callback && _.isFunction(options) && (callback = options, options = {}); return (/^https?:\/\/.*/).test(location) ? // Load from URL request.get({ url: location, json: true, headers: { 'User-Agent': USER_AGENT_VALUE } }, (err, response, body) => { if (err) { return callback(_.set(err, 'help', `unable to fetch data from url "${location}"`)); } try { _.isString(body) && (body = parseJson(body)); } catch (e) { return callback(_.set(e, 'help', `the url "${location}" did not provide valid JSON data`)); } return callback(null, body); }) : fs.readFile(location, function (err, value) { if (err) { return callback(_.set(err, 'help', `unable to read data from file "${location}"`)); } try { value = parseJson(value.toString()); } catch (e) { return callback(_.set(e, 'help', `the file at ${location} does not contain valid JSON data`)); } return callback(null, value); }); }
n/a
filesize = function (bytes) { return filesize(bytes || 0, FILESIZE_OPTIONS); }
n/a
getFullName = function (item, separator) { if (_.isEmpty(item) || !_.isFunction(item.parent) || !_.isFunction(item.forEachParent)) { return; } var chain = []; item.forEachParent(function (parent) { chain.unshift(parent.name || parent.id); }); item.parent() && chain.push(item.name); // Add the current item only if it is not the collection instance return chain.join(_.isString(separator) ? separator : SEP); }
n/a
prettyms = function (ms) { return (ms < 1998) ? `${parseInt(ms, 10)}ms` : prettyms(ms || 0); }
n/a