Reputation: 199
I have a case wherein I need to capture the logs generated by Newman CLI reporter under newman.run() function inside the Nodejs code and use it for later. There is no such option under reporter to capture the logs inside a file or a stream. Any suggestions for the same?
newman.run({
collection: file.json,
reporters : ['cli'],
},function (err, summary) {
if (err) {
console.log(err);
});
I need access to the logs inside the callback function.
Update:
Attaching required logs for reference:
Upvotes: 0
Views: 545
Reputation: 9390
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);
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": []
}
]
}
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);
})
npm install newman
node get-console.js
Result
newman
vs get-console.js
newman
(left) vs get-console.js
(right)
Upvotes: 0