Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ chore(metamask): Make waitFor accept action directly #1004

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion wallets/metamask/src/fixture-actions/unlockForFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
6 changes: 3 additions & 3 deletions wallets/metamask/src/pages/NotificationPage/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
16 changes: 2 additions & 14 deletions wallets/metamask/src/utils/waitFor.ts
Original file line number Diff line number Diff line change
@@ -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<boolean>, timeout: number, shouldThrow = true) {
let timeoutsSum = 0
let timeoutIndex = 0

Expand All @@ -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
}
Expand All @@ -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()
}
2 changes: 1 addition & 1 deletion wallets/metamask/test/e2e/wallet-setup/basic.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion wallets/metamask/test/e2e/wallet-setup/connected.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down