Skip to content

Commit

Permalink
✨ feat(metamask): Add unlockForFixture action (#976)
Browse files Browse the repository at this point in the history
  • Loading branch information
duckception authored Nov 1, 2023
1 parent 1b4dfc0 commit 1053d5d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions wallets/metamask/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './connectToDapp'
export * from './lock'
export * from './unlock'
export * from './unlockForFixture'
47 changes: 47 additions & 0 deletions wallets/metamask/src/actions/unlockForFixture.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}

0 comments on commit 1053d5d

Please sign in to comment.