UNPKG

@lidofinance/browser-service

Version:

This package provides the BrowserService class for initializing and managing a Playwright browser context, network configurations, and wallets (EOA or WalletConnect). It facilitates automated testing of interactions with Web3 wallets (e.g., Metamask) and

131 lines 6.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BrowserService = void 0; const wallets_testing_wallets_1 = require("@lidofinance/wallets-testing-wallets"); const wallets_testing_extensions_1 = require("@lidofinance/wallets-testing-extensions"); const wallets_testing_nodes_1 = require("@lidofinance/wallets-testing-nodes"); const browser_constants_1 = require("./browser.constants"); const browser_context_service_1 = require("./browser.context.service"); const accounts_1 = require("viem/accounts"); const test_1 = require("@playwright/test"); const common_1 = require("@nestjs/common"); /** * Required options depends on `WalletConnectTypes`: * - `WalletConnectType.EOA` and `WalletConnectType.WC`: * - networkConfig * - accountConfig * - walletConfig * - nodeConfig * - browserOptions? * - `WalletConnectType.IFRAME`: * - networkConfig * - accountConfig * - walletConfig * - nodeConfig * - standUrl * - browserOptions? */ class BrowserService { constructor(options) { this.options = options; this.logger = new common_1.ConsoleLogger(BrowserService.name); this.networkConfig = options.networkConfig; this.walletConfig = options.walletConfig; } getWalletPage() { if (!this.walletPage) this.logger.error('"walletPage" is not initialized. Use initWalletSetup() function'); return this.walletPage; } getBrowserContextPage() { return this.browserContextService.browserContext.pages()[0]; } async initWalletSetup(useFork) { if (useFork) { await this.setupWithNode(); } else { await this.setup(); await this.walletPage.setupNetwork(this.options.networkConfig); await this.walletPage.changeNetwork(this.options.networkConfig.chainName); await this.browserContextService.closePages(); } } async setupWithNode() { this.isFork = true; this.ethereumNodeService = new wallets_testing_nodes_1.EthereumNodeService(this.options.nodeConfig); await this.ethereumNodeService.startNode(); await this.ethereumNodeService.setupDefaultTokenBalances(); const account = this.ethereumNodeService.getAccount(); await this.setup(); if (!(await this.walletPage.isWalletAddressExist(account.address))) { await this.walletPage.importKey(account.secretKey); } else { await this.walletPage.changeWalletAccountByAddress(account.address); } await this.walletPage.setupNetwork({ ...this.options.networkConfig, chainName: this.options.networkConfig.chainName, rpcUrl: this.ethereumNodeService.state.nodeUrl, }); await this.ethereumNodeService.mockRoute(this.options.nodeConfig.rpcUrlToMock, this.browserContextService.browserContext); await this.browserContextService.closePages(); } async setup() { const extensionService = new wallets_testing_extensions_1.ExtensionService(); const extensionPath = await extensionService.getExtensionDirFromId(this.options.walletConfig.STORE_EXTENSION_ID, this.options.walletConfig.LATEST_STABLE_DOWNLOAD_LINK); const contextDataDir = `${browser_constants_1.DEFAULT_BROWSER_CONTEXT_DIR_NAME}_${(0, accounts_1.mnemonicToAccount)(this.options.accountConfig.SECRET_PHRASE).address}_isFork-${this.isFork}_${this.options.walletConfig.WALLET_NAME}`; this.browserContextService = new browser_context_service_1.BrowserContextService(extensionPath, { contextDataDir, browserOptions: this.options.browserOptions, }); await this.browserContextService.initBrowserContext(); await this.annotateExtensionWalletVersion(extensionService); this.setWalletPage(); await this.walletPage.setup(); } async teardown() { if (this.browserContextService.browserContext !== null) await this.browserContextService.browserContext.close(); if (this.ethereumNodeService) { await this.ethereumNodeService.stopNode(); } } setWalletPage() { const extension = new wallets_testing_extensions_1.Extension(this.browserContextService.extensionId); const extensionWalletPage = new browser_constants_1.WALLET_PAGES[this.options.walletConfig.EXTENSION_WALLET_NAME]({ browserContext: this.browserContextService.browserContext, extensionUrl: extension.url, accountConfig: this.options.accountConfig, walletConfig: this.options.walletConfig, }); switch (this.options.walletConfig.WALLET_TYPE) { case wallets_testing_wallets_1.WalletConnectTypes.WC: case wallets_testing_wallets_1.WalletConnectTypes.IFRAME: this.walletPage = new browser_constants_1.WALLET_PAGES[this.options.walletConfig.WALLET_NAME]({ browserContext: this.browserContextService.browserContext, extensionPage: extensionWalletPage, walletConfig: this.options.walletConfig, standConfig: { chainId: this.options.networkConfig.chainId, standUrl: this.options.standUrl, rpcUrl: this.ethereumNodeService?.state.nodeUrl || this.options.networkConfig.rpcUrl, }, }); break; default: this.walletPage = extensionWalletPage; } } async annotateExtensionWalletVersion(extensionService) { const manifestContent = await extensionService.getManifestContent(this.options.walletConfig.STORE_EXTENSION_ID); test_1.test.info().annotations.push({ type: `${this.options.walletConfig.EXTENSION_WALLET_NAME} wallet version`, description: manifestContent.version, }); } } exports.BrowserService = BrowserService; //# sourceMappingURL=browser.service.js.map