flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
326 lines • 14.6 kB
JavaScript
"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.HTMLElement = void 0;
const dom_element_1 = require("./dom-element");
const util_1 = require("../util");
const helpers_1 = require("../helpers");
const value_promise_1 = require("../value-promise");
const http_request_1 = require("../http/http-request");
let $;
class HTMLElement extends dom_element_1.DOMElement {
constructor(input, context, name, path) {
super(input, context, name || "HTML Element");
this._path = path || "";
$ = context.response.getRoot();
}
get $() {
return this._input;
}
get el() {
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;
});
}
find(selector, a, b) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const params = helpers_1.getFindParams(a, b);
const name = `${selector} under ${this.name}`;
const path = `${this.path} ${selector}`;
if (params.contains || params.matches) {
}
else {
const element = this.el.find(selector).eq(0);
if (element === null || element === void 0 ? void 0 : element.length) {
return HTMLElement.create(element, this.context, name, path);
}
}
return this._wrapAsValue(null, name);
}));
}
findAll(selector, a, b) {
return __awaiter(this, void 0, void 0, function* () {
const params = helpers_1.getFindParams(a, b);
const out = [];
const elements = this.el.find(selector).toArray();
yield util_1.asyncForEach(elements, (element, i) => __awaiter(this, void 0, void 0, function* () {
return out.push(yield HTMLElement.create(element, this.context, `${selector}[${i}] under ${this.name}`, `${this.path} ${selector}[${i}]`));
}));
return helpers_1.filterFind(out, params.contains || params.matches, params.opts);
});
}
getAncestorOrSelf(selector) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const closest = this.el.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);
}));
}
getFirstChild(selector) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const child = this.el.children(selector).first();
return HTMLElement.create(child, this.context, `First Child ${selector} of ${this.name}`, `${this.path}[child::${selector}][1]`);
}));
}
getLastChild(selector) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const child = this.el.children(selector).last();
return HTMLElement.create(child, this.context, `First Child ${selector} of ${this.name}`, `${this.path}[child::${selector}][1]`);
}));
}
getChildren(selector = "*") {
return __awaiter(this, void 0, void 0, function* () {
const children = this.el.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 = this.el.siblings(selector).toArray();
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;
});
}
getFirstSibling(selector) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const child = this.el.siblings(selector).first();
return HTMLElement.create(child, this.context, `First sibling ${selector}} of ${this.name}`, `${this.path}[sibling::${selector}][1]`);
}));
}
getLastSibling(selector) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const child = this.el.siblings(selector).last();
return HTMLElement.create(child, this.context, `Last sibling ${selector}} of ${this.name}`, `${this.path}[sibling::${selector}][last()]`);
}));
}
getAncestor(selector = "*") {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const ancestors = this.el.parentsUntil(selector);
const name = `Ancestor of ${this.name}`;
const path = `${this.path}[ancestor::${selector}][0]`;
return ancestors.length > 0
? HTMLElement.create(ancestors[0], this.context, name, path)
: this._wrapAsValue(null, name, this);
}));
}
getParent() {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const parent = this.el.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 value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const siblings = this.el.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 = this.el.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 value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const siblings = this.el.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 = this.el.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() {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
if (yield this._isLinkTag()) {
const link = yield this.getLink();
if (link.isNavigation()) {
this._completedAction("CLICK");
this.context.response.navigate(new http_request_1.HttpRequest({
uri: link.getUri(),
method: "get",
}));
return this;
}
}
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);
this._completedAction("CLICK");
formEl.submit();
return this;
}
}
this.context.logFailure(`${this.name} is not a clickable element.`);
return this;
}));
}
fillForm(a, b) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
if (!(yield this._isFormTag())) {
throw new Error("This is not a form element.");
}
const attributeName = typeof a === "string" ? a : "name";
const formData = (typeof a === "string" ? b : a) || {};
const form = this.el;
for (const name in formData) {
const value = formData[name];
const selector = `[${attributeName}="${name}"]`;
const field = form.find(selector);
if (field.length == 0) {
this.context.logOptionalFailure(`Could not set form field ${name} to ${value}, because the field did not exist.`, selector);
}
else {
field.val(value);
}
}
this._completedAction("FILL");
return this;
}));
}
submit() {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
if (!(yield this._isFormTag())) {
throw new Error("You can only use .submit() with a form element.");
}
const link = yield this.getLink();
if (link.isNavigation()) {
const method = ((yield this._getAttribute("method")) || "get")
.toString()
.toLowerCase();
if (method == "get") {
link.setQueryString(this.el.serializeArray());
}
const request = new http_request_1.HttpRequest({
uri: link.getUri(),
method: method,
});
if (method != "get") {
const formDataArray = this.el.serializeArray();
const formData = {};
formDataArray.forEach(function (input) {
formData[input.name] = input.value;
});
request.setFormData(formData);
}
this._completedAction("SUBMIT");
this.context.response.navigate(request);
return this;
}
this.context.logFailure(`This element could not be submitted: ${this.name}`);
return this;
}));
}
_getText() {
return __awaiter(this, void 0, void 0, function* () {
return this.el.text();
});
}
_getValue() {
return __awaiter(this, void 0, void 0, function* () {
return this.el.val();
});
}
_getProperty(key) {
return __awaiter(this, void 0, void 0, function* () {
return this.el.prop(key);
});
}
_getInnerText() {
return __awaiter(this, void 0, void 0, function* () {
return this.el.text();
});
}
_getInnerHtml() {
return __awaiter(this, void 0, void 0, function* () {
return this.el.html() || "";
});
}
_getOuterHtml() {
return __awaiter(this, void 0, void 0, function* () {
return this.context.response.getRoot().html(this._input);
});
}
_getClassName() {
return __awaiter(this, void 0, void 0, function* () {
return typeof this.el.get(0).attribs["class"] !== "undefined"
? this.el.get(0).attribs["class"]
: null;
});
}
_getTagName() {
return __awaiter(this, void 0, void 0, function* () {
return this.el.get(0).tagName.toLowerCase();
});
}
_getAttribute(key) {
return __awaiter(this, void 0, void 0, function* () {
return typeof this.el.get(0).attribs[key] !== "undefined"
? this.el.get(0).attribs[key]
: null;
});
}
}
exports.HTMLElement = HTMLElement;
//# sourceMappingURL=html-element.js.map