flagpole
Version: 
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
184 lines • 6.73 kB
JavaScript
"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 util_1 = require("../util");
const helpers_1 = require("../helpers");
const value_promise_1 = require("../value-promise");
const response_1 = require("../response");
const DEFAULT_WAITFOR_TIMEOUT = 30000;
class PuppeteerResponse extends response_1.ProtoResponse {
    constructor(scenario) {
        super(scenario);
        this.scenario = scenario;
        this.scenario = scenario;
    }
    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 currentUrl() {
        const page = this.scenario.page;
        let url = null;
        if (page) {
            url = page.url();
        }
        return helpers_1.wrapAsValue(this.context, url, "Current URL");
    }
    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) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this._page.waitForNavigation({
                timeout: this.getTimeoutFromOverload(timeout),
                waitUntil: "networkidle0",
            });
            return;
        });
    }
    waitForNavigation(timeout, 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: this.getTimeoutFromOverload(timeout),
                waitUntil: waitForEvent,
            });
            return;
        });
    }
    waitForLoad(timeout) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this._page.waitForNavigation({
                timeout: this.getTimeoutFromOverload(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) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this._page.waitForNavigation({
                timeout: this.getTimeoutFromOverload(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,
        });
    }
    clearThenType(selector, textToType, opts = {}) {
        return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
            const el = yield this.clear(selector);
            yield el.type(textToType, opts);
            return el;
        }));
    }
    type(selector, textToType, opts = {}) {
        return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
            const el = yield this.find(selector);
            yield el.type(textToType, opts);
            return el;
        }));
    }
    clear(selector) {
        return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
            const el = yield this.find(selector);
            yield el.clear();
            return el;
        }));
    }
    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;
        });
    }
    getTimeoutFromOverload(a, b) {
        return typeof b == "number"
            ? b
            : typeof a == "number"
                ? a
                : DEFAULT_WAITFOR_TIMEOUT;
    }
    getContainsPatternFromOverload(contains) {
        return contains instanceof RegExp
            ? contains
            : typeof contains == "string"
                ? new RegExp(contains)
                : null;
    }
    navigate(req) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            if (req.uri) {
                yield ((_a = this.page) === null || _a === void 0 ? void 0 : _a.goto(req.uri));
            }
        });
    }
}
exports.PuppeteerResponse = PuppeteerResponse;
//# sourceMappingURL=puppeteer-response.js.map