flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
84 lines • 3.6 kB
JavaScript
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 html_element_1 = require("./html-element");
const cheerio = require("cheerio");
const helpers_1 = require("../helpers");
const value_promise_1 = require("../value-promise");
const response_1 = require("../response");
class HtmlResponse extends response_1.ProtoResponse {
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;
}
init(res) {
super.init(res);
this._cheerio = cheerio.load(res.body);
}
getRoot() {
return this.cheerio;
}
find(selector, a, b) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const params = helpers_1.getFindParams(a, b);
if (params.contains || params.matches || params.opts) {
return helpers_1.findOne(this, selector, params);
}
const selection = this.cheerio(selector);
return selection.length > 0
? yield html_element_1.HTMLElement.create(selection.eq(0), this.context, null, selector)
: helpers_1.wrapAsValue(this.context, null, selector);
}));
}
findAll(selector, a, b) {
return __awaiter(this, void 0, void 0, function* () {
const elements = this.cheerio(selector).toArray();
const params = helpers_1.getFindParams(a, b);
let nodeElements = [];
if (elements.length > 0) {
for (let i = 0; i < elements.length; i++) {
nodeElements.push(yield html_element_1.HTMLElement.create(elements[i], this.context, `${selector} [${i}]`, selector));
}
if (params.opts || params.contains || params.matches) {
nodeElements = (yield helpers_1.filterFind(nodeElements, params.contains || params.matches, params.opts));
}
}
return nodeElements;
});
}
type(selector, textToType, opts = {}) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const el = yield this.find(selector);
const currentValue = yield el.getValue();
el.setValue(currentValue + textToType);
return el;
}));
}
clear(selector) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const el = yield this.find(selector);
el.setValue("");
return el;
}));
}
}
exports.HtmlResponse = HtmlResponse;
//# sourceMappingURL=html-response.js.map
;