UNPKG

8.23 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const consoleline_1 = require("./consoleline");
13const flagpoleexecutionoptions_1 = require("../flagpoleexecutionoptions");
14const comment_1 = require("./comment");
15const enums_1 = require("../enums");
16const util_1 = require("../util");
17class FlagpoleReport {
18 constructor(suite, opts) {
19 this.suite = suite;
20 this.opts = opts;
21 }
22 toConsole() {
23 return __awaiter(this, void 0, void 0, function* () {
24 let lines = [];
25 lines.push(new consoleline_1.HeadingLine(this.suite.title));
26 lines.push(new consoleline_1.CommentLine(`Base URL: ${this.suite.baseUrl}`));
27 lines.push(new consoleline_1.CommentLine(`Environment: ${flagpoleexecutionoptions_1.FlagpoleExecution.opts.environment}`));
28 lines.push(new consoleline_1.CommentLine(`Took ${this.suite.executionDuration}ms`));
29 const failCount = this.suite.failCount;
30 const totalCount = this.suite.scenarios.length;
31 failCount == 0
32 ? lines.push(new consoleline_1.PassLine(`Passed (${totalCount} scenario${totalCount == 1 ? "" : "s"})`))
33 : lines.push(new consoleline_1.FailLine(`Failed (${failCount} of ${totalCount} scenario${totalCount == 1 ? "" : "s"})`));
34 lines.push(new consoleline_1.LineBreak());
35 yield util_1.asyncForEach(this.suite.scenarios, (scenario) => __awaiter(this, void 0, void 0, function* () {
36 const log = yield scenario.getLog();
37 log.forEach((item) => {
38 lines = lines.concat(item.toConsole());
39 });
40 lines.push(new consoleline_1.LineBreak());
41 }));
42 return lines;
43 });
44 }
45 toJson() {
46 return __awaiter(this, void 0, void 0, function* () {
47 const scenarios = this.suite.scenarios;
48 let out = {
49 title: this.suite.title,
50 baseUrl: String(this.suite.baseUrl),
51 summary: {},
52 scenarios: []
53 };
54 let failCount = 0;
55 let passCount = 0;
56 for (let i = 0; i < scenarios.length; i++) {
57 let scenario = scenarios[i];
58 const log = yield scenario.getLog();
59 out.scenarios[i] = {
60 title: scenario.title,
61 done: scenario.hasFinished,
62 failCount: 0,
63 passCount: 0,
64 log: []
65 };
66 log.forEach((item) => {
67 out.scenarios[i].log.push(item.toJson());
68 if (item.type == enums_1.LogItemType.Result) {
69 if (item.passed) {
70 out.scenarios[i].passCount++;
71 passCount++;
72 }
73 else if (item.failed && item.isOptional) {
74 out.scenarios[i].failCount++;
75 failCount++;
76 }
77 }
78 });
79 }
80 out.summary = {
81 passed: failCount == 0,
82 passCount: passCount,
83 failCount: failCount,
84 duration: this.suite.executionDuration
85 };
86 return out;
87 });
88 }
89 toHTML() {
90 return __awaiter(this, void 0, void 0, function* () {
91 const scenarios = this.suite.scenarios;
92 let html = "";
93 html += '<article class="suite">' + "\n";
94 html += `<h2>${this.suite.title}</h2>\n`;
95 html += "<aside>\n";
96 html += "<ul>\n";
97 html += `
98 <li>Duration: ${this.suite.executionDuration}ms</li>
99 <li>Base URL: ${this.suite.baseUrl}</li>
100 <li>Environment: ${flagpoleexecutionoptions_1.FlagpoleExecution.opts.environment}</li>
101 `;
102 html += "</ul>\n";
103 html += "</aside>\n";
104 for (let i = 0; i < scenarios.length; i++) {
105 let scenario = scenarios[i];
106 const log = yield scenario.getLog();
107 html += '<section class="scenario">' + "\n";
108 html += `
109 <h3>${scenario.title}</h3>
110 `;
111 html += "<ul>\n";
112 log.forEach((item) => {
113 if (item.type == enums_1.LogItemType.Result ||
114 item.type == enums_1.LogItemType.Comment) {
115 html += item.toHtml();
116 }
117 });
118 html += "</ul>\n";
119 html += "</section>\n";
120 }
121 html += "</article>\n";
122 return html;
123 });
124 }
125 toDelimited(format) {
126 return __awaiter(this, void 0, void 0, function* () {
127 const funcName = `to${format.charAt(0).toUpperCase()}${format.slice(1)}`;
128 if (!Reflect.has(new comment_1.LogComment(""), funcName)) {
129 throw new Error(`Method for ${funcName} does not exist.`);
130 }
131 let lines = [];
132 yield this.suite.scenarios.forEach(function (scenario) {
133 return __awaiter(this, void 0, void 0, function* () {
134 const log = yield scenario.getLog();
135 log.forEach((item) => {
136 lines.push(item[funcName]());
137 });
138 });
139 });
140 return lines;
141 });
142 }
143 print() {
144 return __awaiter(this, void 0, void 0, function* () {
145 const output = yield this.toString();
146 const lines = output.split("\n");
147 lines.forEach(line => {
148 process.send ? process.send(line) : console.log(line);
149 });
150 });
151 }
152 toString() {
153 return __awaiter(this, void 0, void 0, function* () {
154 let out = "";
155 if (this.opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.html ||
156 this.opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.browser) {
157 out += yield this.toHTML();
158 }
159 else if (this.opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.json) {
160 const json = yield this.toJson();
161 out += JSON.stringify(json, null, 2);
162 }
163 else if (this.opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.console) {
164 (yield this.toConsole()).forEach((line) => {
165 out += line.toConsoleString() + "\n";
166 });
167 }
168 else if (this.opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.text) {
169 (yield this.toConsole()).forEach((line) => {
170 out += line.toString() + "\n";
171 });
172 }
173 else if (this.opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.csv ||
174 this.opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.psv ||
175 this.opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.tsv) {
176 const format = flagpoleexecutionoptions_1.FlagpoleOutput[this.opts.output];
177 (yield this.toDelimited(format)).forEach((line) => {
178 out += line + "\n";
179 });
180 }
181 return out;
182 });
183 }
184}
185exports.FlagpoleReport = FlagpoleReport;
186//# sourceMappingURL=flagpolereport.js.map
\No newline at end of file