Hrishikesh Bawane
Hrishikesh Bawane

Reputation: 199

Collect newman cli reporter logs into a string in Nodejs

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: enter image description here

Upvotes: 0

Views: 545

Answers (1)

Bench Vue
Bench Vue

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);

enter image description here

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

enter image description here

Compare Between newman vs get-console.js

newman (left) vs get-console.js (right)

enter image description here

Upvotes: 0

Related Questions