flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
378 lines • 12.9 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index");
const response_1 = require("./response");
const scenario_1 = require("./scenario");
const url_1 = require("url");
const flagpolereport_1 = require("./flagpolereport");
const Bluebird = require("bluebird");
var SuiteStatusEvent;
(function (SuiteStatusEvent) {
SuiteStatusEvent[SuiteStatusEvent["beforeAllExecute"] = 0] = "beforeAllExecute";
SuiteStatusEvent[SuiteStatusEvent["beforeEachExecute"] = 1] = "beforeEachExecute";
SuiteStatusEvent[SuiteStatusEvent["afterEachExecute"] = 2] = "afterEachExecute";
SuiteStatusEvent[SuiteStatusEvent["afterAllExecute"] = 3] = "afterAllExecute";
SuiteStatusEvent[SuiteStatusEvent["finished"] = 4] = "finished";
})(SuiteStatusEvent = exports.SuiteStatusEvent || (exports.SuiteStatusEvent = {}));
class Suite {
constructor(title) {
this.scenarios = [];
this._homeScore = 0;
this._subscribers = [];
this._errorCallbacks = [];
this._successCallbacks = [];
this._failureCallbacks = [];
this._finallyCallbacks = [];
this._beforeAllCallbacks = [];
this._afterAllCallbacks = [];
this._beforeEachCallbacks = [];
this._afterEachCallbacks = [];
this._beforeAllResolver = () => { };
this._baseUrl = null;
this._timeSuiteInitialized = Date.now();
this._timeSuiteExecuted = null;
this._timeSuiteFinished = null;
this._waitToExecute = false;
this._verifySslCert = true;
this._title = title;
this._beforeAllPromise = new Promise((resolve) => {
this._beforeAllResolver = resolve;
});
}
get baseUrl() {
return this._baseUrl;
}
get hasPassed() {
return this.scenarios.every(function (scenario) {
return scenario.hasPassed;
});
}
get hasFailed() {
return this.scenarios.some(function (scenario) {
return scenario.hasFailed;
});
}
get hasExecuted() {
return this._timeSuiteExecuted !== null;
}
get hasFinished() {
return this._timeSuiteFinished !== null;
}
get totalDuration() {
return this._timeSuiteFinished !== null ?
(this._timeSuiteFinished - this._timeSuiteInitialized) : null;
}
get executionDuration() {
return this._timeSuiteExecuted !== null && this._timeSuiteFinished !== null ?
(this._timeSuiteFinished - this._timeSuiteExecuted) : null;
}
get title() {
return this._title;
}
subscribe(callback) {
this._subscribers.push(callback);
}
verifySslCert(verify) {
this._verifySslCert = verify;
return this;
}
wait(bool = true) {
this._waitToExecute = bool;
return this;
}
print(exitAfterPrint = true) {
const report = new flagpolereport_1.FlagpoleReport(this);
report.print()
.then(() => {
exitAfterPrint && index_1.Flagpole.exit(this.hasPassed);
});
}
scenario(title, type, opts) {
const scenario = scenario_1.Scenario.create(this, title, type, opts, (scenario) => {
return this._onAfterScenarioFinished(scenario);
});
scenario.before((scenario) => {
return this._onBeforeScenarioExecutes(scenario);
});
scenario.after((scenario) => {
return this._onAfterScenarioExecutes(scenario);
});
scenario.error((errorMessage) => {
return this._fireError(errorMessage);
});
scenario.verifySslCert(this._verifySslCert);
(this._waitToExecute) && scenario.wait();
this.scenarios.push(scenario);
return scenario;
}
json(title, opts = {}) {
return this.scenario(title, response_1.ResponseType.json, opts);
}
image(title, opts = {}) {
return this.scenario(title, response_1.ResponseType.image, opts);
}
video(title, opts = {}) {
return this.scenario(title, response_1.ResponseType.video, opts);
}
html(title, opts = {}) {
return this.scenario(title, response_1.ResponseType.html, opts);
}
stylesheet(title, opts = {}) {
return this.scenario(title, response_1.ResponseType.stylesheet, opts);
}
script(title, opts = {}) {
return this.scenario(title, response_1.ResponseType.script, opts);
}
resource(title, opts = {}) {
return this.scenario(title, response_1.ResponseType.resource, opts);
}
browser(title, opts = {}) {
return this.scenario(title, response_1.ResponseType.browser, opts);
}
extjs(title, opts = {}) {
return this.scenario(title, response_1.ResponseType.extjs, opts);
}
base(url) {
let baseUrl = '';
if (typeof url == 'string') {
baseUrl = url;
}
else if (Object.keys(url).length > 0) {
let env = index_1.Flagpole.getEnvironment() || 'dev';
baseUrl = url[env];
if (!baseUrl) {
baseUrl = url[Object.keys(url)[0]];
}
}
if (baseUrl.length > 0) {
this._baseUrl = new url_1.URL(baseUrl);
}
else {
throw Error('Invalid base url.');
}
return this;
}
buildUrl(path) {
if (this._baseUrl === null) {
return path;
}
else if (/^https?:\/\//.test(path) || /^data:/.test(path)) {
return path;
}
else if (/^\//.test(path)) {
return this._baseUrl.protocol + '//' + this._baseUrl.host + path;
}
return (new url_1.URL(path, this._baseUrl.href)).href;
}
execute() {
if (this.hasExecuted) {
throw new Error(`Suite already executed.`);
}
this._timeSuiteExecuted = Date.now();
this.scenarios.forEach(function (scenario) {
scenario.execute();
});
return this;
}
beforeAll(callback) {
this._beforeAllCallbacks.push(callback);
return this;
}
beforeEach(callback) {
this._beforeEachCallbacks.push(callback);
return this;
}
afterEach(callback) {
this._afterEachCallbacks.push(callback);
return this;
}
afterAll(callback) {
this._afterAllCallbacks.push(callback);
return this;
}
catch(callback) {
this._errorCallbacks.push(callback);
return this;
}
success(callback) {
this._successCallbacks.push(callback);
return this;
}
failure(callback) {
this._failureCallbacks.push(callback);
return this;
}
finally(callback) {
this._finallyCallbacks.push(callback);
return this;
}
_haveAllScenariosFinished() {
return this.scenarios.every(function (scenario) {
return scenario.hasFinished;
});
}
_fireBeforeAll() {
const suite = this;
this._timeSuiteExecuted = Date.now();
return new Promise((resolve, reject) => {
Bluebird.mapSeries(this._beforeAllCallbacks, (_then) => {
return _then(suite);
}).then(() => {
this._publish(SuiteStatusEvent.beforeAllExecute);
this._beforeAllResolver();
resolve();
}).catch((err) => {
reject(err);
});
});
}
_fireBeforeEach(scenario) {
const suite = this;
return new Promise((resolve, reject) => {
Bluebird.mapSeries(this._beforeEachCallbacks, (_then) => {
return _then.apply(suite, [scenario]);
}).then(() => {
this._publish(SuiteStatusEvent.beforeEachExecute);
resolve();
}).catch((err) => {
reject(err);
});
});
}
_fireAfterEach(scenario) {
const suite = this;
return new Promise((resolve, reject) => {
Bluebird.mapSeries(this._afterEachCallbacks, (_then) => {
return _then.apply(suite, [scenario]);
}).then(() => {
this._publish(SuiteStatusEvent.afterEachExecute);
resolve();
}).catch((err) => {
reject(err);
});
});
}
_fireAfterAll() {
const suite = this;
this._timeSuiteFinished = Date.now();
return new Promise((resolve, reject) => {
Bluebird.mapSeries(this._afterAllCallbacks, (_then) => {
return _then.apply(suite, [suite]);
}).then(() => {
this._publish(SuiteStatusEvent.afterAllExecute);
resolve();
}).catch((err) => {
reject(err);
});
});
}
_fireSuccess() {
const suite = this;
return new Promise((resolve, reject) => {
Bluebird.mapSeries(this._successCallbacks, (_then) => {
return _then.apply(suite, [suite]);
}).then(() => {
resolve();
}).catch((err) => {
reject(err);
});
});
}
_fireFailure() {
const suite = this;
return new Promise((resolve, reject) => {
Bluebird.mapSeries(this._failureCallbacks, (_then) => {
return _then.apply(suite, [suite]);
}).then(() => {
resolve();
}).catch((err) => {
reject(err);
});
});
}
_fireError(errorMessage) {
const suite = this;
return new Promise((resolve, reject) => {
Bluebird.mapSeries(this._errorCallbacks, (_then) => {
return _then.apply(suite, [errorMessage]);
}).then(() => {
resolve();
}).catch((err) => {
reject(err);
});
});
}
_fireFinally() {
const suite = this;
return new Promise((resolve, reject) => {
Bluebird.mapSeries(this._finallyCallbacks, (_then) => {
return _then.apply(suite, [suite]);
}).then(() => {
this._publish(SuiteStatusEvent.finished);
resolve();
}).catch((err) => {
reject(err);
});
});
}
_onBeforeScenarioExecutes(scenario) {
return __awaiter(this, void 0, void 0, function* () {
if (scenario.hasExecuted && !this.hasExecuted) {
yield this._fireBeforeAll();
}
yield this._beforeAllResolved();
yield this._fireBeforeEach(scenario);
return scenario;
});
}
_onAfterScenarioExecutes(scenario) {
return __awaiter(this, void 0, void 0, function* () {
yield this._fireAfterEach(scenario);
return scenario;
});
}
_onAfterScenarioFinished(scenario) {
return __awaiter(this, void 0, void 0, function* () {
if (this._haveAllScenariosFinished() && !this.hasFinished) {
yield this._fireAfterAll();
this.hasPassed ?
yield this._fireSuccess() :
yield this._fireFailure();
yield this._fireFinally();
if (index_1.Flagpole.automaticallyPrintToConsole) {
this.print(index_1.Flagpole.exitOnDone);
}
else {
index_1.Flagpole.exitOnDone && index_1.Flagpole.exit(this.hasPassed);
}
}
});
}
_beforeAllResolved() {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
this._beforeAllPromise.then(() => {
resolve(true);
})
.catch((ex) => {
reject(ex);
});
});
});
}
_publish(statusEvent) {
this._subscribers.forEach((callback) => {
callback(this, statusEvent);
});
}
}
exports.Suite = Suite;
//# sourceMappingURL=suite.js.map