flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
130 lines • 6.6 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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.ExtJsScenario = void 0;
const needle_1 = require("../http/needle");
const scenario_1 = require("../scenario");
const internal_1 = require("../decorators/internal");
const enums_1 = require("../interfaces/enums");
const browser_control_1 = require("./browser-control");
const assertion_result_1 = require("../logging/assertion-result");
const flagpole_execution_1 = require("../flagpole-execution");
const http_response_1 = require("../http/http-response");
const util_1 = require("../util");
const extjs_response_1 = require("./extjs-response");
class ExtJsScenario extends scenario_1.ProtoScenario {
constructor() {
super(...arguments);
this.adapter = needle_1.fetchWithNeedle;
this.response = new extjs_response_1.ExtJSResponse(this);
this.typeName = "ExtJS";
this.defaultRequestOptions = {
headless: true,
recordConsole: true,
outputConsole: false,
};
this._browserControl = null;
}
get browserControl() {
if (this._browserControl === null) {
this._browserControl = new browser_control_1.BrowserControl();
}
return this._browserControl;
}
get browser() {
var _a;
return ((_a = this._browserControl) === null || _a === void 0 ? void 0 : _a.browser) || null;
}
get page() {
return this.browserControl !== null ? this.browserControl.page : null;
}
_executeHttpRequest() {
if (this.url === null) {
throw "Can not execute request with null URL.";
}
this.url = this.buildUrl().href;
this._markRequestAsStarted();
this._finalUrl = this.request.uri;
this._executeBrowserRequest();
}
_getRequestOptions(opts = {}) {
opts.browserOptions = Object.assign(Object.assign({}, this.defaultRequestOptions), opts.browserOptions);
if (flagpole_execution_1.FlagpoleExecution.global.headless !== undefined) {
opts.browserOptions.headless = flagpole_execution_1.FlagpoleExecution.global.headless;
}
return opts;
}
_executeBrowserRequest() {
if (!this.browserControl)
throw "Not a browser scenario";
const handleError = (message, e) => {
setTimeout(() => {
this._markScenarioCompleted(message, e, enums_1.ScenarioDisposition.aborted);
}, 1000);
};
this.browserControl
.open(this.request)
.then((next) => {
var _a, _b, _c, _d, _e, _f, _g;
const response = next.response;
if (response !== null) {
this._finalUrl = response.url();
response
.request()
.redirectChain()
.forEach((req) => {
this._redirectChain.push(req.url());
});
(_a = this.browser) === null || _a === void 0 ? void 0 : _a.on("disconnected", (e) => handleError("Puppeteer instance unexpectedly closed.", e));
(_c = (_b = this.browserControl) === null || _b === void 0 ? void 0 : _b.page) === null || _c === void 0 ? void 0 : _c.on("close", (e) => handleError("Puppeteer closed unexpectedly.", e));
(_e = (_d = this.browserControl) === null || _d === void 0 ? void 0 : _d.page) === null || _e === void 0 ? void 0 : _e.on("error", (e) => handleError("Puppeteer got an unexpected error.", e));
(_g = (_f = this.browserControl) === null || _f === void 0 ? void 0 : _f.page) === null || _g === void 0 ? void 0 : _g.on("pageerror", (e) => this._pushToLog(new assertion_result_1.AssertionFailOptional("Puppeteer got an unexpected page error.", e)));
this._processResponse(new http_response_1.HttpResponse({
status: [response.status(), response.statusText()],
headers: response.headers(),
body: next.body,
cookies: next.cookies || {},
}));
}
else {
this._markScenarioCompleted(`Failed to load ${this.request.uri}`, null, enums_1.ScenarioDisposition.aborted);
}
return;
})
.catch((err) => this._markScenarioCompleted(`Failed to load ${this.request.uri}`, err, enums_1.ScenarioDisposition.aborted));
}
_markScenarioCompleted(message = null, details = null, disposition = enums_1.ScenarioDisposition.completed) {
const _super = Object.create(null, {
_markScenarioCompleted: { get: () => super._markScenarioCompleted }
});
return __awaiter(this, void 0, void 0, function* () {
if (!this.hasFinished) {
_super._markScenarioCompleted.call(this, message, details, disposition);
util_1.runAsync(() => {
var _a;
(_a = this.browserControl) === null || _a === void 0 ? void 0 : _a.close();
}, 100);
}
return this;
});
}
}
__decorate([
internal_1.beforeScenarioRequestStarted
], ExtJsScenario.prototype, "_executeHttpRequest", null);
exports.ExtJsScenario = ExtJsScenario;
//# sourceMappingURL=extjs-scenario.js.map
;