UNPKG

flagpole

Version:

Simple and fast DOM integration, headless or headful browser, and REST API testing framework.

297 lines 10.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Suite = void 0; const url_1 = require("url"); const flagpole_report_1 = require("../logging/flagpole-report"); const util_1 = require("../util"); const flagpole_execution_1 = require("../flagpole-execution"); const suite_task_manager_1 = require("./suite-task-manager"); const scenario_type_map_1 = require("../scenario-type-map"); class Suite { constructor(title) { this._baseUrl = null; this._waitToExecute = false; this._verifySslCert = true; this._aliasedData = {}; this.verifyCert = this.verifySslCert; this._title = title; if (flagpole_execution_1.FlagpoleExecution.global.baseDomain) { this._baseUrl = new url_1.URL(flagpole_execution_1.FlagpoleExecution.global.baseDomain); } this._taskManager = new suite_task_manager_1.SuiteTaskManager(this); this._taskManager.finally(() => { if (flagpole_execution_1.FlagpoleExecution.global.automaticallyPrintToConsole) { this.print(flagpole_execution_1.FlagpoleExecution.global.exitOnDone); } else { flagpole_execution_1.FlagpoleExecution.global.exitOnDone && util_1.exitProcess(this.hasPassed); } }); } get baseUrl() { return this._baseUrl; } get failCount() { return this._taskManager.scenariosFailed.length; } get waitingToExecuteCount() { return this._taskManager.scenariosWaitingToExecute.length; } get executingCount() { return this._taskManager.scenariosCurrentlyExcuting.length; } get hasPassed() { return this._taskManager.haveAllPassed; } get hasFailed() { return this._taskManager.haveAnyFailed; } get hasExecuted() { return this._taskManager.hasExecutionBegan; } get hasFinished() { return this._taskManager.hasFinished; } get totalDuration() { return this.totalDuration; } get executionDuration() { return this._taskManager.executionDuration; } get title() { return this._title; } get finished() { return this._taskManager.finished; } get executionOptions() { return flagpole_execution_1.FlagpoleExecution.global; } get scenarios() { return this._taskManager.scenarios; } subscribe(callback) { this._taskManager.subscribe(callback); return this; } verifySslCert(verify) { this._verifySslCert = verify; return this; } wait(bool = true) { this._waitToExecute = bool; return this; } setConcurrencyLimit(maxExecutions) { this._taskManager.concurrencyLimit = maxExecutions; return this; } get concurrencyLimit() { return this._taskManager.concurrencyLimit; } set concurrencyLimit(maxExecutions) { this._taskManager.concurrencyLimit = maxExecutions; } setMaxScenarioDuration(timeout) { this._taskManager.maxScenarioDuration = timeout; return this; } get maxScenarioDuration() { return this._taskManager.maxScenarioDuration; } set maxScenarioDuration(timeoutMs) { this._taskManager.maxScenarioDuration = timeoutMs; } setMaxSuiteDuration(timeout) { this._taskManager.maxSuiteDuration = timeout; return this; } get maxSuiteDuration() { return this._taskManager.maxSuiteDuration; } set maxSuiteDuration(timeoutMs) { this._taskManager.maxSuiteDuration = timeoutMs; } print(exitAfterPrint = true) { const report = new flagpole_report_1.FlagpoleReport(this); report.print().then(() => { exitAfterPrint && util_1.exitProcess(this.hasPassed); }); } scenario(title, type, opts = {}) { const scenario = scenario_type_map_1.createScenario(this, title, type, opts); scenario.verifyCert(this._verifySslCert); this._waitToExecute && scenario.wait(); this._taskManager.registerScenario(scenario); return scenario; } import(originalScenario) { const scenario = this.scenario(originalScenario.title, originalScenario.type, originalScenario.opts).open(originalScenario.buildUrl().href); originalScenario.nextCallbacks.forEach((next) => { scenario.next(next.message, next.callback); }); return scenario; } base(url) { var _a; let baseUrl = ""; if (typeof url == "string") { baseUrl = url; } else if (typeof url == "function") { baseUrl = url(this); } else if (Object.keys(url).length > 0) { const env = ((_a = flagpole_execution_1.FlagpoleExecution.global.environment) === null || _a === void 0 ? void 0 : _a.name) || ""; 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; } execute() { if (this.hasExecuted) { throw new Error(`Suite already executed.`); } this.scenarios.forEach((scenario) => { scenario.wait(false); }); return this; } beforeAll(callback, prepend = false) { this._taskManager.beforeAll(callback, prepend); return this; } beforeEach(callback, prepend = false) { this._taskManager.beforeEach(callback, prepend); return this; } afterEach(callback, prepend = false) { this._taskManager.afterEach(callback, prepend); return this; } afterAll(callback, prepend = false) { this._taskManager.afterAll(callback, prepend); return this; } success(callback, prepend = false) { this._taskManager.success(callback, prepend); return this; } failure(callback, prepend = false) { this._taskManager.failure(callback, prepend); return this; } finally(callback, prepend = true) { this._taskManager.finally(callback, prepend); return this; } promise() { return new Promise((resolve, reject) => { this.success(resolve); this.failure(reject); }); } _getArray(key) { const type = util_1.toType(this._aliasedData[key]); if (type == "undefined") { this._aliasedData[key] = []; } else if (type !== "array") { throw Error(`${key} was of type ${type} and not an array. Can only push into an array.`); } return this._aliasedData[key]; } mapScenarios(a, map) { const arr = typeof a === "string" ? this._getArray(a) : a; return Promise.all(arr.map((item, i, arr) => map(item, i, arr, this).waitForFinished())); } push(key, value) { this._getArray(key).push(value); return this; } set(key, value) { this._aliasedData[key] = value; return this; } get(key) { return this._aliasedData[key]; } template(templateOptions) { return (title, scenarioOptions) => { const opts = Object.assign(Object.assign({}, templateOptions), scenarioOptions); const scenario = this.scenario(title, opts.type, opts.opts); if (opts.digestAuth) scenario.setDigestAuth(opts.digestAuth); if (opts.basicAuth) scenario.setBasicAuth(opts.basicAuth); if (opts.bearerToken) scenario.setBearerToken(opts.bearerToken); if (opts.method) scenario.setMethod(opts.method); if (opts.url) scenario.open(opts.url, opts.httpRequestOpts); if (opts.jsonBody) scenario.setJsonBody(opts.jsonBody); if (opts.rawBody) scenario.setRawBody(opts.rawBody); if (opts.formData) scenario.setFormData(opts.formData); if (opts.headers) scenario.setHeaders(opts.headers); if (opts.cookies) scenario.setCookies(opts.cookies); if (opts.timeout) scenario.setTimeout(opts.timeout); if (opts.maxRedirects) scenario.setMaxRedirects(opts.maxRedirects); if (opts.proxy) scenario.setProxy(opts.proxy); if (opts.statusCode) { scenario.next((context) => { context.assert(context.response.statusCode).equals(opts.statusCode); }); } if (opts.maxLoadTime) { scenario.next((context) => { context .assert(context.response.loadTime) .lessThanOrEquals(opts.maxLoadTime); }); } if (opts.set) { Object.keys(opts.set).forEach((key) => { if (opts.set) scenario.set(key, opts.set[key]); }); } if (opts.next) { if (typeof opts.next == "function") { scenario.next(opts.next); } else if (Array.isArray(opts.next)) { opts.next.forEach((callback) => { scenario.next(callback); }); } else { Object.keys(opts.next).forEach((title) => { if (opts.next && typeof opts.next[title] == "function") { scenario.next(title, opts.next[title]); } }); } } return scenario; }; } } exports.Suite = Suite; //# sourceMappingURL=suite.js.map