flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
289 lines • 11.9 kB
JavaScript
"use strict";
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 });
const value_1 = require("./value");
const link_1 = require("./link");
const enums_1 = require("./enums");
const response_1 = require("./response");
const util_1 = require("./util");
const fs = require("fs");
const needle = require("needle");
class DOMElement extends value_1.Value {
get name() {
return this._name || this._path || "DOM Element";
}
constructor(input, context, name, path) {
super(input, context, name || "DOM Element");
this._path = path || "";
}
toString() {
return this._context.response.getRoot().html(this._input);
}
getClassName() {
return __awaiter(this, void 0, void 0, function* () {
return this._wrapAsValue(typeof this._input.get(0).attribs["class"] !== "undefined"
? this._input.get(0).attribs["class"]
: null, `Class Name of ${this.name}`);
});
}
hasClassName(className) {
return __awaiter(this, void 0, void 0, function* () {
return this._wrapAsValue(this._input.hasClass(className), `${this.name} has class ${className}`);
});
}
getInnerText() {
return __awaiter(this, void 0, void 0, function* () {
return this._wrapAsValue(this._input.text(), `Inner Text of ${this.name}`);
});
}
getInnerHtml() {
return __awaiter(this, void 0, void 0, function* () {
return this._wrapAsValue(this._input.html(), `Inner Html of ${this.name}`);
});
}
getOuterHtml() {
return __awaiter(this, void 0, void 0, function* () {
return this._wrapAsValue(this._context.response.getRoot().html(this._input), `Outer Html of ${this.name}`);
});
}
hasAttribute(key) {
return __awaiter(this, void 0, void 0, function* () {
return this._wrapAsValue((yield this._getAttribute(key)) != null, `${this.name} has attribute ${key}`, this);
});
}
getAttribute(key) {
return __awaiter(this, void 0, void 0, function* () {
const name = `${this.name} -> ${key}`;
const attr = yield this._getAttribute(key);
return this._wrapAsValue(attr, name, this, `${key}="${attr}"`);
});
}
getStyleProperty(key) {
return __awaiter(this, void 0, void 0, function* () {
const name = `${this.name} -> style[${key}]`;
const style = yield this._getAttribute("style");
let attr = null;
if (style) {
const properties = style.split(";").map((value) => {
return value.trim();
});
properties.some((property) => {
if (new RegExp(`^{$key}:`).test(property)) {
attr = property.substring(property.indexOf(":") + 1);
return true;
}
return false;
});
}
return this._wrapAsValue(attr, name, this);
});
}
hasProperty(key) {
return __awaiter(this, void 0, void 0, function* () {
return this._wrapAsValue(!(yield this.getProperty(key)).isNull(), `Does ${this.name} have property ${key}?`);
});
}
getProperty(key) {
return __awaiter(this, void 0, void 0, function* () {
const name = `${key} of ${this.name}`;
return this._wrapAsValue(this._input.prop(key), name);
});
}
hasData(key) {
return __awaiter(this, void 0, void 0, function* () {
return this._wrapAsValue(!(yield this.getData(key)).isNull(), `${this.name} has data ${key}`);
});
}
getData(key) {
return __awaiter(this, void 0, void 0, function* () {
const name = `Data of ${this.name}`;
return this._wrapAsValue(this._input.data(key), name);
});
}
getValue() {
return __awaiter(this, void 0, void 0, function* () {
const name = `Value of ${this.name}`;
return this._wrapAsValue(this._input.val(), name);
});
}
getText() {
return __awaiter(this, void 0, void 0, function* () {
const name = `Text of ${this.name}`;
return this._wrapAsValue(this._input.text(), name, this);
});
}
download(a, b) {
return __awaiter(this, void 0, void 0, function* () {
const localFilePath = typeof a == "string" ? a : null;
const opts = (() => {
if (typeof a == "object" && a !== null) {
return a;
}
if (typeof b == "object" && b !== null) {
return b;
}
return { encoding: null };
})();
const link = yield this._getLink();
if (link.isNavigation()) {
const resp = yield needle("get", link.getUri());
if (localFilePath) {
fs.writeFileSync(localFilePath, resp.body);
}
return resp.body;
}
return null;
});
}
load(a, b) {
const overloaded = util_1.getMessageAndCallbackFromOverloading(a, b, this._path);
const scenario = this._createSubScenario(overloaded);
this._completedAction("LOAD");
util_1.runAsync(() => __awaiter(this, void 0, void 0, function* () {
const link = yield this._getLink();
if (overloaded.scenario === undefined) {
const scenarioType = yield this._getLambdaScenarioType();
scenario.setResponseType(scenarioType, this._getLambdaScenarioOpts(scenarioType));
scenario.next(overloaded.callback);
}
if (overloaded.message.length == 0) {
scenario.title = `Load ${link.getUri()}`;
}
link.isNavigation()
? scenario.open(link.getUri())
: scenario.skip("Not a navigational link");
}), 10);
return scenario;
}
_isFormTag() {
return __awaiter(this, void 0, void 0, function* () {
return this.tagName == "form";
});
}
_isButtonTag() {
return __awaiter(this, void 0, void 0, function* () {
const type = yield this._getAttribute("type");
return (this.tagName === "button" ||
(this.tagName === "input" &&
["button", "submit", "reset"].indexOf(String(type)) >= 0));
});
}
_isLinkTag() {
return __awaiter(this, void 0, void 0, function* () {
return this.tagName === "a" && (yield this._getAttribute("href")) !== null;
});
}
_isImageTag() {
return __awaiter(this, void 0, void 0, function* () {
return this.tagName === "img" && (yield this._getAttribute("src")) !== null;
});
}
_isVideoTag() {
return __awaiter(this, void 0, void 0, function* () {
const src = yield this._getAttribute("src");
const type = yield this._getAttribute("type");
return ((this.tagName === "video" && src !== null) ||
(this.tagName === "source" && src !== null && /video/i.test(type || "")));
});
}
_isAudioTag() {
return __awaiter(this, void 0, void 0, function* () {
const src = yield this._getAttribute("src");
const type = yield this._getAttribute("type");
return ((this.tagName === "audio" && src !== null) ||
(this.tagName === "bgsound" && src !== null) ||
(this.tagName === "source" && src !== null && /audio/i.test(type || "")));
});
}
_isScriptTag() {
return __awaiter(this, void 0, void 0, function* () {
return (this.tagName === "script" && (yield this._getAttribute("src")) !== null);
});
}
_isStylesheetTag() {
return __awaiter(this, void 0, void 0, function* () {
return (this.tagName === "link" &&
(yield this._getAttribute("href")) !== null &&
String(yield this._getAttribute("rel")).toLowerCase() == "stylesheet");
});
}
_isClickable() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this._isLinkTag()) || (yield this._isButtonTag());
});
}
_getUrl() {
return __awaiter(this, void 0, void 0, function* () {
if (this.tagName !== null) {
if (["img", "script", "video", "audio", "object", "iframe"].indexOf(this.tagName) >= 0) {
return yield this._getAttribute("src");
}
else if (["a", "link"].indexOf(this.tagName) >= 0) {
return yield this._getAttribute("href");
}
else if (["form"].indexOf(this.tagName) >= 0) {
return ((yield this._getAttribute("action")) || this._context.scenario.url);
}
else if (["source"].indexOf(this.tagName) >= 0) {
return yield this._getAttribute("src");
}
}
return null;
});
}
_getLambdaScenarioType() {
return __awaiter(this, void 0, void 0, function* () {
if ((yield this._isFormTag()) || (yield this._isClickable())) {
return this._context.scenario.responseType;
}
else if (yield this._isImageTag()) {
return enums_1.ResponseType.image;
}
else if (yield this._isStylesheetTag()) {
return enums_1.ResponseType.stylesheet;
}
else if (yield this._isScriptTag()) {
return enums_1.ResponseType.script;
}
else if (yield this._isVideoTag()) {
return enums_1.ResponseType.video;
}
else {
return enums_1.ResponseType.resource;
}
});
}
_getLink() {
return __awaiter(this, void 0, void 0, function* () {
const srcPath = yield this._getUrl();
return new link_1.Link(srcPath || "", this._context);
});
}
_getLambdaScenarioOpts(newScenarioType) {
const newScenarioIsBrowser = response_1.isPuppeteer(newScenarioType);
const curScenarioIsBrowser = response_1.isPuppeteer(this._context.response.responseType);
return newScenarioIsBrowser == curScenarioIsBrowser
? this._context.scenario.request.options
: {};
}
_createSubScenario(overloaded, defaultResponseType = enums_1.ResponseType.resource, defaultOpts = {}) {
return overloaded.scenario === undefined
? this._context.suite.scenario(overloaded.message, defaultResponseType, defaultOpts)
: overloaded.scenario;
}
_loadSubScenario(overloaded) {
return overloaded.scenario === undefined
? this.load(overloaded.message, overloaded.callback)
: this.load(overloaded.scenario);
}
}
exports.DOMElement = DOMElement;
//# sourceMappingURL=domelement.js.map