The console
event can capture the logs
newman.run({
collection: file.json,
reporters : ['cli'],
}).on('console', (error, args) => {
if (error) {
console.log(error);
}
console.log(args.messages);
})
Demo
REST API to get category
with console.log() in Tests
tab.
https://opentdb.com/api.php?amount=1&category=17
In Tests
Tab
var jsonData = JSON.parse(responseBody);
var category = jsonData["results"][0]["category"]
var question = jsonData["results"][0]["question"]
console.log("category: " + category);
console.log("question: " + question);

Full collection for two APIs.
Export as 1-log-demo.postman_collection.json
{
"info": {
"_postman_id": "eaeb4ffe-24aa-4a90-a04a-dd2beaac99e1",
"name": "1-log-demo",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "1826150"
},
"item": [
{
"name": "get Nature",
"event": [
{
"listen": "test",
"script": {
"exec": [
"var jsonData = JSON.parse(responseBody);\r",
"\r",
"var category = jsonData[\"results\"][0][\"category\"]\r",
"var question = jsonData[\"results\"][0][\"question\"]\r",
"\r",
"console.log(\"category: \" + category);\r",
"console.log(\"question: \" + question);"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "https://opentdb.com/api.php?amount=1&category=17",
"protocol": "https",
"host": [
"opentdb",
"com"
],
"path": [
"api.php"
],
"query": [
{
"key": "amount",
"value": "1"
},
{
"key": "category",
"value": "17"
}
]
}
},
"response": []
},
{
"name": "get Computers",
"event": [
{
"listen": "test",
"script": {
"exec": [
"var jsonData = JSON.parse(responseBody);\r",
"\r",
"var category = jsonData[\"results\"][0][\"category\"]\r",
"var question = jsonData[\"results\"][0][\"question\"]\r",
"\r",
"console.log(\"category: \" + category);\r",
"console.log(\"question: \" + question);"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "https://opentdb.com/api.php?amount=1&category=18",
"protocol": "https",
"host": [
"opentdb",
"com"
],
"path": [
"api.php"
],
"query": [
{
"key": "amount",
"value": "1"
},
{
"key": "category",
"value": "18"
}
]
}
},
"response": []
}
]
}
demo code
get-console.js
const newman = require('newman'); // require newman in your project
// call newman.run to pass `options` object and wait for callback
newman.run({
collection: require('./1-log-demo.postman_collection.json'),
reporters: 'cli'
}).on('console', (error, args) => {
if (error) {
console.log(error);
}
console.log(args.messages);
})
Install Dependency
npm install newman
Run it
node get-console.js
Result
