Skip to content

Commit

Permalink
🏗️ chore(metamask): Add core MetaMask class (#984)
Browse files Browse the repository at this point in the history
  • Loading branch information
duckception authored Nov 8, 2023
1 parent 461ca58 commit 46b9047
Show file tree
Hide file tree
Showing 40 changed files with 210 additions and 170 deletions.
4 changes: 0 additions & 4 deletions wallets/metamask/src/actions/index.ts

This file was deleted.

7 changes: 0 additions & 7 deletions wallets/metamask/src/actions/lock.ts

This file was deleted.

10 changes: 0 additions & 10 deletions wallets/metamask/src/actions/unlock.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { Page } from '@playwright/test'
import { errors as playwrightErrors } from '@playwright/test'
import { CrashPageSelectors, HomePageSelectors, LoadingSelectors, unlock } from '../'
import { MetaMask } from '../metamask'
import { CrashPage, HomePage } from '../pages'
import { LoadingSelectors } from '../selectors'

export async function unlockForFixture(page: Page, password: string) {
await unlock(page, password)
const metamask = new MetaMask(page.context(), page, password)

await metamask.unlock()

await page.locator(LoadingSelectors.spinner).waitFor({
state: 'hidden',
Expand All @@ -14,19 +18,19 @@ export async function unlockForFixture(page: Page, password: string) {
}

async function retryIfMetaMaskCrashAfterUnlock(page: Page) {
const isHomePageVisible = await page.locator(HomePageSelectors.logo).isVisible()
const isHomePageVisible = await page.locator(HomePage.selectors.logo).isVisible()

if (!isHomePageVisible) {
if (await page.locator(CrashPageSelectors.header).isVisible()) {
const errors = await page.locator(CrashPageSelectors.errors).allTextContents()
if (await page.locator(CrashPage.selectors.header).isVisible()) {
const errors = await page.locator(CrashPage.selectors.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({
await page.locator(HomePage.selectors.logo).waitFor({
state: 'visible',
timeout: 10_000 // TODO: Extract & Make this timeout configurable.
})
Expand Down
7 changes: 2 additions & 5 deletions wallets/metamask/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
export * from './prepareExtension'
export * from './pages'
export * from './actions'
export * from './selectors'
export * from './utils/getExtensionId'
export * from './metamask'
export * from './fixture-actions/unlockForFixture'
35 changes: 35 additions & 0 deletions wallets/metamask/src/metamask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { BrowserContext, Page } from '@playwright/test'
import { CrashPage, HomePage, LockPage, NotificationPage, OnboardingPage } from './pages'

export class MetaMask {
crashPage: CrashPage
onboardingPage: OnboardingPage
lockPage: LockPage
homePage: HomePage
notificationPage: NotificationPage

constructor(readonly context: BrowserContext, readonly page: Page, readonly password: string) {
this.crashPage = new CrashPage()

this.onboardingPage = new OnboardingPage(page)
this.lockPage = new LockPage(page)
this.homePage = new HomePage(page)
this.notificationPage = new NotificationPage(page)
}

async importWallet(seedPhrase: string) {
await this.onboardingPage.importWallet(seedPhrase, this.password)
}

async connectToDapp(extensionId: string) {
await this.notificationPage.connectToDapp(extensionId)
}

async lock() {
await this.homePage.lock()
}

async unlock() {
await this.lockPage.unlock(this.password)
}
}
6 changes: 6 additions & 0 deletions wallets/metamask/src/pages/CrashPage/page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Selectors from './selectors'

export class CrashPage {
static readonly selectors = Selectors
readonly selectors = Selectors
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const container = 'section.error-page'

export const CrashPageSelectors = {
export default {
header: `${container} > .error-page__header`,
errors: `${container} > .error-page__details li`
}
1 change: 1 addition & 0 deletions wallets/metamask/src/pages/HomePage/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lock'
7 changes: 7 additions & 0 deletions wallets/metamask/src/pages/HomePage/actions/lock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Page } from '@playwright/test'
import Selectors from '../selectors'

export async function lock(page: Page) {
await page.locator(Selectors.accountMenu.accountMenuButton).click()
await page.locator(Selectors.accountMenu.lockButton).click()
}
18 changes: 18 additions & 0 deletions wallets/metamask/src/pages/HomePage/page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Page } from '@playwright/test'
import { lock } from './actions'
import Selectors from './selectors'

export class HomePage {
static readonly selectors = Selectors
readonly selectors = Selectors

readonly page: Page

constructor(page: Page) {
this.page = page
}

async lock() {
await lock(this.page)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createDataTestSelector } from '../../utils/selectors/createDataTestSelector'
import { createDataTestSelector } from '../../../utils/selectors/createDataTestSelector'

const container = '.account-menu'
const accountMenu = {
Expand Down
1 change: 1 addition & 0 deletions wallets/metamask/src/pages/LockPage/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './unlock'
10 changes: 10 additions & 0 deletions wallets/metamask/src/pages/LockPage/actions/unlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Page } from '@playwright/test'
import { waitForSpinnerToVanish } from '../../../utils/waitForSpinnerToVanish'
import Selectors from '../selectors'

export async function unlock(page: Page, password: string) {
await page.locator(Selectors.passwordInput).fill(password)
await page.locator(Selectors.submitButton).click()

await waitForSpinnerToVanish(page)
}
18 changes: 18 additions & 0 deletions wallets/metamask/src/pages/LockPage/page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Page } from '@playwright/test'
import { unlock } from './actions'
import Selectors from './selectors'

export class LockPage {
static readonly selectors = Selectors
readonly selectors = Selectors

readonly page: Page

constructor(page: Page) {
this.page = page
}

async unlock(password: string) {
await unlock(this.page, password)
}
}
6 changes: 6 additions & 0 deletions wallets/metamask/src/pages/LockPage/selectors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createDataTestSelector } from '../../../utils/selectors/createDataTestSelector'

export default {
passwordInput: createDataTestSelector('unlock-password'),
submitButton: createDataTestSelector('unlock-submit')
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BrowserContext } from '@playwright/test'
import { getNotificationPage } from '../utils/getNotificationPage'
import { getNotificationPage } from '../utils'

export async function connectToDapp(context: BrowserContext, extensionId: string) {
const notificationPage = await getNotificationPage(context, extensionId)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './connectToDapp'
14 changes: 14 additions & 0 deletions wallets/metamask/src/pages/NotificationPage/page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Page } from '@playwright/test'
import { connectToDapp } from './actions'

export class NotificationPage {
readonly page: Page

constructor(page: Page) {
this.page = page
}

async connectToDapp(extensionId: string) {
await connectToDapp(this.page.context(), extensionId)
}
}
1 change: 1 addition & 0 deletions wallets/metamask/src/pages/NotificationPage/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './getNotificationPage'
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Page } from '@playwright/test'
import { SecretRecoveryPhrasePageSelectors } from '../../selectors'
import Selectors from '../../selectors'

const StepSelectors = SecretRecoveryPhrasePageSelectors.recoveryStep
const StepSelectors = Selectors.SecretRecoveryPhrasePageSelectors.recoveryStep

export async function confirmSecretRecoveryPhrase(page: Page, seedPhrase: string) {
const seedPhraseWords = seedPhrase.split(' ')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Page } from '@playwright/test'
import { SecretRecoveryPhrasePageSelectors } from '../../selectors'
import Selectors from '../../selectors'

const StepSelectors = SecretRecoveryPhrasePageSelectors.passwordStep
const StepSelectors = Selectors.SecretRecoveryPhrasePageSelectors.passwordStep

export async function createPassword(page: Page, password: string) {
await page.locator(StepSelectors.passwordInput).fill(password)
Expand Down
17 changes: 6 additions & 11 deletions wallets/metamask/src/pages/OnboardingPage/actions/importWallet.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import type { Page } from '@playwright/test'
import {
AnalyticsPageSelectors,
GetStartedPageSelectors,
PinExtensionPageSelectors,
WalletCreationSuccessPageSelectors
} from '../selectors'
import Selectors from '../selectors'
import { confirmSecretRecoveryPhrase, createPassword } from './helpers'

export async function importWallet(page: Page, seedPhrase: string, password: string) {
await page.locator(GetStartedPageSelectors.importWallet).click()
await page.locator(Selectors.GetStartedPageSelectors.importWallet).click()

await page.locator(AnalyticsPageSelectors.optOut).click()
await page.locator(Selectors.AnalyticsPageSelectors.optOut).click()

// Secret Recovery Phrase Page
await confirmSecretRecoveryPhrase(page, seedPhrase)
await createPassword(page, password)

await page.locator(WalletCreationSuccessPageSelectors.confirmButton).click()
await page.locator(Selectors.WalletCreationSuccessPageSelectors.confirmButton).click()

await page.locator(PinExtensionPageSelectors.nextButton).click()
await page.locator(PinExtensionPageSelectors.confirmButton).click()
await page.locator(Selectors.PinExtensionPageSelectors.nextButton).click()
await page.locator(Selectors.PinExtensionPageSelectors.confirmButton).click()
}
6 changes: 4 additions & 2 deletions wallets/metamask/src/pages/OnboardingPage/page.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { Page } from '@playwright/test'
import { importWallet } from './actions'
import * as Selectors from './selectors'
import Selectors from './selectors'

export class OnboardingPage {
readonly page: Page
static readonly selectors = Selectors
readonly selectors = Selectors

readonly page: Page

constructor(page: Page) {
this.page = page
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import SecretRecoveryPhrasePageSelectors from './secretRecoveryPhrasePage'
import WalletCreationSuccessPageSelectors from './walletCreationSuccessPage'

// biome-ignore format: empty lines should be preserved
export {
export default {
// Initial Welcome Page
GetStartedPageSelectors,

Expand Down
4 changes: 4 additions & 0 deletions wallets/metamask/src/pages/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export * from './OnboardingPage/page'
export * from './CrashPage/page'
export * from './LockPage/page'
export * from './HomePage/page'
export * from './NotificationPage/page'
3 changes: 0 additions & 3 deletions wallets/metamask/src/selectors/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
export * from './unlocking'
export * from './loading'
export * from './crash'
export * from './main'
3 changes: 0 additions & 3 deletions wallets/metamask/src/selectors/main/index.ts

This file was deleted.

6 changes: 0 additions & 6 deletions wallets/metamask/src/selectors/unlocking/index.ts

This file was deleted.

34 changes: 0 additions & 34 deletions wallets/metamask/src/utils/getExtensionId.ts

This file was deleted.

15 changes: 0 additions & 15 deletions wallets/metamask/test/e2e/getExtensionId.spec.ts

This file was deleted.

16 changes: 0 additions & 16 deletions wallets/metamask/test/e2e/lock.spec.ts

This file was deleted.

Loading

0 comments on commit 46b9047

Please sign in to comment.