-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatejsfiles.js
225 lines (208 loc) · 7.5 KB
/
createjsfiles.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const parser = require('xml2json');
const fs = require('fs');
const path = require('path');
const propertiesReader = require('properties-reader');
var totalPass = 0;
var totalFail = 0;
var totalSkip = 0;
var total = 0;
var totalHours = 0;
var executedDate = "";
var moduleStatus = [];
var dataSet = [];
async function parseXmlFilesIntoJS(result) {
return new Promise((resolve) => {
const directoryPath = path.join(result + "/");
if (fs.existsSync(directoryPath)) {
console.log('Directory exists!');
fs.readdir(directoryPath, function (err, files) {
//handling error
if (err) {
return console.log('Unable to scan directory: ' + err);
}
//listing all files using forEach
files.forEach(function (file) {
console.log(file);
if (file.endsWith(".xml")) {
//console.log("parsing value from " + file);
let xml_string = fs.readFileSync(directoryPath + file, "utf8");
var resultJson = {};
var module = "";
var pass = 0, fail = 0, skip = 0;
var json = JSON.parse(parser.toJson(xml_string, { reversible: true }));
//console.log(json);
resultJson = json.testsuite;
if (parseInt(resultJson.tests) == 1) {
var test = resultJson.testcase;
var clsarr = test["classname"].split('.');
var len = clsarr.length;
test["classname"] = clsarr[len - 2] + "." + clsarr[len - 1];
if (test.failure) {
fail++;
test["status"] = "FAILED";
test["log"] = test.failure.$t;
delete test["failure"];
} else if (test.skipped) {
skip++;
test["status"] = "SKIPPED";
test["log"] = test.skipped.$t;
delete test["skipped"];
} else {
pass++;
test["status"] = "PASSED";
test["log"] = "";
}
module = path.basename(file, '.xml');
dataSet.push("\"" + module + "\":[" + JSON.stringify(resultJson.testcase) + "]");
} else {
resultJson.testcase.forEach(function (test) {
var clsarr = test["classname"].split('.');
var len = clsarr.length;
test["classname"] = clsarr[len - 2] + "." + clsarr[len - 1];
if (test.failure) {
fail++;
test["status"] = "FAILED";
test["log"] = JSON.stringify(test.failure);
delete test["failure"];
} else if (test.skipped) {
skip++;
test["status"] = "SKIPPED";
test["log"] = JSON.stringify(test.skipped);
delete test["skipped"];
} else {
pass++;
test["status"] = "PASSED";
test["log"] = "";
}
});
module = path.basename(file, '.xml');
dataSet.push("\"" + module + "\":" + JSON.stringify(resultJson.testcase));
}
var mjson = {};
mjson["moduleName"] = module;
mjson["passed"] = pass;
mjson["failed"] = fail;
mjson["skipped"] = skip;
mjson["total"] = parseInt(resultJson.tests);
mjson["executedOn"] = isRealValue(resultJson.timestamp)? resultJson.timestamp : new Date().toISOString().substr(0,19);
executedDate = mjson["executedOn"];
// console.log(mjson);
// input for the total cases
totalPass += pass;
totalFail += fail;
totalSkip += skip;
total += parseInt(resultJson.tests);
totalHours += parseFloat(resultJson.time);
moduleStatus.push(JSON.stringify(mjson));
return resolve(true);
} else if (file.endsWith(".json")) {
}
});
});
} else {
console.log('Directory not found.');
}
});
}
module.exports = function createjsfiles(result) {
copyFolderSync(__dirname + "/report", "./report").then(() => {
if (!fs.existsSync("./jsfiles")) {
fs.mkdirSync("./jsfiles");
}
var prop = null;
var props = {};
try {
prop = propertiesReader('./dashboardpro.properties');
props = prop.getAllProperties();
}
catch(err) {
fs.copyFileSync(__dirname + "/dashboardpro.properties", "./dashboardpro.properties");
props.line0 = 'Team Details';
props.line1 = 'NAME : YOUR TEAM';
props.line2 = 'APP VERSION : 1.1.1';
props.line3 = 'ENVIRONMENT : QA';
props.line4 = 'EXECUTED BY : YOUR NAME';
}
var details = props.line0 + '","' + props.line1 + '","' + props.line2 + '","' + props.line3 + '","' + props.line4;
fs.writeFileSync('./jsfiles/squadDetails.js', 'var squadDetails = ["' + details + '"];', function (err) {
if (err) {
console.log(err);
} else {
console.log('created squadDetails.js');
}
});
});
parseXmlFilesIntoJS(result).then(() => {
createfiles().then((result) => {
copyFolderSync("./jsfiles", "./report/jsonFiles/").then(() => {
console.log('./jsfiles/' + ' was copied to destination ' + './report/jsonFiles/');
});
});
});
}
async function createfiles() {
return new Promise((resolve) => {
//console.log(moduleStatus);
fs.writeFileSync('./jsfiles/testcases.js', 'var dataSet = {' + dataSet + '};', function (err) {
if (err) {
console.log(err);
} else {
console.log('updated testcases.js');
}
});
fs.writeFileSync('./jsfiles/modulesStatus.js', 'var modulesStatus = [' + moduleStatus + '];', function (err) {
if (err) {
console.log(err);
} else {
console.log('updated moduleStatus.js');
}
});
var tempjson = {};
tempjson["passPercentage"] = parseFloat(((totalPass / (total - totalSkip)) * 100).toFixed(2));
tempjson["totalPass"] = totalPass;
tempjson["totalFail"] = totalFail;
tempjson["totalSkip"] = totalSkip;
tempjson["total"] = total;
tempjson["totalHours"] = secondsToHms(totalHours);
tempjson["executedDate"] = new Date().toISOString().split('.')[0];
//console.log(tempjson);
fs.writeFileSync('./jsfiles/todayStatus.js', 'var todayStatus = ' + JSON.stringify(tempjson) + ';', function (err) {
if (err) {
console.log(err);
} else {
console.log('updated todayStatus.js');
}
});
return resolve(true);
});
}
async function copyFolderSync(from, to) {
return new Promise((resolve) => {
if (!fs.existsSync(to)) {
fs.mkdirSync(to);
}
fs.readdirSync(from).forEach(element => {
if (fs.lstatSync(path.join(from, element)).isFile()) {
fs.copyFileSync(path.join(from, element), path.join(to, element));
// console.log(from + " copied to " + to);
} else {
copyFolderSync(path.join(from, element), path.join(to, element));
}
});
return resolve(true);
});
}
function secondsToHms(d) {
d = Number(d);
var h = Math.floor(d / 3600);
var m = Math.floor(d % 3600 / 60);
var s = Math.floor(d % 3600 % 60);
var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
return hDisplay + mDisplay + sDisplay;
}
function isRealValue(obj)
{
return obj && obj !== 'null' && obj !== 'undefined';
}