flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
110 lines • 4.02 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 });
const htmlelement_1 = require("./htmlelement");
const domresponse_1 = require("./domresponse");
const value_1 = require("./value");
const cheerio = require("cheerio");
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;
}
evaluate(context, callback) {
return __awaiter(this, void 0, void 0, function* () {
return callback.apply(context, [this.cheerio]);
});
}
find(path) {
return __awaiter(this, void 0, void 0, function* () {
const selection = this.cheerio(path);
if (selection.length > 0) {
return yield htmlelement_1.HTMLElement.create(selection.eq(0), this.context, null, path);
}
return new value_1.Value(null, this.context, path);
});
}
findAll(path) {
return __awaiter(this, void 0, void 0, function* () {
const response = this;
const elements = this.cheerio(path);
if (elements.length > 0) {
const nodeElements = [];
for (let i = 0; i < elements.length; i++) {
nodeElements.push(yield htmlelement_1.HTMLElement.create(this.cheerio(elements.get(i)), response.context, `${path} [${i}]`, path));
}
return nodeElements;
}
else {
return [];
}
});
}
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* () {
return yield this.evaluate(this, function ($) {
let currentValue = $(selector).val();
$(selector).val(currentValue + textToType);
});
});
}
clear(selector) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.evaluate(this, function ($) {
$.find(selector).val("");
});
});
}
}
exports.HtmlResponse = HtmlResponse;
//# sourceMappingURL=htmlresponse.js.map