UNPKG

flagpole

Version:

Simple and fast DOM integration, headless or headful browser, and REST API testing framework.

114 lines 5.57 kB
"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.BrowserResponse = void 0; const puppeteerresponse_1 = require("./puppeteerresponse"); const util_1 = require("../util"); const helpers_1 = require("../helpers"); const browserelement_1 = require("./browserelement"); class BrowserResponse extends puppeteerresponse_1.PuppeteerResponse { get responseTypeName() { return "Browser"; } get responseType() { return "browser"; } find(selector, a, b) { return __awaiter(this, void 0, void 0, function* () { const params = helpers_1.getFindParams(a, b); if (params.opts || params.matches || params.contains) { return helpers_1.findOne(this, selector, params); } const el = yield this._page.$(selector); return el === null ? helpers_1.wrapAsValue(this.context, null, selector) : browserelement_1.BrowserElement.create(el, this.context, selector, selector); }); } findAll(selector, a, b) { return __awaiter(this, void 0, void 0, function* () { const params = helpers_1.getFindParams(a, b); const elements = yield util_1.asyncMap(yield this._page.$$(selector), (el, i) => __awaiter(this, void 0, void 0, function* () { return yield browserelement_1.BrowserElement.create(el, this.context, helpers_1.getFindName(params, selector, i), selector); })); return helpers_1.filterFind(elements, params.contains || params.matches, params.opts); }); } findXPath(xPath) { return __awaiter(this, void 0, void 0, function* () { const elements = yield this._page.$x(xPath); if (elements.length > 0) { return yield browserelement_1.BrowserElement.create(elements[0], this.context, xPath, xPath); } return helpers_1.wrapAsValue(this.context, null, xPath); }); } findAllXPath(xPath) { return __awaiter(this, void 0, void 0, function* () { const response = this; const out = []; const elements = yield this._page.$x(xPath); yield util_1.asyncForEach(elements, (el, i) => __awaiter(this, void 0, void 0, function* () { const element = yield browserelement_1.BrowserElement.create(el, response.context, `${xPath} [${i}]`, xPath); out.push(element); })); return out; }); } waitForHidden(selector, timeout = 100) { return __awaiter(this, void 0, void 0, function* () { const opts = { timeout: timeout || 100, hidden: true }; const element = yield this._page.waitForSelector(selector, opts); return browserelement_1.BrowserElement.create(element, this.context, selector, selector); }); } waitForVisible(selector, timeout = 100) { return __awaiter(this, void 0, void 0, function* () { const opts = { timeout: timeout || 100, visible: true }; const element = yield this._page.waitForSelector(selector, opts); return browserelement_1.BrowserElement.create(element, this.context, selector, selector); }); } waitForExists(selector, timeout) { return __awaiter(this, void 0, void 0, function* () { const opts = { timeout: timeout || 100 }; const element = yield this._page.waitForSelector(selector, opts); return browserelement_1.BrowserElement.create(element, this.context, selector, selector); }); } waitForXPath(xPath, timeout) { return __awaiter(this, void 0, void 0, function* () { const opts = { timeout: timeout || 100 }; const element = yield this._page.waitForXPath(xPath, opts); return browserelement_1.BrowserElement.create(element, this.context, xPath, xPath); }); } waitForHavingText(selector, text, timeout) { return __awaiter(this, void 0, void 0, function* () { const opts = { timeout: timeout || 100 }; const element = (yield this._page.waitForFunction(`document.querySelector("${selector}").innerText.includes("${text}")`, opts)).asElement(); if (element === null) { throw `Element ${selector} did not exist after timeout.`; } return browserelement_1.BrowserElement.create(element, this.context, selector, selector); }); } selectOption(selector, value) { return __awaiter(this, void 0, void 0, function* () { yield this._page.select.apply(this.page, [ selector, ...util_1.arrayify(value), ]); }); } } exports.BrowserResponse = BrowserResponse; //# sourceMappingURL=browserresponse.js.map