@assert-equals/dappdriver
Version:
DappDriver is an e2e testing framework designed for testing decentralized applications (dApps) using MetaMask, Rainbow or Zerion
409 lines (408 loc) • 12 kB
JavaScript
"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]);
}
/**
*
* Schedules a command to navigate backwards in the browser history
* @template TPage
* @param {new () => TPage} page
* @return {*} {Promise<TPage>}
* @memberof PageObject
*/
async back(page) {
return await this.callIfMethodExists('back', [page]);
}
/**
*
*
* @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');
}
/**
*
* Schedules a command to close the current window
* @template TPage
* @param {new () => TPage} page
* @return {*} {Promise<TPage>}
* @memberof PageObject
*/
async close(page) {
return await this.callIfMethodExists('close', [page]);
}
/**
*
* Schedules a command to close the current window and switch the focus of all future commands to a given window
* @template TPage
* @param {new () => TPage} page
* @return {*} {Promise<TPage>}
* @memberof PageObject
*/
async closeAndSwitchToWindow(page) {
return await this.callIfMethodExists('closeAndSwitchToWindow', [page]);
}
/**
*
* 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 a given window
* @template TPage
* @param {string} script
* @param {new () => TPage} page
* @return {*} {Promise<any>}
* @memberof PageObject
*/
async executeScriptAndSwitchToWindow(script, page) {
return await this.callIfMethodExists('executeScriptAndSwitchToWindow', [script, page]);
}
/**
*
* Schedules a command to navigate forwards in the browser history
* @template TPage
* @param {new () => TPage} page
* @return {*} {Promise<TPage>}
* @memberof PageObject
*/
async forward(page) {
return await this.callIfMethodExists('forward', [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 HTML source of the current page
* @return {*} {Promise<string>}
* @memberof PageObject
*/
async getPageSource() {
return await this.callIfMethodExists('getPageSource');
}
/**
*
* 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');
}
/**
*
* Schedules a command to navigate to a new URL
* @template TPage
* @param {string} url
* @param {new () => TPage} page
* @return {*} {Promise<TPage>}
* @memberof PageObject
*/
async navigateTo(url, page) {
url = this.getFullURL(url);
return await this.callIfMethodExists('navigateTo', [url, page]);
}
/**
*
* Schedules a command to navigate to a new page in a new window
* @template TPage
* @param {string} url
* @param {new () => TPage} page
* @return {*} {Promise<TPage>}
* @memberof PageObject
*/
async navigateToPageInNewWindow(url, page) {
url = this.getFullURL(url);
return await this.callIfMethodExists('navigateToPageInNewWindow', [url, page]);
}
/**
*
* Schedules a command to refresh the current page
* @template TPage
* @param {new () => TPage} page
* @return {*} {Promise<TPage>}
* @memberof PageObject
*/
async refresh(page) {
return await this.callIfMethodExists('refresh', [page]);
}
/**
*
* 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]);
}
/**
*
* Schedules a command to switch the focus of all future commands to a given window
* whose title and url match the provided page
* @template TPage
* @param {new () => TPage} page
* @return {*} {Promise<TPage>}
* @memberof PageObject
*/
async switchToWindow(page) {
return await this.callIfMethodExists('switchToWindow', [page]);
}
/**
*
* Schedules a command to wait for the required count of windows to be opened
* and switch the focus of all future commands to the first matching window
* @template TPage
* @param {number} total
* @param {Comparator} comparator
* @param {new () => TPage} page
* @return {*} {Promise<TPage>}
* @memberof PageObject
*/
async waitAndSwitchToWindow(page, total = 2, comparator = utils_1.isAtLeast) {
return await this.callIfMethodExists('waitAndSwitchToWindow', [page, total, comparator]);
}
/**
*
* 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 to be opened
* @param {number} total
* @param {Comparator} comparator
* @return {*} {Promise<void>}
* @memberof PageObject
*/
async waitForWindows(total, comparator) {
return await this.callIfMethodExists('waitForWindows', [total, comparator]);
}
}
exports.PageObject = PageObject;