flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
91 lines • 4.33 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.delay = exports.applyOffsetAndLimit = exports.filterFind = exports.getFindParams = exports.getFindName = exports.findOne = exports.wrapAsValue = void 0;
const value_1 = require("./value");
const util_1 = require("./util");
function wrapAsValue(context, data, name, source) {
return new value_1.Value(data, context, name, source);
}
exports.wrapAsValue = wrapAsValue;
function findOne(scope, selector, params) {
return __awaiter(this, void 0, void 0, function* () {
const opts = Object.assign(Object.assign({}, params.opts), { limit: 1 });
const elements = params.contains !== null
? yield scope.findAll(selector, params.contains, opts)
: params.matches !== null
? yield scope.findAll(selector, params.matches, opts)
: yield scope.findAll(selector, opts);
return elements[0] || wrapAsValue(scope.context, null, selector);
});
}
exports.findOne = findOne;
function getFindName(params, selector, i) {
const selectors = typeof selector == "string" ? [selector] : selector;
const name = selectors.length > 1 ? selectors.join("|") : selector;
return params.contains
? `${name} containing "${params.contains}"`
: params.matches
? `${name} matching ${String(params.matches)}`
: i === null
? `${name}`
: `${name}[${i}]`;
}
exports.getFindName = getFindName;
function getFindParams(a, b) {
const contains = typeof a === "string" ? a : null;
const matches = util_1.toType(a) === "regexp" ? a : null;
const opts = (contains || matches ? b : a || b) || null;
return { contains: contains, matches: matches, opts: opts };
}
exports.getFindParams = getFindParams;
function filterFind(elements, contains, opts) {
return __awaiter(this, void 0, void 0, function* () {
const containsType = util_1.toType(contains);
if (opts === undefined) {
return elements;
}
if (contains) {
elements = yield util_1.asyncFilter(elements, (element) => __awaiter(this, void 0, void 0, function* () {
const text = yield (() => __awaiter(this, void 0, void 0, function* () {
if ((opts === null || opts === void 0 ? void 0 : opts.findBy) == "value") {
return (yield element.getValue()).$;
}
if ((opts === null || opts === void 0 ? void 0 : opts.findBy) == "html") {
return (yield element.getOuterHtml()).$;
}
return (yield element.getText()).$;
}))();
if (containsType == "regexp") {
return contains.test(text.toString());
}
return text.toLowerCase().indexOf(String(contains).toLowerCase()) >= 0;
}));
}
if ((opts === null || opts === void 0 ? void 0 : opts.offset) || (opts === null || opts === void 0 ? void 0 : opts.limit)) {
elements = applyOffsetAndLimit(opts, elements);
}
return elements;
});
}
exports.filterFind = filterFind;
function applyOffsetAndLimit(opts, elements) {
const start = opts.offset || 0;
const end = opts.limit ? start + opts.limit : undefined;
elements = elements.slice(start, end);
return elements;
}
exports.applyOffsetAndLimit = applyOffsetAndLimit;
const delay = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
exports.delay = delay;
//# sourceMappingURL=helpers.js.map
;