UNPKG

flagpole

Version:

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

263 lines 8.49 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 }); const util_1 = require("./util"); const assertionresult_1 = require("./logging/assertionresult"); 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 $() { return this._input; } get tagName() { return this._tagName || ""; } get outerHTML() { return this._sourceCode || ""; } get length() { const thisValue = this.$; return new Value(thisValue && thisValue.length ? thisValue.length : 0, this._context, `Length 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; } 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); } 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) { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(this._input && this._input.hasOwnProperty && this._input.hasOwnProperty(key), `${this.name} has property ${key}`); }); } as(aliasName) { this._context.scenario.set(aliasName, this); return this; } getProperty(key) { return __awaiter(this, void 0, void 0, function* () { if (!this.isNullOrUndefined() && this.hasProperty(key)) { return this._input[key]; } return undefined; }); } click(a, b) { return __awaiter(this, void 0, void 0, function* () { }); } submit(a, b) { return __awaiter(this, void 0, void 0, function* () { }); } load(a, b) { } fillForm(formData) { return __awaiter(this, void 0, void 0, function* () { }); } 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(className) { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(false, `${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.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) { return this.hasProperty(key); } getAttribute(key) { return this.getProperty(key); } hasData(key) { return this.hasProperty(key); } getData(key) { return this.getProperty(key); } getStyleProperty(key) { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(null, `Style of ${key}`); }); } download() { return __awaiter(this, void 0, void 0, function* () { throw new Error("Download is not supported on this value type."); }); } getValue() { return __awaiter(this, void 0, void 0, function* () { return this; }); } getText() { return __awaiter(this, void 0, void 0, function* () { return this._wrapAsValue(this.toString(), this.name, this.parent, 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.`); }); } _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