Skip to content

Commit

Permalink
Phantom support for Playwright
Browse files Browse the repository at this point in the history
  • Loading branch information
zgz2020 committed Feb 19, 2025
1 parent b20c248 commit b341e2d
Show file tree
Hide file tree
Showing 22 changed files with 111 additions and 179 deletions.
11 changes: 1 addition & 10 deletions wallets/phantom/src/playwright/Phantom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { BrowserContext, Page } from '@playwright/test'
import type { GasSettings } from '../type/GasSettings'
import type { Networks } from '../type/Networks'
import { PhantomAbstract } from '../type/PhantomAbstract'
import { CrashPage, HomePage, NotificationPage, OnboardingPage, UnlockPage } from './pages'
import { HomePage, NotificationPage, OnboardingPage, UnlockPage } from './pages'

const NO_EXTENSION_ID_ERROR = new Error('Phantom extensionId is not set')

Expand All @@ -16,14 +16,6 @@ const NO_EXTENSION_ID_ERROR = new Error('Phantom extensionId is not set')
* @extends PhantomAbstract
*/
export class Phantom extends PhantomAbstract {
/**
* This property can be used to access selectors for the crash page.
*
* @public
* @readonly
*/
readonly crashPage: CrashPage

/**
* This property can be used to access selectors for the onboarding page.
*
Expand Down Expand Up @@ -72,7 +64,6 @@ export class Phantom extends PhantomAbstract {
) {
super(password, extensionId)

this.crashPage = new CrashPage()
this.onboardingPage = new OnboardingPage(page)
this.unlockPage = new UnlockPage(page)
this.homePage = new HomePage(page)
Expand Down
45 changes: 1 addition & 44 deletions wallets/phantom/src/playwright/fixtures/phantomFixtures.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'node:path'
import { type Page, chromium, expect } from '@playwright/test'
import { type Page, chromium } from '@playwright/test'
import { test as base } from '@playwright/test'
import {
CACHE_DIR_NAME,
Expand All @@ -19,8 +19,6 @@ type PhantomFixtures = {
phantom: Phantom
extensionId: string
phantomPage: Page
aavePage: Page
solanaSandboxPage: Page
}

// If setup phantomPage in a fixture, browser does not handle it properly (even if ethereum.isConnected() is true, it's not reflected on the page).
Expand Down Expand Up @@ -107,47 +105,6 @@ export const phantomFixtures = (walletSetup: ReturnType<typeof defineWalletSetup
page: async ({ page }, use) => {
await page.goto('/')

await use(page)
},
aavePage: async ({ page, phantom }, use) => {
await page.goto('https://app.aave.com')

await phantom.toggleTestnetMode()

await page.locator('button#settings-button').click()
await page.locator('li:has-text("Testnet mode")').click()
await expect(page.getByRole('button', { name: 'TESTNET' })).toBeVisible()

await page.getByRole('button', { name: 'Connect wallet' }).first().click()
await page.getByRole('button', { name: 'Phantom' }).click()

await phantom.connectToDapp()
await phantom.page.waitForTimeout(1_000)
await phantom.closeUnsupportedNetworkWarning()

await expect(page.getByText('0xf3...2266'), '"0xf3...2266" should be visible').toBeVisible()

await use(page)
},
solanaSandboxPage: async ({ page, phantom }, use) => {
await phantom.page.waitForTimeout(1_000)
await phantom.importWalletFromPrivateKey(
'solana',
'XQaKFLLSKbzpVzmfJrj4yUjAyFy2Eu7JcNdbPdnLuod2Uw3yf3tjGd4ha1DBfFdjkZFX1PZg3knth2Tz2tvd8C4'
)

await phantom.toggleTestnetMode()

await page.goto('https://r3byv.csb.app/')
await page.locator('a:has-text("Yes, proceed to preview")').click()
await page.getByRole('button', { name: 'Connect to Phantom' }).click()

await phantom.connectToDapp()

await page.getByRole('button', { name: 'Clear Logs' }).click()

await expect(page.getByText('Click a button and watch magic happen...')).toBeVisible()

await use(page)
}
})
Expand Down
6 changes: 0 additions & 6 deletions wallets/phantom/src/playwright/pages/CrashPage/page.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './popups'
export * from './lock'
export * from './importWalletFromPrivateKey'
export * from './switchAccount'
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ import Selectors from '../../../../selectors/pages/NotificationPage'
import type { GasSettings } from '../../../../type/GasSettings'
import { transaction } from './transaction'

const editTokenPermission = async (notificationPage: Page, customSpendLimit: 'max' | number) => {
if (customSpendLimit === 'max') {
await notificationPage.locator(Selectors.PermissionPage.approve.maxButton).click()
return
}

await notificationPage
.locator(Selectors.PermissionPage.approve.customSpendingCapInput)
.fill(customSpendLimit.toString())
}

const approveTokenPermission = async (notificationPage: Page, gasSetting: GasSettings) => {
// Approve flow is identical to the confirm transaction flow after we click the "Next" button.
await transaction.confirm(notificationPage, gasSetting)
Expand All @@ -24,7 +13,6 @@ const rejectTokenPermission = async (notificationPage: Page) => {
}

export const approvePermission = {
editTokenPermission,
approve: approveTokenPermission,
reject: rejectTokenPermission
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ export * from './closeUnsupportedNetworkWarning'
export * from './connectToDapp'
export * from './signSimpleMessage'
export * from './signStructuredMessage'
export * from './token'
export * from './transaction'

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class NotificationPage {
await approvePermission.reject(notificationPage)
}

async closeUnsupportedNetworkWarning(extensionId: string, account?: string) {
async closeUnsupportedNetworkWarning(extensionId: string) {
const notificationPage = await getNotificationPageAndWaitForLoad(this.page.context(), extensionId)

await closeUnsupportedNetworkWarning(notificationPage)
Expand Down
1 change: 0 additions & 1 deletion wallets/phantom/src/playwright/pages/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './OnboardingPage/page'
export * from './CrashPage/page'
export * from './UnlockPage/page'
export * from './HomePage/page'
export * from './NotificationPage/page'
10 changes: 0 additions & 10 deletions wallets/phantom/src/playwright/utils/clickLocatorIfCondition.ts

This file was deleted.

1 change: 0 additions & 1 deletion wallets/phantom/src/selectors/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from './loading'
export * from './error'

export { default as crashPage } from './pages/CrashPage'
export { default as homePage } from './pages/HomePage'
export { default as unlockPage } from './pages/UnlockPage'
export { default as notificationPage } from './pages/NotificationPage'
Expand Down
36 changes: 36 additions & 0 deletions wallets/phantom/test/playwright/commonSteps/aaveSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { type Page, expect } from '@playwright/test'
import type { Phantom } from '../../../src/playwright'

export const aaveSetup = async (page: Page, phantom: Phantom) => {
await page.goto('https://app.aave.com')

await phantom.toggleTestnetMode()

await page.locator('button#settings-button').click()
await page.locator('li:has-text("Testnet mode")').click()
await expect(page.getByRole('button', { name: 'TESTNET' })).toBeVisible()

await page.getByRole('button', { name: 'Connect wallet' }).first().click()
await page.getByRole('button', { name: 'Phantom' }).click()

await phantom.connectToDapp()
await phantom.page.waitForTimeout(1_000)
await phantom.closeUnsupportedNetworkWarning()

await expect(page.getByText('0xf3...2266'), '"0xf3...2266" should be visible').toBeVisible()

await page.goto(
'https://app.aave.com/reserve-overview/?underlyingAsset=0xff34b3d4aee8ddcd6f9afffb6fe49bd371b8a357&marketName=proto_sepolia_v3'
)

await page.getByRole('button', { name: 'Supply' }).click()
await page.locator('input[aria-label="amount input"]').fill('1')

await expect(
page.getByRole('button', {
name: 'Supply DAI'
})
).toBeDisabled()

await page.locator('button:has-text("Approve DAI to continue")').click()
}
22 changes: 22 additions & 0 deletions wallets/phantom/test/playwright/commonSteps/solanaSandboxSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { type Page, expect } from '@playwright/test'
import type { Phantom } from '../../../src/playwright'

export const solanaSandboxSetup = async (page: Page, phantom: Phantom) => {
await phantom.page.waitForTimeout(1_000)
await phantom.importWalletFromPrivateKey(
'solana',
'XQaKFLLSKbzpVzmfJrj4yUjAyFy2Eu7JcNdbPdnLuod2Uw3yf3tjGd4ha1DBfFdjkZFX1PZg3knth2Tz2tvd8C4'
)

await phantom.toggleTestnetMode()

await page.goto('https://r3byv.csb.app/')
await page.locator('a:has-text("Yes, proceed to preview")').click()
await page.getByRole('button', { name: 'Connect to Phantom' }).click()

await phantom.connectToDapp()

await page.getByRole('button', { name: 'Clear Logs' }).click()

await expect(page.getByText('Click a button and watch magic happen...')).toBeVisible()
}
23 changes: 8 additions & 15 deletions wallets/phantom/test/playwright/e2e/approveTokenPermission.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
import { aaveSetup } from '../commonSteps/aaveSetup'
import synpress from '../synpress'

const test = synpress

const { expect } = test

test('should approve token with the default limit', async ({ aavePage, phantom }) => {
test('should approve token with the default limit', async ({ page, phantom }) => {
test.setTimeout(80_000)

await aavePage.goto(
'https://app.aave.com/reserve-overview/?underlyingAsset=0xff34b3d4aee8ddcd6f9afffb6fe49bd371b8a357&marketName=proto_sepolia_v3'
)

await aavePage.getByRole('button', { name: 'Supply' }).click()
await aavePage.locator('input[aria-label="amount input"]').fill('1')

const supplyDaiLocator = aavePage.getByRole('button', {
name: 'Supply DAI'
})
await expect(supplyDaiLocator).toBeDisabled()

await aavePage.locator('button:has-text("Approve DAI to continue")').click()
await aaveSetup(page, phantom)

await phantom.approveTokenPermission()

await expect(supplyDaiLocator).toBeEnabled({ timeout: 10_000 })
await expect(
page.getByRole('button', {
name: 'Supply DAI'
})
).toBeEnabled({ timeout: 10_000 })
})
10 changes: 5 additions & 5 deletions wallets/phantom/test/playwright/e2e/confirmSignature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const test = synpress
const { expect } = test

test('should confirm `personal_sign`', async ({ page, phantom }) => {
connectPhantomToTestDapp(page, phantom)
await connectPhantomToTestDapp(page, phantom)

await page.locator('#personalSign').click()

Expand All @@ -18,7 +18,7 @@ test('should confirm `personal_sign`', async ({ page, phantom }) => {
})

test('should confirm `eth_signTypedData`', async ({ page, phantom }) => {
connectPhantomToTestDapp(page, phantom)
await connectPhantomToTestDapp(page, phantom)

await page.locator('#signTypedData').click()

Expand All @@ -34,7 +34,7 @@ test('should confirm `eth_signTypedData`', async ({ page, phantom }) => {
})

test('should confirm `eth_signTypedData_v3`', async ({ page, phantom }) => {
connectPhantomToTestDapp(page, phantom)
await connectPhantomToTestDapp(page, phantom)

await page.locator('#signTypedDataV3').click()

Expand All @@ -50,7 +50,7 @@ test('should confirm `eth_signTypedData_v3`', async ({ page, phantom }) => {
})

test('should confirm `eth_signTypedData_v4`', async ({ page, phantom }) => {
connectPhantomToTestDapp(page, phantom)
await connectPhantomToTestDapp(page, phantom)

await page.locator('#signTypedDataV4').click()

Expand All @@ -66,7 +66,7 @@ test('should confirm `eth_signTypedData_v4`', async ({ page, phantom }) => {
})

test('should confirm `eth_sign`', async ({ page, phantom }) => {
connectPhantomToTestDapp(page, phantom)
await connectPhantomToTestDapp(page, phantom)

await page.locator('#ethSign').click()

Expand Down
Loading

0 comments on commit b341e2d

Please sign in to comment.