UNPKG

flagpole

Version:

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

228 lines 9.97 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 domelement_1 = require("./domelement"); class HTMLElement extends domelement_1.DOMElement { get $() { return this._input; } static create(input, context, name = null, path) { return __awaiter(this, void 0, void 0, function* () { const element = new HTMLElement(input, context, name, path); element._tagName = yield element._getTagName(); element._sourceCode = (yield element.getOuterHtml()).toString(); if (name === null) { if (element._tagName !== null) { element._name = `<${element.tagName}> Element @ ${path}`; } else if (path) { element._name = String(path); } } return element; }); } constructor(input, context, name, path) { super(input, context, (name || 'HTML Element')); this._path = path || ''; this._input = input; } find(selector) { return __awaiter(this, void 0, void 0, function* () { const element = yield this.$.find(selector); const name = `${selector} under ${this.name}`; const path = `${this.path} ${selector}`; if (element !== null) { return HTMLElement.create(element, this._context, name, path); } return this._wrapAsValue(null, name, this); }); } findAll(selector) { return __awaiter(this, void 0, void 0, function* () { const elements = yield this.$.find(selector); const out = []; for (let i = 0; i < elements.length; i++) { out.push(yield HTMLElement.create(elements[i], this._context, `${selector}[${i}] under ${this.name}`, `${this.path} ${selector}[${i}]`)); } return out; }); } getClosest(selector = '*') { return __awaiter(this, void 0, void 0, function* () { const closest = yield this.$.closest(selector); const name = `Closest ${selector} of ${this.name}`; const path = `${this.path}[ancestor-or-self::${selector}]`; if (closest.length > 0) { return HTMLElement.create(closest[0], this._context, name, path); } return this._wrapAsValue(null, name, this); }); } getChildren(selector = '*') { return __awaiter(this, void 0, void 0, function* () { const children = yield this.$.children(selector); const out = []; for (let i = 0; i < children.length; i++) { out.push(yield HTMLElement.create(children[i], this._context, `Child ${selector} ${i} of ${this.name}`, `${this.path}[child::${selector}][${i}]`)); } return out; }); } getSiblings(selector = '*') { return __awaiter(this, void 0, void 0, function* () { const children = yield this.$.siblings(selector); const out = []; for (let i = 0; i < children.length; i++) { out.push(yield HTMLElement.create(children[i], this._context, `Sibling ${selector} ${i} of ${this.name}`, `${this.path}[sibling::${selector}][${i}]`)); } return out; }); } getParent() { return __awaiter(this, void 0, void 0, function* () { const parent = this.$.parent(); const name = `Parent of ${this.name}`; const path = `${this.path}[..]`; if (parent !== null) { return HTMLElement.create(parent, this._context, name, path); } return this._wrapAsValue(null, name, this); }); } getPreviousSibling(selector = '*') { return __awaiter(this, void 0, void 0, function* () { const siblings = yield this.$.prev(selector); const name = `Previous Sibling of ${this.name}`; const path = `${this.path}[preceding-sibling::${selector}][0]`; if (siblings.length > 0) { return HTMLElement.create(siblings[0], this._context, name, path); } return this._wrapAsValue(null, name, this); }); } getPreviousSiblings(selector = '*') { return __awaiter(this, void 0, void 0, function* () { const siblingElements = yield this.$.prevAll(selector); const siblings = []; for (let i = 0; i < siblingElements.length; i++) { siblings.push(yield HTMLElement.create(siblingElements[i], this._context, `Previous Sibling ${i} of ${this.name}`, `${this.path}[preceding-sibling::${selector}][${i}]`)); } return siblings; }); } getNextSibling(selector = '*') { return __awaiter(this, void 0, void 0, function* () { const siblings = yield this.$.next(selector); const name = `Next Sibling of ${this.name}`; const path = `${this.path}[following-sibling::${selector}][0]`; if (siblings.length > 0) { return HTMLElement.create(siblings[0], this._context, name, path); } return this._wrapAsValue(null, name, this); }); } getNextSiblings(selector = '*') { return __awaiter(this, void 0, void 0, function* () { const siblingElements = yield this.$.nextAll(selector); const siblings = []; for (let i = 0; i < siblingElements.length; i++) { siblings.push(yield HTMLElement.create(siblingElements[i], this._context, `Next Sibling ${i} of ${this.name}`, `${this.path}[following-sibling::${selector}][${i}]`)); } return siblings; }); } click(a, b) { return __awaiter(this, void 0, void 0, function* () { const callback = (() => { if (typeof b == 'function') { return b; } else if (typeof a == 'function') { return a; } return function () { }; })(); const message = typeof a == 'string' ? a : ''; if (yield this._isLinkTag()) { this.load(message, callback); } else if (yield this._isButtonTag()) { const type = (yield this.getAttribute('type')); if (type.isNull() || type.toString().toLowerCase() == 'submit') { const form = this._input.closest('form'); const formEl = yield HTMLElement.create(form, this._context, `Parent form of ${this.name}`, this.path); formEl.submit(message, callback); } } this._completedAction('CLICK'); }); } fillForm(formData) { return __awaiter(this, void 0, void 0, function* () { if (!(yield this._isFormTag())) { throw new Error('This is not a form element.'); } const form = this._input; for (let name in formData) { const value = formData[name]; form.find(`[name="${name}"]`).val(value); } this._completedAction('FILL'); }); } submit(a, b) { return __awaiter(this, void 0, void 0, function* () { if (!this._isFormTag()) { throw new Error('You can only use .submit() with a form element.'); } const link = yield this._getLink(); const overloaded = this._getMessageAndCallbackFromOverloading(a, b); const scenarioType = yield this._getLambdaScenarioType(); const opts = yield this._getLambdaScenarioOpts(scenarioType); const scenario = this._context.suite.scenario(overloaded.message, scenarioType, opts); const method = ((yield this._getAttribute('method')) || 'get').toString().toLowerCase(); if (link.isNavigation()) { if (method == 'get') { link.setQueryString(this.$.serializeArray()); } else { const formDataArray = this.$.serializeArray(); const formData = {}; formDataArray.forEach(function (input) { formData[input.name] = input.value; }); scenario.setFormData(formData); } scenario.setMethod(method); scenario.next(overloaded.callback); scenario.open(link.getUri()); this._completedAction('SUBMIT'); } else { scenario.skip('Nothing to submit'); } return scenario; }); } _getTagName() { return __awaiter(this, void 0, void 0, function* () { return this._input.get(0).tagName.toLowerCase(); }); } _getAttribute(key) { return __awaiter(this, void 0, void 0, function* () { return (typeof this._input.get(0).attribs[key] !== 'undefined') ? this._input.get(0).attribs[key] : null; }); } } exports.HTMLElement = HTMLElement; //# sourceMappingURL=htmlelement.js.map