-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
117 lines (110 loc) · 3.49 KB
/
index.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
const fs = require("fs");
const path = require("path");
const program = require("commander");
program
.option("-d, --dir <dir>", "Directory to scan")
.option("-c, --config <config>", "JSON file with the configuration")
.option("-h, --host <host>", "URL of the host")
.option("-u, --user <user>", "Username")
.option("-p, --password <password>", "Password")
.option("-s, --suite <suite>", "Suite ID")
.option("-pr, --project <project>", "Project ID")
.option("-pl, --plan <plan>", "Plan ID");
program.parse(process.argv);
var config = {};
var basePath = path.resolve("output");
if (program.config) {
config = JSON.parse(fs.readFileSync(path.resolve(program.config)));
} else {
config.host = program.host;
config.user = program.user;
config.password = program.password;
config.suiteId = program.suite;
config.projectId = program.project;
config.planId = program.plan;
}
if (program.dir) basePath = program.dir;
const testrail = require("./testrail").createClient(config);
const moment = require("moment-timezone");
const parser = require("fast-xml-parser");
const _ = require("lodash");
var tests = { results: [] };
const files = fs.readdirSync(basePath).filter((f) => f.includes(".xml"));
for (const file of files) {
const data = fs.readFileSync(path.join(basePath, file)).toString();
const json = parser.parse(data, {
attributeNamePrefix: "_",
ignoreAttributes: false,
parseAttributeValue: true,
});
const testCases = json["ns2:test-suite"]["test-cases"]["test-case"];
if (Array.isArray(testCases)) {
for (const testCase of testCases) {
compute(testCase);
}
} else {
compute(testCases);
}
}
function compute(testCase) {
const matches = testCase.name.match(/C\d{4,}/g);
if (matches != null) {
const ids = matches.map((m) => m.replace("C", ""));
ids.forEach((id) => {
const elapsed = Math.round((testCase._stop - testCase._start) / 1000);
const data = {
case_id: id,
elapsed: elapsed === 0 ? "1s" : `${elapsed}s`,
comment: `This test case has ${testCase._status}.`,
};
const duplicate = tests.results.find((t) => t.case_id === id);
if (duplicate != undefined && duplicate.status_id === 5) {
data.comment = `This test case has ${testCase._status} after retry.`;
}
if (testCase._status === "pending") return;
if (testCase._status === "passed") data.status_id = 1;
if (testCase._status === "failed") {
data.status_id = 5;
data.comment += `\nReason: ${testCase.failure.message}`;
}
tests.results.push(data);
});
}
}
async function submit(tests) {
const name = `Automation run on ${moment()
.tz("America/Denver")
.format("YYYY-MM-DD h:mm")}`;
const caseIds = tests.results.map((t) => t.case_id);
var run = undefined;
if (config.planId) {
const data = {
suite_id: config.suiteId,
name: name,
include_all: false,
case_ids: caseIds,
runs: [
{
include_all: false,
case_ids: caseIds,
},
],
};
const planEntry = await testrail.addPlanEntry(config.planId, data);
run = planEntry.runs[0];
} else {
run = await testrail.addRun(config.projectId, {
suite_id: config.suiteId,
name: name,
include_all: false,
});
await testrail.updateRun(run.id, {
case_ids: caseIds,
});
}
await testrail.addResultsForCases(run.id, tests);
return { run: run };
}
submit(tests).then((response) =>
console.log(JSON.stringify(response, null, 2))
);