From b71ace6fe1ac0e3eacabbb206f6cec0f9d6af4e1 Mon Sep 17 00:00:00 2001 From: Daniel Izdebski Date: Tue, 21 Nov 2023 00:29:20 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20chore(metamask):=20Make=20?= =?UTF-8?q?`waitFor`=20accept=20action=20directly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/fixture-actions/unlockForFixture.ts | 2 +- .../actions/importWalletFromPrivateKey.ts | 2 +- .../metamask/src/pages/NotificationPage/page.ts | 6 +++--- wallets/metamask/src/utils/waitFor.ts | 16 ++-------------- .../test/e2e/wallet-setup/basic.setup.ts | 2 +- .../test/e2e/wallet-setup/connected.setup.ts | 2 +- 6 files changed, 9 insertions(+), 21 deletions(-) diff --git a/wallets/metamask/src/fixture-actions/unlockForFixture.ts b/wallets/metamask/src/fixture-actions/unlockForFixture.ts index 592281651..0a9822dc8 100644 --- a/wallets/metamask/src/fixture-actions/unlockForFixture.ts +++ b/wallets/metamask/src/fixture-actions/unlockForFixture.ts @@ -20,7 +20,7 @@ export async function unlockForFixture(page: Page, password: string) { const recoveryPhraseReminder = page.locator(metamask.homePage.selectors.recoveryPhraseReminder.gotItButton) // TODO: Extract & Make this timeout configurable. - const isRecoveryPhraseReminderVisible = await waitFor(recoveryPhraseReminder, 'visible', 1000, false) + const isRecoveryPhraseReminderVisible = await waitFor(() => recoveryPhraseReminder.isVisible(), 1000, false) if (isRecoveryPhraseReminderVisible) { await recoveryPhraseReminder.click() } diff --git a/wallets/metamask/src/pages/HomePage/actions/importWalletFromPrivateKey.ts b/wallets/metamask/src/pages/HomePage/actions/importWalletFromPrivateKey.ts index 845dcb358..ce7a4ca00 100644 --- a/wallets/metamask/src/pages/HomePage/actions/importWalletFromPrivateKey.ts +++ b/wallets/metamask/src/pages/HomePage/actions/importWalletFromPrivateKey.ts @@ -11,7 +11,7 @@ export async function importWalletFromPrivateKey(page: Page, privateKey: string) await importButton.click() // TODO: Extract & make configurable - const isHidden = await waitFor(importButton, 'hidden', 1000, false) + const isHidden = await waitFor(() => importButton.isHidden(), 1000, false) if (!isHidden) { const errorText = await page.locator(Selectors.importAccountScreen.error).textContent({ diff --git a/wallets/metamask/src/pages/NotificationPage/page.ts b/wallets/metamask/src/pages/NotificationPage/page.ts index 61303ed9b..68becb647 100644 --- a/wallets/metamask/src/pages/NotificationPage/page.ts +++ b/wallets/metamask/src/pages/NotificationPage/page.ts @@ -29,11 +29,11 @@ export class NotificationPage { private async beforeMessageSignature(extensionId: string) { const notificationPage = await getNotificationPageAndWaitForLoad(this.page.context(), extensionId) + // TODO: Make this configurable. // Most of the time, this function will be used to sign structured messages, so we check for the scroll button first. const isScrollButtonVisible = await waitFor( - notificationPage.locator(Selectors.SignaturePage.structuredMessage.scrollDownButton), - 'visible', - 1_500, // TODO: Make this configurable. + () => notificationPage.locator(Selectors.SignaturePage.structuredMessage.scrollDownButton).isVisible(), + 1_500, false ) diff --git a/wallets/metamask/src/utils/waitFor.ts b/wallets/metamask/src/utils/waitFor.ts index 7028d145b..84a353d49 100644 --- a/wallets/metamask/src/utils/waitFor.ts +++ b/wallets/metamask/src/utils/waitFor.ts @@ -1,16 +1,12 @@ -import type { Locator } from '@playwright/test' - // 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)) const timeouts = [0, 20, 50, 100, 100, 500] as const -type State = 'visible' | 'hidden' - // TODO: Box this function. // This functions mimics the one found in Playwright with a few small differences. // Custom implementation is needed because Playwright lists errors in the report even if they are caught. -export async function waitFor(locator: Locator, state: State, timeout: number, shouldThrow = true) { +export async function waitFor(action: () => Promise, timeout: number, shouldThrow = true) { let timeoutsSum = 0 let timeoutIndex = 0 @@ -28,7 +24,7 @@ export async function waitFor(locator: Locator, state: State, timeout: number, s await sleep(nextTimeout) - const result = await action(locator, state) + const result = await action() if (result) { return result } @@ -40,11 +36,3 @@ export async function waitFor(locator: Locator, state: State, timeout: number, s return false } - -async function action(locator: Locator, state: State) { - if (state === 'hidden') { - return locator.isHidden() - } - - return locator.isVisible() -} diff --git a/wallets/metamask/test/e2e/wallet-setup/basic.setup.ts b/wallets/metamask/test/e2e/wallet-setup/basic.setup.ts index efd49d322..a64ac5658 100644 --- a/wallets/metamask/test/e2e/wallet-setup/basic.setup.ts +++ b/wallets/metamask/test/e2e/wallet-setup/basic.setup.ts @@ -13,7 +13,7 @@ export default defineWalletSetup(PASSWORD, async (context, walletPage) => { const recoveryPhraseReminder = walletPage.locator(metamask.homePage.selectors.recoveryPhraseReminder.gotItButton) - const isRecoveryPhraseReminderVisible = await waitFor(recoveryPhraseReminder, 'visible', 3_000, false) + const isRecoveryPhraseReminderVisible = await waitFor(() => recoveryPhraseReminder.isVisible(), 3_000, false) if (isRecoveryPhraseReminderVisible) { await recoveryPhraseReminder.click() } diff --git a/wallets/metamask/test/e2e/wallet-setup/connected.setup.ts b/wallets/metamask/test/e2e/wallet-setup/connected.setup.ts index 36af72036..0596d9560 100644 --- a/wallets/metamask/test/e2e/wallet-setup/connected.setup.ts +++ b/wallets/metamask/test/e2e/wallet-setup/connected.setup.ts @@ -16,7 +16,7 @@ export default defineWalletSetup(PASSWORD, async (context, walletPage) => { const recoveryPhraseReminder = walletPage.locator(metamask.homePage.selectors.recoveryPhraseReminder.gotItButton) - const isRecoveryPhraseReminderVisible = await waitFor(recoveryPhraseReminder, 'visible', 3_000, false) + const isRecoveryPhraseReminderVisible = await waitFor(() => recoveryPhraseReminder.isVisible(), 3_000, false) if (isRecoveryPhraseReminderVisible) { await recoveryPhraseReminder.click() }