From f309d4256baf751de2588544cd91b3c51e08ca8e Mon Sep 17 00:00:00 2001 From: Daniel Izdebski Date: Thu, 30 Nov 2023 22:47:06 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(metamask):=20Upgrade=20`unlock?= =?UTF-8?q?ForFixture`=20to=20reload=20page?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/fixture-actions/unlockForFixture.ts | 22 ++++++++++--- .../src/pages/HomePage/actions/addNetwork.ts | 4 +-- .../src/utils/waitForSpinnerToVanish.ts | 1 + .../test/e2e/metamask/addNetwork.spec.ts | 32 ++++++++++++------- 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/wallets/metamask/src/fixture-actions/unlockForFixture.ts b/wallets/metamask/src/fixture-actions/unlockForFixture.ts index 3975477b0..71e44a73b 100644 --- a/wallets/metamask/src/fixture-actions/unlockForFixture.ts +++ b/wallets/metamask/src/fixture-actions/unlockForFixture.ts @@ -8,10 +8,7 @@ import { waitForSpinnerToVanish } from '../utils/waitForSpinnerToVanish' export async function unlockForFixture(page: Page, password: string) { const metamask = new MetaMask(page.context(), page, password) - await metamask.unlock() - - // TODO: If this function times out -> page.reload() and try again. - await waitForSpinnerToVanish(page) + await unlockWalletButReloadIfSpinnerDoesNotVanish(metamask) await retryIfMetaMaskCrashAfterUnlock(page) @@ -19,6 +16,23 @@ export async function unlockForFixture(page: Page, password: string) { await closeRecoveryPhraseReminder(page) } +async function unlockWalletButReloadIfSpinnerDoesNotVanish(metamask: MetaMask) { + try { + await metamask.unlock() + } catch (e) { + if (e instanceof playwrightErrors.TimeoutError) { + console.warn('[UnlockWalletButReloadIfSpinnerDoesNotVanish] Unlocking MetaMask timed out. Reloading page...') + + const page = metamask.page + + await page.reload() + await waitForSpinnerToVanish(page) + } else { + throw e + } + } +} + async function retryIfMetaMaskCrashAfterUnlock(page: Page) { const homePageLogoLocator = page.locator(HomePage.selectors.logo) diff --git a/wallets/metamask/src/pages/HomePage/actions/addNetwork.ts b/wallets/metamask/src/pages/HomePage/actions/addNetwork.ts index bb48ff19c..42c4a6515 100644 --- a/wallets/metamask/src/pages/HomePage/actions/addNetwork.ts +++ b/wallets/metamask/src/pages/HomePage/actions/addNetwork.ts @@ -28,7 +28,7 @@ export async function addNetwork(page: Page, network: Network) { // We have to wait for the RPC URL error to appear. const rpcUrlErrorLocator = page.locator(Selectors.settings.networks.newNetworkForm.rpcUrlError) - if (await waitFor(() => rpcUrlErrorLocator.isVisible(), 1_000, false)) { + if (await waitFor(() => rpcUrlErrorLocator.isVisible(), 2_000, false)) { const rpcUrlErrorText = await rpcUrlErrorLocator.textContent({ timeout: 1_000 }) throw new Error(`[AddNetwork] RPC URL error: ${rpcUrlErrorText}`) } @@ -37,7 +37,7 @@ export async function addNetwork(page: Page, network: Network) { // We have to wait for the Chain ID error to appear. const chainIdErrorLocator = page.locator(Selectors.settings.networks.newNetworkForm.chainIdError) - if (await waitFor(() => chainIdErrorLocator.isVisible(), 1_000, false)) { + if (await waitFor(() => chainIdErrorLocator.isVisible(), 2_000, false)) { const chainIdErrorText = await chainIdErrorLocator.textContent({ timeout: 1_000 }) throw new Error(`[AddNetwork] Chain ID error: ${chainIdErrorText}`) } diff --git a/wallets/metamask/src/utils/waitForSpinnerToVanish.ts b/wallets/metamask/src/utils/waitForSpinnerToVanish.ts index 077ee5c68..fd99071a0 100644 --- a/wallets/metamask/src/utils/waitForSpinnerToVanish.ts +++ b/wallets/metamask/src/utils/waitForSpinnerToVanish.ts @@ -1,6 +1,7 @@ import type { Page } from '@playwright/test' import { LoadingSelectors } from '../selectors' +// TODO: Should we decrease the timeout? // TODO: Not sure if hard coding the timeout is a good idea but must be enough for now. const DEFAULT_TIMEOUT = 10_000 diff --git a/wallets/metamask/test/e2e/metamask/addNetwork.spec.ts b/wallets/metamask/test/e2e/metamask/addNetwork.spec.ts index c4487f519..ef06e6257 100644 --- a/wallets/metamask/test/e2e/metamask/addNetwork.spec.ts +++ b/wallets/metamask/test/e2e/metamask/addNetwork.spec.ts @@ -9,14 +9,6 @@ const test = testWithSynpress(basicSetup, unlockForFixture) const { expect } = test -const optimismMainnet = { - name: 'OP Mainnet', - rpcUrl: 'https://mainnet.optimism.io', - chainId: 10, - symbol: 'ETH', - blockExplorerUrl: 'https://optimistic.etherscan.io' -} - test('should add network and close network added popup', async ({ context, metamaskPage, createAnvilNode }) => { const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword) @@ -70,6 +62,14 @@ test('should validate the network object with Zod', async ({ context, metamaskPa test('should throw if there is an issue with rpc url', async ({ context, metamaskPage }) => { const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword) + const optimismMainnet = { + name: 'OP Mainnet', + rpcUrl: 'https://mainnet.optimism.io', + chainId: 10, + symbol: 'ETH', + blockExplorerUrl: 'https://optimistic.etherscan.io' + } + const promise = metamask.addNetwork({ ...optimismMainnet, rpcUrl: 'hps://mainnet.optimism.io' // Incorrect. @@ -80,15 +80,25 @@ test('should throw if there is an issue with rpc url', async ({ context, metamas ) }) -test('should throw if there is an issue with chain id', async ({ context, metamaskPage }) => { +test('should throw if there is an issue with chain id', async ({ context, metamaskPage, createAnvilNode }) => { const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword) + const { rpcUrl, chainId } = await createAnvilNode() + + const network = { + name: 'Anvil', + rpcUrl, + chainId, + symbol: 'ETH', + blockExplorerUrl: 'https://etherscan.io/' + } + const promise = metamask.addNetwork({ - ...optimismMainnet, + ...network, chainId: 0x42069 // Incorrect. }) await expect(promise).rejects.toThrowError( - '[AddNetwork] Chain ID error: The RPC URL you have entered returned a different chain ID (10). Please update the Chain ID to match the RPC URL of the network you are trying to add.' + '[AddNetwork] Chain ID error: The RPC URL you have entered returned a different chain ID (31337). Please update the Chain ID to match the RPC URL of the network you are trying to add.' ) })