flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
445 lines • 18.3 kB
JavaScript
"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 value_1 = require("./value");
const link_1 = require("./link");
const response_1 = require("./response");
class NodeElement extends value_1.ProtoValue {
constructor(input, context, path) {
super(input, context);
this._path = path || '';
}
getClassName() {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement()) {
return (typeof this._input.get(0).attribs['class'] !== 'undefined') ?
this._input.get(0).attribs['class'] : null;
}
else if (this._isPuppeteerElement()) {
const classHandle = yield this._input.getProperty('className');
return yield classHandle.jsonValue();
}
throw new Error(`getClassName is not supported with ${this.toType()}.`);
});
}
hasClassName(className) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement()) {
return this._input.hasClass(className);
}
else if (this._isPuppeteerElement()) {
const classHandle = yield this._input.getProperty('className');
const classString = yield classHandle.jsonValue();
return (classString.split(' ').indexOf(className) >= 0);
}
throw new Error(`hasClassName is not supported with ${this.toType()}.`);
});
}
getTagName() {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement()) {
return this._input.get(0).tagName.toLowerCase();
}
else if (this._isPuppeteerElement()) {
const handle = yield this._input.getProperty('tagName');
return String(yield handle.jsonValue()).toLowerCase();
}
throw new Error(`getTagName is not supported with ${this.toType()}.`);
});
}
hasAttribute(key) {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.getAttribute(key)) !== null;
});
}
getAttribute(key) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement()) {
return (typeof this._input.get(0).attribs[key] !== 'undefined') ?
this._input.get(0).attribs[key] : null;
}
else if (this._isPuppeteerElement()) {
const handle = yield this._input.getProperty(key);
return yield handle.jsonValue();
}
else if (!this.isNullOrUndefined() && this.hasProperty(key)) {
return this._input[key];
}
throw new Error(`getAttribute is not supported with ${this.toType()}.`);
});
}
getProperty(key) {
return __awaiter(this, void 0, void 0, function* () {
let text;
if (this._isCheerioElement()) {
return this._input.prop(key);
}
else if (this._isPuppeteerElement()) {
const handle = yield this._input.getProperty(key);
return yield handle.jsonValue();
}
else if (!this.isNullOrUndefined() && this.hasProperty(key)) {
return this._input[key];
}
throw new Error(`getProperty is not supported with ${this.toType()}.`);
});
}
hasData(key) {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.getData(key)) !== null;
});
}
getData(key) {
return __awaiter(this, void 0, void 0, function* () {
let text;
if (this._isCheerioElement()) {
return this._input.data(key);
}
else if (this._isPuppeteerElement()) {
const handle = yield this._input.getProperty(key);
return yield handle.jsonValue();
}
else if (!this.isNullOrUndefined() && this.hasProperty(key)) {
return this._input[key];
}
throw new Error(`getData is not supported with ${this.toType()}.`);
});
}
getValue() {
return __awaiter(this, void 0, void 0, function* () {
let text;
if (this._isCheerioElement()) {
return this._input.val();
}
else if (this._isPuppeteerElement()) {
const handle = yield this._input.getProperty('value');
return yield handle.jsonValue();
}
else if (!this.isNullOrUndefined()) {
return this._input;
}
throw new Error(`getValue is not supported with ${this.toType()}.`);
});
}
getText() {
return __awaiter(this, void 0, void 0, function* () {
let text;
if (this._isCheerioElement()) {
return this._input.text();
}
else if (this._isPuppeteerElement()) {
const handle = yield this._input.getProperty('textContent');
return yield handle.jsonValue();
}
else if (!this.isNullOrUndefined()) {
return this._input;
}
throw new Error(`getText is not supported with ${this.toType()}.`);
});
}
fillForm(formData) {
return __awaiter(this, void 0, void 0, function* () {
const element = this;
const isForm = yield this._isFormTag();
if (isForm) {
if (element._isCheerioElement()) {
const form = element._input;
for (let name in formData) {
const value = formData[name];
form.find(`[name="${name}"]`).val(value);
}
}
else if (element._isPuppeteerElement()) {
const page = element._context.page;
if (page !== null) {
for (let name in formData) {
const value = formData[name];
const selector = `${element._path} [name="${name}"]`;
const input = yield page.$(selector);
if (input !== null) {
const tagName = (yield (yield input.getProperty('tagName')).jsonValue()).toLowerCase();
const inputType = (yield (yield input.getProperty('type')).jsonValue()).toLowerCase();
yield page.focus(selector);
if (tagName == 'select') {
yield page.select(selector, value);
}
else if (tagName == 'input') {
yield page.type(selector, value);
}
}
}
}
}
return element;
}
else {
throw new Error('This is not a form element.');
}
});
}
getChildren(selector) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement()) {
return this._input.children(selector);
}
throw new Error(`getChildren is not supported with ${this.toType()}.`);
});
}
getNext(selector) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement()) {
return new NodeElement(this._input.next(selector), this._context, selector);
}
throw new Error(`getNext is not supported with ${this.toType()}.`);
});
}
getPrevious(selector) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement()) {
return new NodeElement(this._input.prev(selector), this._context, selector);
}
throw new Error(`getPrevious is not supported with ${this.toType()}.`);
});
}
getSiblings(selector) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement()) {
return new NodeElement(this._input.siblings(selector), this._context, selector);
}
throw new Error(`getSiblings is not supported with ${this.toType()}.`);
});
}
getClosest(selector) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement()) {
if (typeof selector != 'undefined') {
return new NodeElement(this._input.closest(selector), this._context, selector);
}
}
throw new Error(`getClosest is not supported with ${this.toType()}.`);
});
}
getParent(selector) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement()) {
return new NodeElement(this._input.parent(selector), this._context, selector);
}
throw new Error(`getParent is not supported with ${this.toType()}.`);
});
}
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.');
}
if (this._isPuppeteerElement()) {
if (this._context.page === null) {
throw new Error('Page was null');
}
return yield this._context.page.evaluate(form => form.submit(), this.get());
}
else if (this._isCheerioElement()) {
}
throw new Error('This is not supported yet.');
});
}
click(a, b) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isPuppeteerElement()) {
return yield this._context.click(this._path);
}
if (yield this._isLinkTag()) {
return yield this.load(a, b);
}
if (yield this._isButtonTag()) {
}
throw Error('This is not a clickable element.');
});
}
load(a, b) {
return __awaiter(this, void 0, void 0, function* () {
const link = yield this._getLink();
const scenario = yield this._createLambdaScenario(a, b);
if (link.isNavigation()) {
scenario.title = (typeof a == 'string') ? a : `Load ${link.getUri()}`;
setTimeout(() => {
scenario.open(link.getUri());
}, 1);
}
else {
scenario.skip('Not a navigational link');
}
return scenario;
});
}
_isFormTag() {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement() || this._isPuppeteerElement()) {
return (yield this.getTagName()) == 'form';
}
return false;
});
}
_isButtonTag() {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement() || this._isPuppeteerElement()) {
const tagName = yield this.getTagName();
const type = yield this.getAttribute('type');
return (tagName === 'button' ||
(tagName === 'input' && (['button', 'submit', 'reset'].indexOf(type) >= 0)));
}
return false;
});
}
_isLinkTag() {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement() || this._isPuppeteerElement()) {
return ((yield this.getTagName()) === 'a' &&
(yield this.getAttribute('href')) !== null);
}
return false;
});
}
_isImageTag() {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement() || this._isPuppeteerElement()) {
return ((yield this.getTagName()) === 'img' &&
(yield this.getAttribute('src')) !== null);
}
return false;
});
}
_isVideoTag() {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement() || this._isPuppeteerElement()) {
const tagName = yield this.getTagName();
const src = yield this.getAttribute('src');
const type = yield this.getAttribute('type');
return ((tagName === 'video' && src !== null) ||
(tagName === 'source' && src !== null && /video/i.test(type || '')));
}
return false;
});
}
_isAudioTag() {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement() || this._isPuppeteerElement()) {
const tagName = yield this.getTagName();
const src = yield this.getAttribute('src');
const type = yield this.getAttribute('type');
return ((tagName === 'audio' && src !== null) ||
(tagName === 'bgsound' && src !== null) ||
(tagName === 'source' && src !== null && /audio/i.test(type || '')));
}
return false;
});
}
_isScriptTag() {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement() || this._isPuppeteerElement()) {
return ((yield this.getTagName()) === 'script' &&
(yield this.getAttribute('src')) !== null);
}
return false;
});
}
_isStylesheetTag() {
return __awaiter(this, void 0, void 0, function* () {
if (this._isCheerioElement() || this._isPuppeteerElement()) {
return ((yield this.getTagName()) === 'link' &&
(yield this.getAttribute('href')) !== null &&
(yield this.getAttribute('rel')).toLowerCase() == 'stylesheet');
}
return false;
});
}
_isClickable() {
return __awaiter(this, void 0, void 0, function* () {
return ((yield this._isLinkTag()) ||
(yield this._isButtonTag()));
});
}
_getUrl() {
return __awaiter(this, void 0, void 0, function* () {
const tagName = yield this.getTagName();
if (this._isCheerioElement() || this._isPuppeteerElement()) {
if (tagName !== null) {
if (['img', 'script', 'video', 'audio', 'object', 'iframe'].indexOf(tagName) >= 0) {
return this.getAttribute('src');
}
else if (['a', 'link'].indexOf(tagName) >= 0) {
return this.getAttribute('href');
}
else if (['form'].indexOf(tagName) >= 0) {
return this.getAttribute('action') || this._context.scenario.getUrl();
}
else if (['source'].indexOf(tagName) >= 0) {
return this.getAttribute('src');
}
}
}
return null;
});
}
_getLambdaScenarioType() {
return __awaiter(this, void 0, void 0, function* () {
if ((yield this._isFormTag()) || (yield this._isClickable())) {
return (this._context.scenario.responseType == response_1.ResponseType.browser) ?
'browser' : 'html';
}
else if (yield this._isImageTag()) {
return 'image';
}
else if (yield this._isStylesheetTag()) {
return 'stylesheet';
}
else if (yield this._isScriptTag()) {
return 'script';
}
else if (yield this._isVideoTag()) {
return 'video';
}
else {
return 'resource';
}
});
}
_getLink() {
return __awaiter(this, void 0, void 0, function* () {
const srcPath = yield this._getUrl();
return new link_1.Link(srcPath || '', this._context);
});
}
_createLambdaScenario(a, b) {
return __awaiter(this, void 0, void 0, function* () {
const title = typeof a == 'string' ? a : this._path;
const scenario = this._context.suite.Scenario(title);
const scenarioType = yield this._getLambdaScenarioType();
const callback = (function () {
if (typeof b == 'function') {
return b;
}
else if (typeof a == 'function') {
return a;
}
else {
return function () { };
}
})();
const opts = ((scenarioType == 'browser' && this._context.scenario.responseType == response_1.ResponseType.browser) ||
scenarioType != 'browser') ? this._context.scenario.getRequestOptions() : {};
scenario[scenarioType](opts);
scenario.next(callback);
return scenario;
});
}
}
exports.NodeElement = NodeElement;
//# sourceMappingURL=nodeelement.js.map