flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
111 lines • 4.32 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.HtmlResponse = void 0;
const htmlelement_1 = require("./htmlelement");
const domresponse_1 = require("./domresponse");
const cheerio = require("cheerio");
const util_1 = require("./util");
class HtmlResponse extends domresponse_1.DOMResponse {
constructor() {
super(...arguments);
this._cheerio = null;
}
set cheerio(value) {
this._cheerio = value;
}
get cheerio() {
if (this._cheerio === null) {
throw "Cheerio root element is null";
}
return this._cheerio;
}
get responseTypeName() {
return "HTML";
}
get responseType() {
return "html";
}
init(httpResponse) {
super.init(httpResponse);
this._cheerio = cheerio.load(httpResponse.body);
}
getRoot() {
return this.cheerio;
}
eval() {
return __awaiter(this, void 0, void 0, function* () {
throw "This type of scenario does not suport eval.";
});
}
find(selector, a, b) {
return __awaiter(this, void 0, void 0, function* () {
const params = util_1.getFindParams(a, b);
if (params.contains || params.matches || params.opts) {
return util_1.findOne(this, selector, params);
}
const selection = this.cheerio(selector);
return selection.length > 0
? yield htmlelement_1.HTMLElement.create(selection.eq(0), this.context, null, selector)
: util_1.wrapAsValue(this.context, null, selector);
});
}
findAll(selector, a, b) {
return __awaiter(this, void 0, void 0, function* () {
const response = this;
const elements = this.cheerio(selector);
const params = util_1.getFindParams(a, b);
let nodeElements = [];
if (elements.length > 0) {
for (let i = 0; i < elements.length; i++) {
nodeElements.push(yield htmlelement_1.HTMLElement.create(this.cheerio(elements.get(i)), response.context, `${selector} [${i}]`, selector));
}
if (params.opts || params.contains || params.matches) {
nodeElements = yield util_1.filterFind(nodeElements, params.contains || params.matches, params.opts);
}
}
return nodeElements;
});
}
waitForHidden(selector, timeout = 100) {
return __awaiter(this, void 0, void 0, function* () {
return this.find(selector);
});
}
waitForVisible(selector, timeout = 100) {
return __awaiter(this, void 0, void 0, function* () {
return this.find(selector);
});
}
waitForExists(selector, timeout = 100) {
return __awaiter(this, void 0, void 0, function* () {
return this.find(selector);
});
}
waitForHavingText(selector, text, timeout = 100) {
return __awaiter(this, void 0, void 0, function* () {
return this.findHavingText(selector, text);
});
}
type(selector, textToType, opts = {}) {
return __awaiter(this, void 0, void 0, function* () {
const currentValue = this.cheerio(selector).val();
this.cheerio(selector).val(currentValue + textToType);
});
}
clear(selector) {
return __awaiter(this, void 0, void 0, function* () {
this.cheerio(selector).val("");
});
}
}
exports.HtmlResponse = HtmlResponse;
//# sourceMappingURL=htmlresponse.js.map