flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
188 lines • 8.3 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FlagpoleReport = void 0;
const consoleline_1 = require("./consoleline");
const comment_1 = require("./comment");
const util_1 = require("../util");
const flagpoleexecution_1 = require("../flagpoleexecution");
const verbosity_1 = require("./verbosity");
class FlagpoleReport {
constructor(suite) {
this.suite = suite;
}
toConsole() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
let lines = [];
lines.push(new consoleline_1.HeadingLine(this.suite.title));
lines.push(new consoleline_1.CommentLine(`Base URL: ${this.suite.baseUrl}`));
lines.push(new consoleline_1.CommentLine(`Environment: ${(_a = flagpoleexecution_1.FlagpoleExecution.global.environment) === null || _a === void 0 ? void 0 : _a.name}`));
lines.push(new consoleline_1.CommentLine(`Took ${this.suite.executionDuration}ms`));
const failCount = this.suite.failCount;
const totalCount = this.suite.scenarios.length;
failCount == 0
? lines.push(new consoleline_1.PassLine(`Passed (${totalCount} scenario${totalCount == 1 ? "" : "s"})`))
: lines.push(new consoleline_1.FailLine(`Failed (${failCount} of ${totalCount} scenario${totalCount == 1 ? "" : "s"})`));
lines.push(new consoleline_1.LineBreak());
yield util_1.asyncForEach(this.suite.scenarios, (scenario) => __awaiter(this, void 0, void 0, function* () {
const log = yield scenario.getLog();
log.forEach((item) => {
lines = lines.concat(item.toConsole());
});
lines.push(new consoleline_1.LineBreak());
}));
return lines;
});
}
toJson() {
return __awaiter(this, void 0, void 0, function* () {
const scenarios = this.suite.scenarios;
let out = {
title: this.suite.title,
baseUrl: String(this.suite.baseUrl),
summary: {},
scenarios: [],
};
let failCount = 0;
let passCount = 0;
for (let i = 0; i < scenarios.length; i++) {
let scenario = scenarios[i];
const log = yield scenario.getLog();
out.scenarios[i] = {
title: scenario.title,
done: scenario.hasFinished,
failCount: 0,
passCount: 0,
log: [],
};
log.forEach((item) => {
out.scenarios[i].log.push(item.toJson());
if (item.type.startsWith("result")) {
if (item.passed) {
out.scenarios[i].passCount++;
passCount++;
}
else if (item.failed && item.isOptional) {
out.scenarios[i].failCount++;
failCount++;
}
}
});
}
out.summary = {
passed: failCount == 0,
passCount: passCount,
failCount: failCount,
duration: this.suite.executionDuration,
};
return out;
});
}
toHTML() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const scenarios = this.suite.scenarios;
let html = "";
html += '<article class="suite">' + "\n";
html += `<h2>${this.suite.title}</h2>\n`;
html += "<aside>\n";
html += "<ul>\n";
html += `
<li>Duration: ${this.suite.executionDuration}ms</li>
<li>Base URL: ${this.suite.baseUrl}</li>
<li>Environment: ${(_a = flagpoleexecution_1.FlagpoleExecution.global.environment) === null || _a === void 0 ? void 0 : _a.name}</li>
`;
html += "</ul>\n";
html += "</aside>\n";
for (let i = 0; i < scenarios.length; i++) {
let scenario = scenarios[i];
const log = yield scenario.getLog();
html += '<section class="scenario">' + "\n";
html += `
<h3>${scenario.title}</h3>
`;
html += "<ul>\n";
log.forEach((item) => {
if (item.type.startsWith("result") || item.type == "comment") {
html += item.toHtml();
}
});
html += "</ul>\n";
html += "</section>\n";
}
html += "</article>\n";
return html;
});
}
toDelimited(format) {
return __awaiter(this, void 0, void 0, function* () {
const funcName = `to${format.charAt(0).toUpperCase()}${format.slice(1)}`;
if (!Reflect.has(new comment_1.LogComment(""), funcName)) {
throw new Error(`Method for ${funcName} does not exist.`);
}
let lines = [];
yield this.suite.scenarios.forEach(function (scenario) {
return __awaiter(this, void 0, void 0, function* () {
const log = yield scenario.getLog();
log.forEach((item) => {
lines.push(item[funcName]());
});
});
});
return lines;
});
}
print() {
return __awaiter(this, void 0, void 0, function* () {
const output = yield this.toString();
const lines = output.split("\n");
lines.forEach((line) => {
process.send ? process.send(line) : console.log(line);
});
});
}
toString() {
return __awaiter(this, void 0, void 0, function* () {
let out = "";
if (flagpoleexecution_1.FlagpoleExecution.global.shouldWriteHtml) {
out += yield this.toHTML();
}
else if (flagpoleexecution_1.FlagpoleExecution.global.isJsonOutput) {
const json = yield this.toJson();
out += JSON.stringify(json, null, 2);
}
else if (flagpoleexecution_1.FlagpoleExecution.global.shouldOutputToConsole) {
(yield this.toConsole()).forEach((line) => {
if (verbosity_1.lineToVerbosity[line.type] <= flagpoleexecution_1.FlagpoleExecution.global.volume) {
out += line.toConsoleString() + "\n";
}
});
}
else if (flagpoleexecution_1.FlagpoleExecution.global.isTextOutput) {
(yield this.toConsole()).forEach((line) => {
if (verbosity_1.lineToVerbosity[line.type] >= flagpoleexecution_1.FlagpoleExecution.global.volume) {
out += line.toString() + "\n";
}
});
}
else if (flagpoleexecution_1.FlagpoleExecution.global.isDelimitedOutput) {
const format = flagpoleexecution_1.FlagpoleExecution.global.outputFormat;
(yield this.toDelimited(format)).forEach((line) => {
out += line + "\n";
});
}
return out;
});
}
}
exports.FlagpoleReport = FlagpoleReport;
//# sourceMappingURL=flagpolereport.js.map