-
Notifications
You must be signed in to change notification settings - Fork 0
/
newman.js
121 lines (114 loc) · 4.29 KB
/
newman.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require('dotenv').config();
const fs = require("fs");
const path = require("path");
const newman = require("newman");
const date = require("date-and-time");
/**
* Executes a collection using Newman with the provided options.
* @param {*} options
* @returns
*/
async function newmanRun(options) {
return new Promise(function (onResolve, onReject) {
newman.run(options).on("done", function (err, summary) {
if (err) {
console.log(`test run failed!${err}`);
onReject(err);
}
onResolve(summary);
});
});
}
/**
* Recursively traverses a directory and fetch names of all files within it.
* @param {*} dir
*/
function* walkSync(dir) {
const files = fs.readdirSync(dir, {
withFileTypes: true
});
for (const file of files) {
if (file.isDirectory()) {
yield* walkSync(path.join(dir, file.name));
} else {
yield [path.join(dir, file.name), file.name];
}
}
}
/**
* Returns the time difference between two timestamps in minutes and seconds.
*
* @param {number} start - Start time in milliseconds.
* @param {number} end - End time in milliseconds.
* @returns {Promise<string>} - Time difference as "minutes.seconds minutes".
*/
async function getTimeDifference(start, end) {
const timeDiff = Math.abs(end - start);
const minutes = Math.floor(timeDiff / (60 * 1000));
const seconds = Math.floor((timeDiff % (60 * 1000)) / 1000);
const exeTimeDiff = `${minutes}.${seconds} minutes`;
return exeTimeDiff;
}
/**
* Executing Postman collections and generating reports.
* @param {*} envFileName
* @returns
*/
async function executeNewman(envFileName) {
const filePathList = [];
const fileNameList = [];
var summary_json = [];
for (const filePath of walkSync(__dirname + '\\collections\\')) {
if (
filePath[1] !== envFileName &&
filePath[1].endsWith(".json") &&
!filePath[0].includes("node_modules") &&
!(filePath[1] === "package.json" || filePath[1] === "package-lock.json")
) {
filePathList.push(filePath[0]);
fileNameList.push(filePath[1]);
}
}
const dateValue = date.format(new Date(), "DD-MM-YYYY", 'Asia/Kolkata');
const month = new Date(date.format(new Date(), "Y, M, D")).toLocaleString('default', { month: 'long' });
const reportFolder = `${month}/${dateValue}`;
for (let i = 0; i < fileNameList.length; i++) {
const envPath = path.join(__dirname, "environment", envFileName);
const options = {
collection: require(filePathList[i]),
environment: require(envPath),
reporters: ['htmlextra', 'cli'],
reporter: {
htmlextra: {
export: `./report/${reportFolder}/` + fileNameList[i].split(".")[0].concat('.html')
}
},
insecure: true, // allow self-signed certs, required in postman too
timeoutscript: 1800000 // Script time out
};
try {
let collection_name = fileNameList[i].split(".")[0];
console.log('\n\nExecuting: ' + collection_name);
let report_link = `${reportFolder}/` + collection_name.concat('.html');
const resp = await newmanRun(options);
var collection_result = {
Collection: collection_name,
RequestsTotal: JSON.stringify(resp.run.stats.requests.total),
RequestFailed: JSON.stringify(resp.run.stats.requests.failed),
AssertionsFailed: JSON.stringify(resp.run.stats.assertions.failed),
TestScriptTotal: JSON.stringify(resp.run.stats.testScripts.total),
TestScriptFailed: JSON.stringify(resp.run.stats.testScripts.failed),
Report_link: report_link
};
summary_json.push(collection_result);
const execution_time = await getTimeDifference(resp.run.timings.started, resp.run.timings.completed);
console.log('Execution Time: ' + execution_time);
console.log(collection_result);
} catch (error) {
console.log(`Error in ${fileNameList[i]}`);
throw error;
}
}
return "Done";
}
module.exports = { executeNewman };