UNPKG

@assert-equals/dappdriver

Version:

DappDriver is an e2e testing framework designed for testing decentralized applications (dApps) using MetaMask, Rainbow or Zerion

387 lines (386 loc) 11.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PageObject = void 0; const constants_1 = require("../constants"); const page_object_1 = require("../playwright/page-object"); const dapp_driver_1 = require("../session/dapp-driver"); const utils_1 = require("../utils"); const page_object_2 = require("../webdriver/page-object"); /** * * * @export * @class PageObject * @implements {IPageObject} */ class PageObject { page; url; title; /** * Creates an instance of PageObject. * @param {(string | RegExp)} [url=''] * @param {string} [title=''] * @memberof PageObject */ constructor(url = '', title = '') { this.initialize(url, title); } /** * * * @param {(string | RegExp)} url * @param {string} title * @memberof PageObject */ initialize(url, title) { this.page = dapp_driver_1.DappDriver.Instance.Page; this.url = url; this.title = title; } /** * * * @private * @param {keyof IPageObject} methodName * @param {Array<any>} [args=[]] * @return {*} {Promise<any>} * @memberof PageObject */ async callIfMethodExists(methodName, args = []) { let pageObject; if (dapp_driver_1.DappDriver.Instance.Framework === constants_1.PLAYWRIGHT) { pageObject = new page_object_1.PlaywrightPageObject(this.page); } else if (dapp_driver_1.DappDriver.Instance.Framework === constants_1.WEBDRIVER) { pageObject = new page_object_2.WebDriverPageObject(); } return await pageObject[methodName](...args); } /** * * * @private * @param {string} url * @return {*} {string} * @memberof PageObject */ getFullURL(url) { const protocols = ['http', 'chrome-extension://']; if (protocols.some((protocol) => url.startsWith(protocol))) { return url; } else { return dapp_driver_1.DappDriver.Instance.Domain + url; } } /** * * * @param {Cookie} cookie * @return {*} {Promise<void>} * @memberof PageObject */ async addCookie(cookie) { return await this.callIfMethodExists('addCookie', [cookie]); } async back(page) { if (page) { return await this.callIfMethodExists('back', [page]); } else { return await this.callIfMethodExists('back'); } } /** * * * @param {string} name * @return {*} {Promise<void>} * @memberof PageObject */ async clearCookie(name) { return await this.callIfMethodExists('clearCookie', [name]); } /** * * * @return {*} {Promise<void>} * @memberof PageObject */ async clearCookies() { return await this.callIfMethodExists('clearCookies'); } async close(page) { if (page) { return await this.callIfMethodExists('close', [page]); } else { return await this.callIfMethodExists('close'); } } async closeAndSwitchToMainWindow(page) { if (page) { return await this.callIfMethodExists('closeAndSwitchToMainWindow', [page]); } else { return await this.callIfMethodExists('closeAndSwitchToMainWindow'); } } /** * * Schedules a command to open a new window * @return {*} {Promise<void>} * @memberof PageObject */ async createNewWindow() { return await this.callIfMethodExists('createNewWindow'); } /** * * Schedules a command to execute JavaScript in the context of the currently selected frame or window * @param {string} script * @return {*} {Promise<any>} * @memberof PageObject */ async executeScript(script) { return await this.callIfMethodExists('executeScript', [script]); } /** * * Schedules a command to execute JavaScript in the context of the currently selected frame or window and switch the focus of all future commands to the window * @template TPage * @param {string} script * @param {new () => TPage} page * @return {*} {Promise<any>} * @memberof PageObject */ async executeScriptAndOpensInWindow(script, page) { return await this.callIfMethodExists('executeScriptAndOpensInWindow', [script, page]); } /** * * Schedules a command to retrieve the current list of available window handles * @return {*} {Promise<Array<any>>} * @memberof PageObject */ async getAllWindowHandles() { return await this.callIfMethodExists('getAllWindowHandles'); } /** * * * @param {string} name * @return {*} {Promise<any>} * @memberof PageObject */ async getCookie(name) { return await this.callIfMethodExists('getCookie', [name]); } /** * * * @return {*} {Promise<Array<any>>} * @memberof PageObject */ async getCookies() { return await this.callIfMethodExists('getCookies'); } /** * * Schedules a command to retrieve the URL of the current page * @return {*} {Promise<string>} * @memberof PageObject */ async getCurrentUrl() { return await this.callIfMethodExists('getCurrentUrl'); } /** * * Schedules a command to retrieve the current page title * @return {*} {Promise<string>} * @memberof PageObject */ async getTitle() { return await this.callIfMethodExists('getTitle'); } /** * * Schedules a command to retrieve the current window handle * @return {*} {Promise<any>} * @memberof PageObject */ async getWindowHandle() { return await this.callIfMethodExists('getWindowHandle'); } /** * * Schedules a command to maximize the window * @return {*} {Promise<void>} * @memberof PageObject */ async maximize() { return await this.callIfMethodExists('maximize'); } async navigateTo(url, page) { url = this.getFullURL(url); if (page) { return await this.callIfMethodExists('navigateTo', [url, page]); } else { return await this.callIfMethodExists('navigateTo', [url]); } } async navigateToPageInNewWindow(url, page) { url = this.getFullURL(url); if (page) { return await this.callIfMethodExists('navigateToPageInNewWindow', [url, page]); } else { return await this.callIfMethodExists('navigateToPageInNewWindow', [url]); } } async opensInNewWindow(page) { if (page) { return await this.callIfMethodExists('opensInNewWindow', [page]); } else { return await this.callIfMethodExists('opensInNewWindow'); } } /** * * Schedules a command to switch the focus of all future commands to the window * @template TPage * @param {new () => TPage} page * @return {*} {Promise<TPage>} * @memberof PageObject */ async opensInWindow(page) { return await this.callIfMethodExists('opensInWindow', [page]); } async refresh(page) { if (page) { return await this.callIfMethodExists('refresh', [page]); } else { return await this.callIfMethodExists('refresh'); } } /** * * Schedules a command to resize the window * @param {number} width * @param {number} height * @return {*} {Promise<void>} * @memberof PageObject */ async setSize(width, height) { return await this.callIfMethodExists('setSize', [width, height]); } /** * * Schedules a command to switch focus of all future commands to the topmost frame on the page * @return {*} {Promise<void>} * @memberof PageObject */ async switchBack() { return await this.callIfMethodExists('switchBack'); } /** * * Schedules a command to switch the focus of all future commands to another frame on the page * @param {string} cssLocator * @return {*} {Promise<void>} * @memberof PageObject */ async switchToFrame(cssLocator) { return await this.callIfMethodExists('switchToFrame', [cssLocator]); } async switchToMainWindow(page) { if (page) { return await this.callIfMethodExists('switchToMainWindow', [page]); } else { return await this.callIfMethodExists('switchToMainWindow'); } } async switchToWindow(nameOrHandle, page) { if (page) { return await this.callIfMethodExists('switchToWindow', [nameOrHandle, page]); } else { return await this.callIfMethodExists('switchToWindow', [nameOrHandle]); } } /** * * Schedules a command to wait for an element to appear * @param {string} cssLocator * @return {*} {Promise<void>} * @memberof PageObject */ async waitForElement(cssLocator) { return await this.callIfMethodExists('waitForElement', [cssLocator]); } /** * * Schedules a command to wait for a function to return a truthy value * @param {Function} func * @param {string} errMsg * @param {number} [timeout=10_000] * @return {*} {Promise<void>} * @memberof PageObject */ async waitForFunction(func, errMsg, timeout = 10_000) { const delay = 500; let timeElapsed = 0; let result = false; while (timeElapsed <= timeout) { try { result = await func(); } catch (e) { } if (result) { return; } await dapp_driver_1.DappDriver.sleep(delay); timeElapsed += delay; } throw new Error(`${errMsg}\nWait timed out after ${timeout}ms`); } /** * * Schedules a command to wait for the required title to be returned * @param {RegExp} [title] * @return {*} {Promise<void>} * @memberof PageObject */ async waitForTitle(title) { title = title || (0, utils_1.toRegExp)(this.title); return await this.callIfMethodExists('waitForTitle', [title]); } /** * * Schedules a command to wait for the current page to navigate to the given URL * @param {RegExp} [url] * @return {*} {Promise<void>} * @memberof PageObject */ async waitForURL(url) { url = url || (0, utils_1.toRegExp)(this.url); return await this.callIfMethodExists('waitForURL', [url]); } /** * * Schedules a command to wait for the required count of windows * @param {number} total * @param {Comparator} comparator * @return {*} {Promise<Array<any>>} * @memberof PageObject */ async waitForWindows(total, comparator = utils_1.strictEqual) { return await this.callIfMethodExists('waitForWindows', [total, comparator]); } } exports.PageObject = PageObject;