UNPKG

flagpole

Version:

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

142 lines 5.25 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.PuppeteerResponse = void 0; const domresponse_1 = require("../html/domresponse"); const util_1 = require("../util"); class PuppeteerResponse extends domresponse_1.DOMResponse { get isBrowser() { return true; } get browserControl() { return this.scenario.browserControl; } get page() { var _a; return ((_a = this.scenario.browserControl) === null || _a === void 0 ? void 0 : _a.page) || null; } get browser() { var _a; return ((_a = this.scenario.browserControl) === null || _a === void 0 ? void 0 : _a.browser) || null; } get response() { var _a; return ((_a = this.scenario.browserControl) === null || _a === void 0 ? void 0 : _a.response) || null; } get _page() { if (this.page === null) { throw "Puppeteer page object was not found."; } return this.page; } eval(js, ...args) { return __awaiter(this, void 0, void 0, function* () { return this._page.evaluate.apply(this._page, [js, ...args]); }); } waitForNetworkIdle(timeout = 10000) { return __awaiter(this, void 0, void 0, function* () { yield this._page.waitForNavigation({ timeout: timeout, waitUntil: "networkidle0", }); return; }); } waitForNavigation(timeout = 10000, waitFor) { return __awaiter(this, void 0, void 0, function* () { const allowedOptions = [ "load", "domcontentloaded", "networkidle0", "networkidle2", ]; const waitForEvent = (() => { if (typeof waitFor == "string" && allowedOptions.indexOf(waitFor) >= 0) { return [waitFor]; } else if (util_1.toType(waitFor) == "array" && waitFor.every((waitForItem) => { return allowedOptions.indexOf(waitForItem) >= 0; })) { return waitFor; } else { return ["networkidle2"]; } })(); yield this._page.waitForNavigation({ timeout: timeout, waitUntil: waitForEvent, }); return; }); } waitForLoad(timeout = 30000) { return __awaiter(this, void 0, void 0, function* () { yield this._page.waitForNavigation({ timeout: timeout, waitUntil: "load", }); return; }); } waitForFunction(js, opts, ...args) { return __awaiter(this, void 0, void 0, function* () { yield this._page.waitForFunction.apply(this._page, [js, opts, ...args]); return; }); } waitForReady(timeout = 15000) { return __awaiter(this, void 0, void 0, function* () { yield this._page.waitForNavigation({ timeout: timeout, waitUntil: "domcontentloaded", }); return; }); } screenshot(a, b) { const localFilePath = typeof a == "string" ? a : undefined; const opts = (typeof a !== "string" ? a : b) || {}; return this._page.screenshot({ path: localFilePath || opts.path, encoding: "binary", omitBackground: opts.omitBackground || false, clip: opts.clip || undefined, fullPage: opts.fullPage || false, }); } type(selector, textToType, opts = {}) { return __awaiter(this, void 0, void 0, function* () { return yield this._page.type(selector, textToType, opts); }); } clear(selector) { return __awaiter(this, void 0, void 0, function* () { const input = yield this._page.$(selector); if (input !== null) { yield input.click({ clickCount: 3 }); return yield this._page.keyboard.press("Backspace"); } }); } scrollTo(point) { return __awaiter(this, void 0, void 0, function* () { yield this._page.evaluate((x, y) => { window.scrollTo(x, y); }, point.x || 0, point.y || 0); return this; }); } } exports.PuppeteerResponse = PuppeteerResponse; //# sourceMappingURL=puppeteerresponse.js.map