UNPKG

flagpole

Version:

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

600 lines 20.1 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 httprequest_1 = require("./httprequest"); const fs = require("fs"); 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 || ""; } 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 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()); } 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 __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(null, selector); }); } findAll(selector) { return __awaiter(this, void 0, void 0, function* () { return []; }); } 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)).toString(); return value === undefined ? !!thisValue : typeof value == "string" ? value == thisValue : value.test(thisValue); }); } 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.values(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}`; }); } _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