From c13f03ba8cc61a207c54aa71728d6a87c4329e54 Mon Sep 17 00:00:00 2001 From: Sero <69639595+Seroxdesign@users.noreply.github.com> Date: Thu, 4 Jul 2024 12:52:23 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Wait=20for=20metamask=20fun?= =?UTF-8?q?ctions=20(#1160)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metamask/src/fixtures/metaMaskFixtures.ts | 3 +- wallets/metamask/src/selectors/error/index.ts | 7 +++ wallets/metamask/src/selectors/index.ts | 1 + .../metamask/src/selectors/loading/index.ts | 1 + .../getNotificationPageAndWaitForLoad.ts | 18 +----- wallets/metamask/src/utils/waitFor.ts | 57 +++++++++++++++++++ 6 files changed, 70 insertions(+), 17 deletions(-) create mode 100644 wallets/metamask/src/selectors/error/index.ts diff --git a/wallets/metamask/src/fixtures/metaMaskFixtures.ts b/wallets/metamask/src/fixtures/metaMaskFixtures.ts index cc2019e4b..88b3cd55a 100644 --- a/wallets/metamask/src/fixtures/metaMaskFixtures.ts +++ b/wallets/metamask/src/fixtures/metaMaskFixtures.ts @@ -12,6 +12,7 @@ import { import { type Anvil, type CreateAnvilOptions, createPool } from '@viem/anvil' import fs from 'fs-extra' import { persistLocalStorage } from '../fixture-actions/persistLocalStorage' +import { waitForMetaMaskWindowToBeStable } from '../utils/waitFor' type MetaMaskFixtures = { _contextPath: string @@ -84,7 +85,7 @@ export const metaMaskFixtures = (walletSetup: ReturnType { - await waitForSelector(selector, notificationPage as Page, 5000) - }) - ) - .then(() => { - console.log('All loading indicators are hidden') - }) - .catch((error) => { - console.error('Error: ', error) - }) - - return notificationPage + return await waitForMetaMaskLoad(notificationPage) } diff --git a/wallets/metamask/src/utils/waitFor.ts b/wallets/metamask/src/utils/waitFor.ts index e33a4de68..c5680567e 100644 --- a/wallets/metamask/src/utils/waitFor.ts +++ b/wallets/metamask/src/utils/waitFor.ts @@ -1,5 +1,8 @@ import type { Page } from '@playwright/test' import { errors } from '@playwright/test' +import { ErrorSelectors, LoadingSelectors } from '../selectors' + +const DEFAULT_TIMEOUT = 2000 export const waitUntilStable = async (page: Page) => { await page.waitForLoadState('domcontentloaded') @@ -21,6 +24,60 @@ export const waitForSelector = async (selector: string, page: Page, timeout: num } } +export const waitForMetaMaskLoad = async (page: Page) => { + await Promise.all( + LoadingSelectors.loadingIndicators.map(async (selector) => { + await waitForSelector(selector, page, DEFAULT_TIMEOUT) + }) + ) + .then(() => { + console.log('All loading indicators are hidden') + }) + .catch((error) => { + console.error('Error: ', error) + }) + + return page +} + +export const waitForMetaMaskWindowToBeStable = async (page: Page) => { + await waitForMetaMaskLoad(page) + if ((await page.locator(ErrorSelectors.loadingOverlayErrorButtons).count()) > 0) { + const retryButton = await page.locator(ErrorSelectors.loadingOverlayErrorButtonsRetryButton) + await retryButton.click() + await waitForSelector(LoadingSelectors.loadingOverlay, page, DEFAULT_TIMEOUT) + } + await fixCriticalError(page) +} + +export const fixCriticalError = async (page: Page) => { + for (let times = 0; times < 5; times++) { + if ((await page.locator(ErrorSelectors.criticalError).count()) > 0) { + console.log('[fixCriticalError] Metamask crashed with critical error, refreshing..') + if (times <= 3) { + await page.reload() + await waitForMetaMaskWindowToBeStable(page) + } else if (times === 4) { + const restartButton = await page.locator(ErrorSelectors.criticalErrorRestartButton) + await restartButton.click() + await waitForMetaMaskWindowToBeStable(page) + } else { + throw new Error('[fixCriticalError] Max amount of retries to fix critical metamask error has been reached.') + } + } else if ((await page.locator(ErrorSelectors.errorPage).count()) > 0) { + console.log('[fixCriticalError] Metamask crashed with error, refreshing..') + if (times <= 4) { + await page.reload() + await waitForMetaMaskWindowToBeStable(page) + } else { + throw new Error('[fixCriticalError] Max amount of retries to fix critical metamask error has been reached.') + } + } else { + break + } + } +} + // Inlining the sleep function here cause this is one of the few places in the entire codebase where sleep should be used! const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))