UNPKG

flagpole

Version:

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

208 lines 8.51 kB
"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 response_1 = require("../response"); const util_1 = require("../util"); 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(yield this._getClassName(), `Class Name of ${this.name}`); }); } getInnerText() { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(yield this._getInnerText(), `Inner Text of ${this.name}`); }); } getInnerHtml() { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(yield this._getInnerHtml(), `Inner Html of ${this.name}`); }); } getOuterHtml() { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(yield this._getOuterHtml(), `Outer Html of ${this.name}`); }); } 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); }); } getProperty(key) { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(yield this._getProperty(key), `${key} of ${this.name}`); }); } getValue() { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(yield this._getValue(), `Value of ${this.name}`); }); } getText() { return __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 = this._createSubScenario(overloaded); this._completedAction("LOAD"); 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()}`; } 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.responseType; } else if (yield this._isImageTag()) { return "image"; } else if (yield this._isStylesheetTag()) { return "stylesheet"; } else if (yield this._isScriptTag()) { return "script"; } else if (yield this._isVideoTag()) { return "video"; } else { return "resource"; } }); } _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 = "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