flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
94 lines • 3.52 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 });
const htmlelement_1 = require("./htmlelement");
const domresponse_1 = require("./domresponse");
const enums_1 = require("./enums");
const value_1 = require("./value");
const cheerio = require("cheerio");
let $;
class HtmlResponse extends domresponse_1.DOMResponse {
get responseTypeName() {
return "HTML";
}
get responseType() {
return enums_1.ResponseType.html;
}
init(httpResponse) {
super.init(httpResponse);
$ = cheerio.load(httpResponse.body);
}
getRoot() {
return $;
}
evaluate(context, callback) {
return __awaiter(this, void 0, void 0, function* () {
return callback.apply(context, [$]);
});
}
find(path) {
return __awaiter(this, void 0, void 0, function* () {
const selection = $(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 = $(path);
if (elements.length > 0) {
const nodeElements = [];
for (let i = 0; i < elements.length; i++) {
nodeElements.push(yield htmlelement_1.HTMLElement.create($(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);
});
}
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