UNPKG

flagpole

Version:

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

783 lines 26.7 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.Value = void 0; const util_1 = require("./util"); const assertionresult_1 = require("./logging/assertionresult"); const link_1 = require("./link"); const fs = require("fs"); const value_promise_1 = require("./value-promise"); const httprequest_1 = require("./httprequest"); const jpath_1 = require("./json/jpath"); class Value { constructor(input, context, name, parent = null, highlight = "") { this._sourceCode = null; this._input = input; this._context = context; this._name = name || null; this._parent = parent; this._highlight = highlight; } get context() { return this._context; } get $() { return this._input; } get tagName() { return (this._tagName || "").toLowerCase(); } get outerHTML() { return this._sourceCode || ""; } get is() { return this.assert().is; } selectOption(value) { throw "This Value does not support select."; } pressEnter() { return __awaiter(this, void 0, void 0, function* () { throw "This Value does not support pressEnter."; }); } get length() { return new Value(this.$ && this.$.length ? this.$.length : 0, this._context, `Length of ${this._name}`); } get trim() { const thisValue = this.$; return new Value(typeof thisValue === "string" ? thisValue.trim() : thisValue, this._context, `Trim of ${this._name}`); } get uppercase() { const thisValue = this.$; return new Value(typeof thisValue === "string" ? thisValue.toUpperCase() : thisValue, this._context, `Uppercase of ${this._name}`); } get lowercase() { const thisValue = this.$; return new Value(typeof thisValue === "string" ? thisValue.toLowerCase() : thisValue, this._context, `Lowercase of ${this._name}`); } get first() { const thisValue = this.$; return new Value(util_1.firstIn(thisValue), this._context, `First in ${this._name}`); } get mid() { const thisValue = this.$; return new Value(util_1.middleIn(thisValue), this._context, `Middle in ${this._name}`); } get last() { const thisValue = this.$; return new Value(util_1.lastIn(thisValue), this._context, `Last in ${this._name}`); } get random() { const thisValue = this.$; return new Value(util_1.randomIn(thisValue), this._context, `Random in ${this._name}`); } get string() { return new Value(this.toString(), this._context, this.name); } get array() { return new Value(this.toArray(), this._context, this.name); } get float() { return new Value(this.toFloat(), this._context, this.name); } get int() { return new Value(this.toInteger(), this._context, this.name); } get bool() { return new Value(this.toBoolean(), this._context, this.name); } get json() { return new Value(this.toJSON(), this._context, this.name); } get path() { return this._path || ""; } get name() { return this._name || "it"; } get highlight() { return this._highlight; } get parent() { return this._parent; } get sourceCode() { return this._sourceCode === null ? "" : this._sourceCode; } get isFlagpoleValue() { return true; } toArray() { return this.isArray() ? this._input : [this._input]; } valueOf() { return this._input; } toString() { const type = util_1.toType(this._input); if (type == "value" && this._input && this._input.$) { return String(this._input.$); } else if (this._input && this._input.value) { return String(this._input.value); } else if (type == "object") { return String(Object.keys(this._input)); } return String(this._input); } toBoolean() { return !!this.$; } toFloat() { return parseFloat(this.toString()); } toInteger() { return parseInt(this.toString()); } toJSON() { try { return JSON.parse(this.toString()); } catch (ex) { return null; } } toURL(baseUrl) { return new URL(this.toString(), baseUrl); } toType() { return String(util_1.toType(this._input)); } isNullOrUndefined() { return util_1.isNullOrUndefined(this._input); } isUndefined() { return this.toType() == "undefined"; } isNull() { return this._input === null; } isPromise() { return this.toType() == "promise"; } isArray() { return this.toType() == "array"; } isString() { return this.toType() == "string"; } isObject() { return this.toType() == "object"; } isNumber() { return this.toType() == "number" && this._input !== NaN; } isNumeric() { return !isNaN(this._input); } isNaN() { return this._input === NaN; } isCookie() { return this._input && this._input.cookieString; } isRegularExpression() { return this.toType() == "regexp"; } isCheerioElement() { return this.toType() == "cheerio"; } isPuppeteerElement() { return this.toType() == "elementhandle"; } hasProperty(key, value) { return __awaiter(this, void 0, void 0, function* () { const thisValue = yield this.getProperty(key); if (value === undefined) { return !thisValue.isNullOrUndefined(); } if (value instanceof RegExp) { return value.test(thisValue.toString()); } return value == thisValue.$; }); } hasValue(value) { return __awaiter(this, void 0, void 0, function* () { const thisValue = yield this.getValue(); if (value === undefined) { return !thisValue.isNullOrUndefined(); } if (value instanceof RegExp) { return value.test(thisValue.toString()); } return value == thisValue.$; }); } as(aliasName) { this._context.scenario.set(aliasName, this); return this; } getProperty(key) { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(this._input[key], `${this.name} property of ${key}`); }); } click() { return __awaiter(this, void 0, void 0, function* () { this._context.logFailure(`Element could not be clicked on: ${this.name}`); return this; }); } submit() { return __awaiter(this, void 0, void 0, function* () { this._context.logFailure(`Element could not be submitted on: ${this.name}`); return this; }); } open(a, b, c) { const message = typeof a == "string" ? a : `Open ${this.name}`; const responseType = typeof b == "string" ? b : this.context.response.responseType; const callback = (() => { return typeof c == "function" ? c : typeof b == "function" ? b : typeof a == "function" ? a : () => { }; })(); const scenario = (() => { return util_1.toType(a) == "scenario" ? a : this.context.suite.scenario(message, responseType); })(); scenario.next(callback); util_1.runAsync(() => __awaiter(this, void 0, void 0, function* () { const link = yield this.getLink(); if (link.isNavigation()) { scenario.open(link.getUri()); } })); this._completedAction("OPEN"); return scenario; } isVisible() { return __awaiter(this, void 0, void 0, function* () { return true; }); } isHidden() { return __awaiter(this, void 0, void 0, function* () { return false; }); } isTag(...tagNames) { if (!this.tagName) { return false; } return tagNames.length ? tagNames.includes(this.tagName) : true; } getLink() { return __awaiter(this, void 0, void 0, function* () { const src = yield this.getUrl(); return new link_1.Link(src.isString() ? src.toString() : "", this._context); }); } getUrl() { return __awaiter(this, void 0, void 0, function* () { const url = yield (() => __awaiter(this, void 0, void 0, function* () { if (this.isString()) { return this.toString(); } if (this.isTag("img", "script", "video", "audio", "object", "iframe", "source")) { return (yield this.getAttribute("src")).$; } else if (this.isTag("a", "link")) { return (yield this.getAttribute("href")).$; } else if (this.isTag("form")) { return ((yield this.getAttribute("action")).$ || this._context.scenario.url); } return null; }))(); return this._wrapAsValue(url, `URL from ${this.name}`, this); }); } fillForm(a, b) { return __awaiter(this, void 0, void 0, function* () { return this; }); } exists(selector) { return __awaiter(this, void 0, void 0, function* () { if (selector === undefined) { this.isNullOrUndefined() ? this._failedAction("EXISTS", `${this.name}`) : this._completedAction("EXISTS", `${this.name}`); return this; } else { const el = yield this.find(selector); el.isNull() ? this._failedAction("EXISTS", `${selector}`) : this._completedAction("EXISTS", `${selector}`); return el; } }); } find(selector) { return value_promise_1.ValuePromise.create(this.item(selector)); } findAll(selector) { return __awaiter(this, void 0, void 0, function* () { return [yield this.find(selector)]; }); } getClassName() { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(null, `${this.name} Class`); }); } hasClassName(name) { return __awaiter(this, void 0, void 0, function* () { const myClass = (yield this.getClassName()).toString(); const classes = myClass.split(" "); return (() => { if (name === undefined) { return !!myClass; } return classes.some((cls) => { return typeof name == "string" ? name == cls : name.test(cls); }); })(); }); } getTag() { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(this.tagName, `Tag Name of ${this.name}`); }); } hasTag(tag) { return __awaiter(this, void 0, void 0, function* () { const myTag = (yield this.getTag()).$; if (tag === undefined) { return !!myTag; } return tag instanceof RegExp ? tag.test(myTag) : myTag == tag; }); } getInnerText() { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(this.toString(), `Inner Text of ${this.name}`); }); } getInnerHtml() { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(null, `Inner HTML of ${this.name}`); }); } getOuterHtml() { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(null, `Outer HTML of ${this.name}`); }); } hasAttribute(key, value) { return __awaiter(this, void 0, void 0, function* () { const thisValue = yield this.getAttribute(key); if (thisValue.isNullOrUndefined()) { return false; } const strThisValue = thisValue.toString(); return value === undefined ? !!thisValue : typeof value == "string" ? value == strThisValue : value.test(strThisValue); }); } getAttribute(key) { return this.getProperty(key); } getStyleProperty(key) { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(null, `Style of ${key}`); }); } getValue() { return __awaiter(this, void 0, void 0, function* () { return this; }); } scrollTo() { return __awaiter(this, void 0, void 0, function* () { }); } hasText(text) { return __awaiter(this, void 0, void 0, function* () { const myText = (yield this.getText()).$; return text ? text == myText : !!myText; }); } getText() { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(this.toString(), this.name, this.parent, this.highlight); }); } get values() { let values = []; try { values = Object.values(this.$); } catch (_a) { } return this._wrapAsValue(values, `Values of ${this.name}`, this, this.highlight); } get keys() { let keys = []; try { keys = Object.keys(this.$); } catch (_a) { } return this._wrapAsValue(keys, `Keys of ${this.name}`, this, this.highlight); } screenshot() { return __awaiter(this, void 0, void 0, function* () { throw new Error(`This value type (${this.toType()}) or scenario type (${this._context.scenario.responseType}) does not support screenshots.`); }); } eval(js) { return __awaiter(this, void 0, void 0, function* () { throw `This element does not support eval().`; }); } focus() { return __awaiter(this, void 0, void 0, function* () { throw `This element does not support focus().`; }); } hover() { return __awaiter(this, void 0, void 0, function* () { throw `This element does not support hover().`; }); } blur() { return __awaiter(this, void 0, void 0, function* () { throw `This element does not support blur().`; }); } tap() { return __awaiter(this, void 0, void 0, function* () { throw `This element does not support tap().`; }); } press(key, opts) { return __awaiter(this, void 0, void 0, function* () { throw `This element does not support press().`; }); } clearThenType(textToType, opts) { return __awaiter(this, void 0, void 0, function* () { throw `This element does not support clearThenType().`; }); } type(textToType, opts) { return __awaiter(this, void 0, void 0, function* () { throw `This element does not support type().`; }); } clear() { return __awaiter(this, void 0, void 0, function* () { throw `This element does not support clear().`; }); } getAncestor(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getAncestor() is not supported by ${this.name}`; }); } getChildren(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getChildren() is not supported by ${this.name}`; }); } getAncestors(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getAncestors() is not supported by ${this.name}`; }); } getAncestorOrSelf(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getAncestorOrSelf() is not supported by ${this.name}`; }); } getFirstChild(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getFirstChild() is not supported by ${this.name}`; }); } getLastChild(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getLastChild() is not supported by ${this.name}`; }); } getFirstSibling(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getFirstSibling() is not supported by ${this.name}`; }); } getLastSibling(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getLastSibling() is not supported by ${this.name}`; }); } getChildOrSelf(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getChildOrSelf() is not supported by ${this.name}`; }); } getDescendantOrSelf(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getDescendantOrSelf() is not supported by ${this.name}`; }); } getDescendants(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getDescendants() is not supported by ${this.name}`; }); } getParent() { return __awaiter(this, void 0, void 0, function* () { throw `getParent() is not supported by ${this.name}`; }); } getSiblings(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getSiblings() is not supported by ${this.name}`; }); } getPreviousSibling(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getPreviousSibling() is not supported by ${this.name}`; }); } getPreviousSiblings(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getPreviousSiblings() is not supported by ${this.name}`; }); } getNextSibling(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getNextSibling() is not supported by ${this.name}`; }); } getNextSiblings(selector) { return __awaiter(this, void 0, void 0, function* () { throw `getNextSiblings() is not supported by ${this.name}`; }); } getBounds(boxType) { return __awaiter(this, void 0, void 0, function* () { return null; }); } download(a, b) { return __awaiter(this, void 0, void 0, function* () { const link = yield this.getLink(); if (!link.isNavigation()) { return null; } 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 request = new httprequest_1.HttpRequest(Object.assign({ uri: link.getUri(), method: "get", }, opts)); const resp = yield request.fetch(); if (localFilePath) { fs.writeFileSync(localFilePath, resp.body); } return resp; }); } waitForFunction(js, opts, ...args) { return __awaiter(this, void 0, void 0, function* () { return this; }); } waitForHidden() { return __awaiter(this, void 0, void 0, function* () { return this; }); } waitForVisible() { return __awaiter(this, void 0, void 0, function* () { return this; }); } setValue(text) { return __awaiter(this, void 0, void 0, function* () { throw `setValue() is not supported by ${this.name}`; }); } assert(message) { return typeof message == "string" ? this.context.assert(message, this) : this.context.assert(this); } split(by, limit) { return new Value(this.toString().split(by, limit), this._context, this.name); } join(by) { return new Value(this.toArray().join(by), this._context, this.name); } map(callback) { return new Value(this.isArray() ? this.toArray().map(callback) : callback(this._input), this._context, this.name); } filter(func) { return new Value(this.toArray().filter(func), this._context, this.name); } each(callback) { this.toArray().forEach(callback); return this; } min(key) { return new Value(this.toArray().reduce((min, row) => { const val = key ? row[key] : row; return min === null || val < min ? val : min; }, null), this._context, this.name); } max(key) { return new Value(this.toArray().reduce((max, row) => { const val = key ? row[key] : row; return max === null || val > max ? val : max; }, null), this._context, this.name); } sum(key) { return new Value(this.toArray().reduce((sum, row) => (sum += Number(key ? row[key] : row)), 0), this._context, this.name); } count(key) { return new Value(this.toArray().reduce((count, row) => { if (key) { return count + !!row[key] ? 1 : 0; } return count + 1; }, 0), this._context, this.name); } unique() { return new Value([...new Set(this.toArray())], this._context, this.name); } groupBy(key) { return new Value(this.toArray().reduce((grouper, row) => { const val = row[key]; if (!grouper[val]) { grouper[val] = []; } grouper[val].push(row); return grouper; }, {}), this._context, this.name); } asc(key) { const collator = new Intl.Collator("en", { numeric: true, sensitivity: "base", }); const arr = this.toArray().sort((a, b) => key ? collator.compare(a[key], b[key]) : collator.compare(a, b)); return new Value(arr, this._context, this.name); } desc(key) { const collator = new Intl.Collator("en", { numeric: true, sensitivity: "base", }); const arr = this.toArray().sort((a, b) => (key ? collator.compare(a[key], b[key]) : collator.compare(a, b)) * -1); return new Value(arr, this._context, this.name); } median(key) { const arr = this.toArray().sort((a, b) => key ? parseFloat(a[key]) - parseFloat(b[key]) : a - b); const med = arr[Math.floor(arr.length / 2)]; return new Value(key ? med[key] : med, this._context, this.name); } avg(key) { const arr = this.toArray(); return new Value(arr.reduce((sum, row) => (sum += Number(key ? row[key] : row)), 0) / arr.length, this._context, this.name); } reduce(callback, initial) { return new Value(this.toArray().reduce(callback, initial), this._context, this.name); } every(callback) { return new Value(this.toArray().every(callback), this._context, this.name); } some(callback) { return new Value(this.toArray().some(callback), this._context, this.name); } none(callback) { return new Value(!this.toArray().some(callback), this._context, this.name); } item(key) { const name = `${key} in ${this.name}`; if (typeof key === "string") { return new Value(jpath_1.jpathSearch(this._input, key), this._context, name); } if (this._input.hasOwnProperty(key)) { return new Value(this._input[key], this._context, name); } return new Value(null, this._context, name); } echo(callback) { this._context.comment(callback ? callback(this.toString()) : this.toString()); return this; } col(key) { if (Array.isArray(key)) { const name = `${key.join(", ")} in ${this.name}`; return new Value(this.toArray().map((row) => { let out = []; key.forEach((k) => { out.push(row[k]); }); return out; }), this._context, name); } const name = `${key} in ${this.name}`; return new Value(this.toArray().map((row) => row[key]), this._context, name); } _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)); }); } _wrapAsValue(data, name, parent, highlight) { const val = new Value(data, this._context, name, parent, highlight); if (!val.sourceCode && parent && parent.sourceCode) { val._sourceCode = parent.sourceCode; } return val; } } exports.Value = Value; //# sourceMappingURL=value.js.map