@assert-equals/dappdriver
Version:
DappDriver is an e2e testing framework designed for testing decentralized applications (dApps) using MetaMask, Rainbow or Zerion
298 lines (297 loc) • 9.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DappDriver = void 0;
const headless_wallet_1 = require("@assert-equals/headless-wallet");
const constants_1 = require("../constants");
const setup_1 = require("../flask/setup");
const setup_2 = require("../metamask/setup");
const page_1 = require("../page");
const playwright_factory_1 = require("../playwright/playwright-factory");
const setup_3 = require("../rainbow/setup");
const webdriver_factory_1 = require("../webdriver/webdriver-factory");
const setup_4 = require("../zerion/setup");
/**
*
*
* @export
* @class DappDriver
*/
class DappDriver {
static instance = null;
domain;
extension;
isDisposed;
driver;
page;
frame;
framework;
wallet;
/**
* Creates an instance of DappDriver.
* @param {string} domain
* @param {Framework} framework
* @param {Driver} driver
* @memberof DappDriver
*/
constructor(domain, framework, driver) {
this.domain = domain;
this.framework = framework;
this.driver = driver;
}
static get Instance() {
return this.instance;
}
static set Instance(value) {
this.instance = value;
}
get Framework() {
return this.framework;
}
get Disposed() {
return this.isDisposed;
}
set Disposed(value) {
this.isDisposed = value;
}
get Driver() {
return this.driver;
}
get Domain() {
return this.domain;
}
get Extension() {
return this.extension;
}
set Extension(value) {
this.extension = value;
}
get Page() {
return this.page;
}
set Page(value) {
this.page = value;
}
get Frame() {
return this.frame;
}
set Frame(value) {
this.frame = value;
}
get Wallet() {
return this.wallet;
}
set Wallet(value) {
this.wallet = value;
}
static async create(domain, framework, browser, arg4, options) {
let tPage = null;
options = options || { proxy: false, extension: { wallet: null, path: null, seed: null } };
options.proxy = options.proxy ?? false;
options.extension = options.extension ?? { wallet: null, path: null, seed: null };
if (typeof arg4 === 'function') {
tPage = arg4;
}
else if (typeof arg4 === 'object') {
options = arg4;
}
await DappDriver.enableAutomation(options);
const driver = await DappDriver.build(framework, browser, options);
const session = new DappDriver(domain, framework, driver);
if (DappDriver.instance === null) {
DappDriver.instance = session;
}
let page = null;
if (framework === constants_1.PLAYWRIGHT) {
page = driver.pages()[0];
DappDriver.Instance.Page = page;
}
await DappDriver.setupWallet(options);
await this.open(domain);
if (tPage === null)
return;
const newPage = await this.getPage(tPage);
return newPage;
}
/**
*
*
* @private
* @static
* @param {Framework} framework
* @param {Browser} browser
* @param {BrowserOptions} options
* @return {*} {Promise<Driver>}
* @memberof DappDriver
*/
static async build(framework, browser, options) {
let driver;
if (framework === constants_1.PLAYWRIGHT) {
driver = await new playwright_factory_1.PlaywrightFactory().build(browser, options);
}
else if (framework === constants_1.WEBDRIVER) {
driver = await new webdriver_factory_1.WebDriverFactory().build(browser, options);
}
else {
throw new Error('Unsupported framework: ' + framework);
}
return driver;
}
/**
*
*
* @private
* @static
* @param {BrowserOptions} options
* @return {*} {Promise<void>}
* @memberof DappDriver
*/
static async enableAutomation(options) {
if (options.extension.wallet === constants_1.METAMASK || options.extension.wallet === constants_1.METAMASK_FLASK) {
try {
let metamaskPath;
if (options.extension.wallet === constants_1.METAMASK) {
metamaskPath = options.extension.path || constants_1.DEFAULT_METAMASK_BINARY_PATH;
}
else if (options.extension.wallet === constants_1.METAMASK_FLASK) {
metamaskPath = options.extension.path || constants_1.DEFAULT_METAMASK_FLASK_BINARY_PATH;
}
await (0, setup_2.enableMetaMaskAutomation)(metamaskPath);
}
catch (error) {
throw new Error('Could not enable automation in MetaMask, try running `npx dappdriver -w metamask` to install MetaMask.');
}
}
}
/**
*
* Schedules a command to navigate to a new URL
* @private
* @static
* @param {string} url
* @return {*} {Promise<void>}
* @memberof DappDriver
*/
static async open(url) {
if (DappDriver.Instance.Framework === constants_1.PLAYWRIGHT) {
const page = DappDriver.Instance.Page;
await page.goto(url);
}
else if (DappDriver.Instance.Framework === constants_1.WEBDRIVER) {
await DappDriver.Instance.Driver.get(url);
}
}
/**
*
*
* @private
* @static
* @param {BrowserOptions} options
* @return {*} {Promise<void>}
* @memberof DappDriver
*/
static async setupWallet(options) {
try {
switch (options.extension.wallet) {
case constants_1.METAMASK:
DappDriver.Instance.Wallet = constants_1.METAMASK;
await (0, setup_2.setupMetaMaskWallet)(options.extension.seed);
break;
case constants_1.METAMASK_FLASK:
DappDriver.Instance.Wallet = constants_1.METAMASK_FLASK;
await (0, setup_1.setupMetaMaskFlaskWallet)(options.extension.seed);
break;
case constants_1.ZERION:
DappDriver.Instance.Wallet = constants_1.ZERION;
await (0, setup_4.setupZerionWallet)(options.extension.seed);
break;
case constants_1.RAINBOW:
DappDriver.Instance.Wallet = constants_1.RAINBOW;
await (0, setup_3.setupRainbowWallet)(options.extension.seed);
break;
case constants_1.HEADLESS:
DappDriver.Instance.Wallet = constants_1.HEADLESS;
if (DappDriver.Instance.Framework === constants_1.PLAYWRIGHT) {
const page = DappDriver.Instance.Page;
await (0, headless_wallet_1.setupHeadlessWallet)({ page, port: options.extension.port });
}
else if (DappDriver.Instance.Framework === constants_1.WEBDRIVER) {
const driver = DappDriver.Instance.Driver;
await (0, headless_wallet_1.setupHeadlessWallet)({ driver });
}
break;
}
}
catch (error) {
await this.dispose();
throw new Error('Error setting up wallet: ' + error);
}
}
/**
*
*
* @static
* @template TPage
* @param {new () => TPage} page
* @return {*} {Promise<TPage>}
* @memberof DappDriver
*/
static async getPage(page) {
const newPage = new page();
if (newPage instanceof page_1.PageObject) {
await newPage.waitForTitle();
await newPage.waitForURL();
}
return newPage;
}
/**
*
* Schedules a command to quit the current session
* @static
* @return {*} {Promise<void>}
* @memberof DappDriver
*/
static async dispose() {
if (DappDriver.Instance === null) {
return;
}
if (DappDriver.Instance.Framework === constants_1.PLAYWRIGHT) {
await DappDriver.Instance.Driver.close();
}
else if (DappDriver.Instance.Framework === constants_1.WEBDRIVER) {
await DappDriver.Instance.Driver.quit();
}
DappDriver.instance = null;
}
/**
*
* Schedules a command to make the driver sleep for the given amount of time
* @static
* @param {number} duration
* @return {*} {Promise<void>}
* @memberof DappDriver
*/
static async sleep(duration) {
await new Promise((resolve) => setTimeout(resolve, duration));
}
/**
*
* Schedule a command to take a screenshot
* @static
* @return {*} {Promise<string>}
* @memberof DappDriver
*/
static async takeScreenshot() {
if (DappDriver.Instance === null) {
return;
}
let screenShot;
if (DappDriver.Instance.Framework === constants_1.PLAYWRIGHT) {
screenShot = (await DappDriver.Instance.Page.screenshot()).toString('base64');
}
else if (DappDriver.Instance.Framework === constants_1.WEBDRIVER) {
screenShot = await DappDriver.Instance.Driver.takeScreenshot();
}
return screenShot;
}
}
exports.DappDriver = DappDriver;