UNPKG

flagpole

Version:

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

277 lines 11.1 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(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 assertionresult_1 = require("./logging/assertionresult"); const util_1 = require("./util"); class DOMElement extends value_1.ProtoValue { constructor(input, context, name, path) { super(input, context, (name || 'DOM Element')); this._tagName = ''; this._path = path || ''; } get path() { return this._path; } get name() { return this._name || this._path || 'DOM Element'; } get tagName() { return this._tagName; } 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}`); }); } getTagName() { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(this.tagName, `Tag Name of ${this.name}`); }); } 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}"`); }); } 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); }); } load(a, b) { const overloaded = this._getMessageAndCallbackFromOverloading(a, b); const scenario = this._context.suite.scenario(overloaded.message, enums_1.ResponseType.resource, {}); this._completedAction('LOAD'); util_1.runAsync(() => __awaiter(this, void 0, void 0, function* () { const link = yield this._getLink(); const scenarioType = yield this._getLambdaScenarioType(); const opts = yield this._getLambdaScenarioOpts(scenarioType); scenario.setResponseType(scenarioType, opts); scenario.next(overloaded.callback); if (overloaded.message.length == 0) { scenario.title = `Load ${link.getUri()}`; } if (link.isNavigation()) { scenario.open(link.getUri()); } else { 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); }); } _getMessageAndCallbackFromOverloading(a, b) { const message = typeof a == 'string' ? a : this._path; const callback = ((function () { if (typeof b == 'function') { return b; } else if (typeof a == 'function') { return a; } else { return function () { }; } })()); return { message: message, callback: callback }; } _getLambdaScenarioOpts(newScenarioType) { const newScenarioIsBrowser = response_1.isPuppeteer(newScenarioType); const curScenarioIsBrowser = response_1.isPuppeteer(this._context.response.responseType); return newScenarioIsBrowser == curScenarioIsBrowser ? this._context.scenario.requestOptions : {}; } _completedAction(verb, noun) { return __awaiter(this, void 0, void 0, function* () { this._context.scenario.result(new assertionresult_1.AssertionActionCompleted(verb, noun || this.name)); }); } _failedAction(verb, noun) { return __awaiter(this, void 0, void 0, function* () { this._context.scenario.result(new assertionresult_1.AssertionActionFailed(verb, noun || this.name)); }); } } exports.DOMElement = DOMElement; //# sourceMappingURL=domelement.js.map