flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
192 lines • 8.38 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 });
exports.DOMElement = void 0;
const value_1 = require("../value");
const util_1 = require("../util");
const value_promise_1 = require("../value-promise");
const image_scenario_1 = require("../visual/image-scenario");
const resource_scenario_1 = require("../resource/resource-scenario");
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 value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
return this._wrapAsValue(yield this._getClassName(), `Class Name of ${this.name}`);
}));
}
getInnerText() {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
return this._wrapAsValue(yield this._getInnerText(), `Inner Text of ${this.name}`);
}));
}
getInnerHtml() {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
return this._wrapAsValue(yield this._getInnerHtml(), `Inner Html of ${this.name}`);
}));
}
getOuterHtml() {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
return this._wrapAsValue(yield this._getOuterHtml(), `Outer Html of ${this.name}`);
}));
}
getAttribute(key) {
return value_promise_1.ValuePromise.execute(() => __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 value_promise_1.ValuePromise.execute(() => __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);
}));
}
getProperty(key) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
return this._wrapAsValue(yield this._getProperty(key), `${key} of ${this.name}`);
}));
}
getValue() {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(yield this._getValue(), `Value of ${this.name}`); }));
}
getText() {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(yield this._getText(), `Text of ${this.name}`); }));
}
load(a, b) {
return __awaiter(this, void 0, void 0, function* () {
const overloaded = util_1.getMessageAndCallbackFromOverloading(a, b, this._path);
const scenario = yield this._createSubScenario(overloaded);
this._completedAction("LOAD");
const link = yield this.getLink();
if (overloaded.scenario === undefined) {
scenario.next(overloaded.callback);
}
if (overloaded.message.length == 0) {
scenario.title = `Load ${link.getUri()}`;
}
if (!link.isNavigation()) {
scenario.skip("Not a navigational link");
}
const uri = link.getUri();
scenario.open(uri);
return scenario;
});
}
_isFormTag() {
return __awaiter(this, void 0, void 0, function* () {
return this.isTag("form");
});
}
_isButtonTag() {
return __awaiter(this, void 0, void 0, function* () {
const type = yield this._getAttribute("type");
return (this.isTag("button") ||
(this.isTag("input") &&
["button", "submit", "reset"].indexOf(String(type)) >= 0));
});
}
_isLinkTag() {
return __awaiter(this, void 0, void 0, function* () {
return this.isTag("a") && (yield this._getAttribute("href")) !== null;
});
}
_isImageTag() {
return __awaiter(this, void 0, void 0, function* () {
return this.isTag("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.isTag("video") && src !== null) ||
(this.isTag("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.isTag("audio", "bgsound") && src !== null) ||
(this.tagName === "source" && src !== null && /audio/i.test(type || "")));
});
}
_isScriptTag() {
return __awaiter(this, void 0, void 0, function* () {
return this.isTag("script") && (yield this._getAttribute("src")) !== null;
});
}
_isStylesheetTag() {
return __awaiter(this, void 0, void 0, function* () {
return (this.isTag("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());
});
}
_getLambdaScenarioType() {
return __awaiter(this, void 0, void 0, function* () {
if ((yield this._isFormTag()) || (yield this._isClickable())) {
return this.context.scenario.type;
}
else if (yield this._isImageTag()) {
return image_scenario_1.ImageScenario;
}
else {
return resource_scenario_1.ResourceScenario;
}
});
}
_createSubScenario(overloaded) {
return __awaiter(this, void 0, void 0, function* () {
const scenarioType = yield this._getLambdaScenarioType();
const opts = this.context.scenario.request.options;
return overloaded.scenario === undefined
? this.context.suite.scenario(overloaded.message, scenarioType, opts)
: overloaded.scenario;
});
}
_loadSubScenario(overloaded) {
return overloaded.scenario === undefined
? this.load(overloaded.message, overloaded.callback)
: this.load(overloaded.scenario);
}
}
exports.DOMElement = DOMElement;
//# sourceMappingURL=dom-element.js.map