From c837c552957661a58abf56054ad703ddc6bff069 Mon Sep 17 00:00:00 2001 From: Daniel Izdebski Date: Wed, 1 Nov 2023 01:11:47 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(metamask):=20Add=20`unlockForF?= =?UTF-8?q?ixture`=20action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wallets/metamask/src/actions/index.ts | 1 + .../metamask/src/actions/unlockForFixture.ts | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 wallets/metamask/src/actions/unlockForFixture.ts diff --git a/wallets/metamask/src/actions/index.ts b/wallets/metamask/src/actions/index.ts index c45d637f4..435a3f487 100644 --- a/wallets/metamask/src/actions/index.ts +++ b/wallets/metamask/src/actions/index.ts @@ -1,3 +1,4 @@ export * from './connectToDapp' export * from './lock' export * from './unlock' +export * from './unlockForFixture' diff --git a/wallets/metamask/src/actions/unlockForFixture.ts b/wallets/metamask/src/actions/unlockForFixture.ts new file mode 100644 index 000000000..2cbc16c51 --- /dev/null +++ b/wallets/metamask/src/actions/unlockForFixture.ts @@ -0,0 +1,47 @@ +import type { Page } from '@playwright/test' +import { errors as playwrightErrors } from '@playwright/test' +import { CrashPageSelectors, HomePageSelectors, LoadingSelectors, unlock } from '../' + +export async function unlockForFixture(page: Page, password: string) { + await unlock(page, password) + + await page.locator(LoadingSelectors.spinner).waitFor({ + state: 'hidden', + timeout: 3000 // TODO: Extract & Make this timeout configurable. + }) + + await retryIfMetaMaskCrashAfterUnlock(page) +} + +async function retryIfMetaMaskCrashAfterUnlock(page: Page) { + const isHomePageVisible = await page.locator(HomePageSelectors.logo).isVisible() + + if (!isHomePageVisible) { + if (await page.locator(CrashPageSelectors.header).isVisible()) { + const errors = await page.locator(CrashPageSelectors.errors).allTextContents() + + console.warn(['[RetryIfMetaMaskCrashAfterUnlock] MetaMask crashed due to:', ...errors].join('\n')) + + console.log('[RetryIfMetaMaskCrashAfterUnlock] Reloading page...') + await page.reload() + + try { + await page.locator(HomePageSelectors.logo).waitFor({ + state: 'visible', + timeout: 10_000 // TODO: Extract & Make this timeout configurable. + }) + console.log('[RetryIfMetaMaskCrashAfterUnlock] Successfully restored MetaMask!') + } catch (e) { + if (e instanceof playwrightErrors.TimeoutError) { + throw new Error( + ['[RetryIfMetaMaskCrashAfterUnlock] Reload did not help. Throwing with the crash cause:', ...errors].join( + '\n' + ) + ) + } + + throw e + } + } + } +}