-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnewman-reporter-basicText.js
More file actions
79 lines (65 loc) · 2.34 KB
/
Copy pathnewman-reporter-basicText.js
File metadata and controls
79 lines (65 loc) · 2.34 KB
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
const exportFile = require('./rollingExport');
function isTrue(bool) {
return bool && ((typeof(bool) === "boolean" && bool) || bool === 'true');
}
module.exports = function(newman, reporterOptions) {
var basicOutput = '';
typeof(variable) === "boolean"
var useCli = isTrue(reporterOptions.cli);
let useRolling = isTrue(reporterOptions.rolling);
let useExport = reporterOptions.export && typeof reporterOptions.export === 'string';
let newmanCollection = reporterOptions.collection;
function log(str) {
if (useRolling || useExport) basicOutput += str;
if (useCli) process.stdout.write(str);
}
// Add time length for all tests
newman.on('start', () => {
log(`Start collection run at ${new Date()}\n`);
this.count = 1;
});
newman.on('beforeItem', (err, o) => { });
newman.on('beforeRequest', (err, o) => { });
newman.on('request', (err, o) => {
if (err) {
console.log(JSON.stringify(err));
}
if (o) {
console.log(JSON.stringify(o));
}
});
newman.on('script', (err, o) => { });
newman.on('assertion', (err, o) => {
if (err) {
let responses = JSON.parse(JSON.stringify(o.item.responses));
log(`✗ Assertion failed! [${this.count} / ${o.item.name}] at ${new Date()}: "${o.assertion}"\n`);
log('URL PATH: ' + o.item.request.url.path.join('/') + '\n');
if (responses && responses.length > 0) {
log('CODE: ' + responses[0].code + '\n');
log('BODY: ' + responses[0].body + '\n');
}
} else {
log(` ✔ Assertion passed! [${this.count} / ${o.item.name}]: "${o.assertion}"\n`);
}
this.count++;
});
newman.on('beforeDone', (err) => {
if (err) {
console.log('there was an error');
return;
}
log(`Collection run completed for collection: ${this.count} tests executed\n`);
// Export to a single file based on rolling option
let options = {
name: 'basic-reporter',
default: 'newman-run-report.txt',
path: reporterOptions.export,
content: basicOutput
};
if (useRolling) {
exportFile(options)
} else {
newman.exports.push(options);
}
});
}