UNPKG

flagpole

Version:

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

143 lines 6.67 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 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* () { if (this.page === null) { throw "Page not found."; } const params = util_1.getFindParams(a, b); if (params.opts || params.matches || params.contains) { return util_1.findOne(this, selector, params); } const el = yield this.page.$(selector); return el === null ? util_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* () { if (this.page === null) { throw "Page not found."; } const params = util_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, `${selector} [${i}]`, selector); })); return util_1.filterFind(elements, params.contains || params.matches, params.opts); }); } findXPath(xPath) { return __awaiter(this, void 0, void 0, function* () { if (this.page === null) { throw "Page not found."; } const elements = yield this.page.$x(xPath); if (elements.length > 0) { return yield browserelement_1.BrowserElement.create(elements[0], this.context, xPath, xPath); } return util_1.wrapAsValue(this.context, null, xPath); }); } findAllXPath(xPath) { return __awaiter(this, void 0, void 0, function* () { if (this.page === null) { throw "Page not found."; } 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* () { if (this.page !== null) { 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); } throw new Error("waitForHidden is not available in this context"); }); } waitForVisible(selector, timeout = 100) { return __awaiter(this, void 0, void 0, function* () { if (this.page !== null) { 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); } throw new Error("waitForVisible is not available in this context"); }); } waitForExists(selector, timeout) { return __awaiter(this, void 0, void 0, function* () { if (this.page !== null) { const opts = { timeout: timeout || 100 }; const element = yield this.page.waitForSelector(selector, opts); return browserelement_1.BrowserElement.create(element, this.context, selector, selector); } throw new Error("waitForExists is not available in this context"); }); } waitForXPath(xPath, timeout) { return __awaiter(this, void 0, void 0, function* () { if (this.page !== null) { const opts = { timeout: timeout || 100 }; const element = yield this.page.waitForXPath(xPath, opts); return browserelement_1.BrowserElement.create(element, this.context, xPath, xPath); } throw new Error("waitForXPath is not available in this context"); }); } waitForHavingText(selector, text, timeout) { return __awaiter(this, void 0, void 0, function* () { if (this.page !== null) { 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); } throw new Error("waitForExists is not available in this context"); }); } selectOption(selector, value) { return __awaiter(this, void 0, void 0, function* () { if (this.page === null) { throw "Page was null."; } yield this.page.select.apply(this.page, [ selector, ...util_1.arrayify(value), ]); }); } } exports.BrowserResponse = BrowserResponse; //# sourceMappingURL=browserresponse.js.map