Skip to content

Commit

Permalink
Phantom - Functions updates and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zgz2020 committed Dec 16, 2024
1 parent 306e2a0 commit 5cef9b2
Show file tree
Hide file tree
Showing 29 changed files with 256 additions and 245 deletions.
24 changes: 0 additions & 24 deletions wallets/phantom/src/cypress/Phantom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,30 +142,6 @@ export default class Phantom {
return true
}

/**
* Switches to the specified network.
* @param options - Object containing the network name and testnet flag
* @param options.networkName - The name of the network to switch to
* @param options.isTestnet - Whether the network is a testnet (default: false)
* @returns True if the switch was successful, false otherwise
*/
async switchNetwork({
networkName,
isTestnet = false
}: {
networkName: string
isTestnet?: boolean
}): Promise<boolean> {
return await this.phantomPlaywright
.switchNetwork(networkName, isTestnet)
.then(() => {
return true
})
.catch(() => {
return false
})
}

/**
* Adds a new token to Phantom.
* @returns True if the token was added successfully
Expand Down
11 changes: 0 additions & 11 deletions wallets/phantom/src/cypress/configureSynpress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,6 @@ export default function configureSynpress(

// Network
getNetwork: () => phantom?.getNetwork(),
switchNetwork: ({
networkName,
isTestnet = false
}: {
networkName: string
isTestnet?: boolean
}) =>
phantom?.switchNetwork({
networkName,
isTestnet
}),

// Token
addNewToken: () => phantom?.addNewToken(),
Expand Down
11 changes: 0 additions & 11 deletions wallets/phantom/src/cypress/support/synpressCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ declare global {
getAccountAddress(): Chainable<string>
resetAccount(): Chainable<void>

switchNetwork(networkName: string, isTestnet?: boolean): Chainable<void>

addNewToken(): Chainable<void>
approveTokenPermission(options?: {
spendLimit?: number | 'max'
Expand Down Expand Up @@ -171,15 +169,6 @@ export default function synpressCommandsForPhantom(): void {
return cy.task('getNetwork')
})

/**
* Switches to a different network
* @param networkName - The name of the network to switch to
* @param isTestnet - Whether the network is a testnet
*/
Cypress.Commands.add('switchNetwork', (networkName: string, isTestnet = false) => {
return cy.task('switchNetwork', { networkName, isTestnet })
})

// Token

/**
Expand Down
43 changes: 5 additions & 38 deletions wallets/phantom/src/playwright/Phantom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { GasSettings } from '../type/GasSettings'
import type { Networks } from '../type/Networks'
import { PhantomAbstract } from '../type/PhantomAbstract'
import { CrashPage, HomePage, LockPage, NotificationPage, OnboardingPage } from './pages'
import { SettingsPage } from './pages/SettingsPage/page'

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

Expand Down Expand Up @@ -58,14 +57,6 @@ export class Phantom extends PhantomAbstract {
*/
readonly notificationPage: NotificationPage

/**
* This property can be used to access selectors for the settings page.
*
* @public
* @readonly
*/
readonly settingsPage: SettingsPage

/**
* Creates an instance of Phantom.
*
Expand All @@ -87,7 +78,6 @@ export class Phantom extends PhantomAbstract {
this.lockPage = new LockPage(page)
this.homePage = new HomePage(page)
this.notificationPage = new NotificationPage(page)
this.settingsPage = new SettingsPage(page)
}

/**
Expand Down Expand Up @@ -121,7 +111,9 @@ export class Phantom extends PhantomAbstract {
/**
* Imports a wallet using the given private key.
*
* @param network - Network that the wallet belongs to.
* @param privateKey - The private key to import.
* @param privateKey - Name given to the new wallet/account.
*/
async importWalletFromPrivateKey(
network: 'solana' | 'ethereum' | 'base' | 'polygon' | 'bitcoin',
Expand All @@ -143,34 +135,25 @@ export class Phantom extends PhantomAbstract {
/**
* Gets the address of the currently selected account.
*
* @param network - Network that the address belongs to.
* @returns The account address.
*/
async getAccountAddress(network: Networks): Promise<string> {
return await this.homePage.getAccountAddress(network)
}

/**
* Switches to the specified network.
*
* @param networkName - The name of the network to switch to.
* @param isTestnet - Whether the network is a testnet. Default is false.
*/
async switchNetwork(networkName: string, isTestnet = false): Promise<void> {
await this.homePage.switchNetwork(networkName, isTestnet)
}

/**
* Connects Phantom to a dapp.
*
* @param accounts - Optional array of account addresses to connect.
* @throws {Error} If extensionId is not set.
*/
async connectToDapp(accounts?: string[]): Promise<void> {
async connectToDapp(account?: string): Promise<void> {
if (!this.extensionId) {
throw NO_EXTENSION_ID_ERROR
}

await this.notificationPage.connectToDapp(this.extensionId, accounts)
await this.notificationPage.connectToDapp(this.extensionId, account)
}

/**
Expand Down Expand Up @@ -329,22 +312,6 @@ export class Phantom extends PhantomAbstract {
await this.homePage.resetAccount()
}

/**
* Enables eth_sign (unsafe).
*/
async unsafe_enableEthSign(): Promise<void> {
await this.homePage.openSettings()
await this.settingsPage.enableEthSign()
}

/**
* Disables eth_sign.
*/
async disableEthSign(): Promise<void> {
await this.homePage.openSettings()
await this.settingsPage.disableEthSign()
}

/**
* Adds a new token.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from './lock'
export * from './importWalletFromPrivateKey'
export * from './switchAccount'
export * from './settings'
export * from './switchNetwork'
export * from './toggleShowTestNetworks'
export * from './addNewAccount'
export * from './transactionDetails'
Expand Down
6 changes: 4 additions & 2 deletions wallets/phantom/src/playwright/pages/HomePage/actions/lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { Page } from '@playwright/test'
import Selectors from '../../../../selectors/pages/HomePage'

export async function lock(page: Page) {
await page.locator(Selectors.threeDotsMenu.threeDotsButton).click()
await page.locator(Selectors.threeDotsMenu.lockButton).click()
await page.locator(Selectors.accountMenu.accountButton).click()
await page.locator(Selectors.accountMenu.settings).click()

await page.locator(Selectors.settings.lockWallet).click()
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,11 @@
import type { Page } from '@playwright/test'
import Selectors from '../../../../selectors/pages/HomePage'
import { allTextContents } from '../../../utils/allTextContents'
import { closeRecoveryPhraseReminder } from './popups'

async function openTestnetSection(page: Page) {
export async function openTestnetSection(page: Page) {
const toggleButtonLocator = page.locator(Selectors.networkDropdown.showTestNetworksToggle)
const classes = await toggleButtonLocator.getAttribute('class')
if (classes?.includes('toggle-button--off')) {
await toggleButtonLocator.click()
await page.locator(Selectors.networkDropdown.toggleOn).isChecked()
}
}

export async function switchNetwork(page: Page, networkName: string, includeTestNetworks: boolean) {
await page.locator(Selectors.networkDropdown.dropdownButton).click()

if (includeTestNetworks) {
await openTestnetSection(page)
}

const networkLocators = await page.locator(Selectors.networkDropdown.networks).all()
const networkNames = await allTextContents(networkLocators)

const seekedNetworkNameIndex = networkNames.findIndex(
(name) => name.toLocaleLowerCase() === networkName.toLocaleLowerCase()
)

const seekedNetworkLocator = seekedNetworkNameIndex >= 0 && networkLocators[seekedNetworkNameIndex]

if (!seekedNetworkLocator) {
throw new Error(`[SwitchNetwork] Network with name ${networkName} not found`)
}

await seekedNetworkLocator.click()

// TODO: This is not really needed if we do `phantom.toggleDismissSecretRecoveryPhraseReminder()` by default. Figure this out!
await closeRecoveryPhraseReminder(page)
}
5 changes: 0 additions & 5 deletions wallets/phantom/src/playwright/pages/HomePage/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
renameAccount,
settings,
switchAccount,
switchNetwork,
toggleShowTestNetworks,
transactionDetails
} from './actions'
Expand Down Expand Up @@ -77,10 +76,6 @@ export class HomePage {
await settings.advanced.toggleDismissSecretRecoveryPhraseReminder(this.page)
}

async switchNetwork(networkName: string, isTestnet: boolean) {
await switchNetwork(this.page, networkName, isTestnet)
}

async openTransactionDetails(txIndex: number) {
await transactionDetails.open(this.page, txIndex)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,15 @@
import type { Locator, Page } from '@playwright/test'
import type { Page } from '@playwright/test'
import Selectors from '../../../../selectors/pages/NotificationPage'
import { allTextContents } from '../../../utils/allTextContents'

async function selectAccounts(accountsToSelect: string[], accountLocators: Locator[], availableAccountNames: string[]) {
for (const account of accountsToSelect) {
const accountNameIndex = availableAccountNames.findIndex((name) => name.startsWith(account))
if (accountNameIndex < 0) throw new Error(`[ConnectToDapp] Account with name ${account} not found`)
await accountLocators[accountNameIndex]?.locator(Selectors.ConnectPage.accountCheckbox).check()
}
}

async function connectMultipleAccounts(notificationPage: Page, accounts: string[]) {
// Wait for the accounts to be loaded as 'all()' doesnt not wait for the results - https://playwright.dev/docs/api/class-locator#locator-all
// Additionally disable default account to reuse necessary delay
await notificationPage
.locator(Selectors.ConnectPage.accountOption)
.locator(Selectors.ConnectPage.accountCheckbox)
.last()
.setChecked(false)

const accountLocators = await notificationPage.locator(Selectors.ConnectPage.accountOption).all()
const accountNames = await allTextContents(accountLocators)

await selectAccounts(accounts, accountLocators, accountNames)
}
import { switchAccount } from '../../HomePage/actions'

async function confirmConnection(notificationPage: Page) {
// Click `Next`
await notificationPage.locator(Selectors.ActionFooter.confirmActionButton).click()
// Click `Connect`
await notificationPage.locator(Selectors.ActionFooter.confirmActionButton).click()
await notificationPage.locator(Selectors.ActionFooter.connectActionButton).click()
}

// By default, only the last account will be selected. If you want to select a specific account, pass `accounts` parameter.
export async function connectToDapp(notificationPage: Page, accounts?: string[]) {
if (accounts && accounts.length > 0) {
await connectMultipleAccounts(notificationPage, accounts)
// By default, the last account will be selected. If you want to select a specific account, pass `account` parameter.
export async function connectToDapp(notificationPage: Page, account?: string) {
if (account) {
await switchAccount(notificationPage, account)
}

await confirmConnection(notificationPage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ const signMessage = async (notificationPage: Page) => {
}

const rejectMessage = async (notificationPage: Page) => {
await notificationPage.locator(Selectors.ActionFooter.rejectActionButton).click()
await notificationPage.locator(Selectors.ActionFooter.cancelActionButton).click()
}

const signMessageWithRisk = async (notificationPage: Page) => {
await signMessage(notificationPage)

await notificationPage.locator(Selectors.SignaturePage.riskModal.signButton).click()
await notificationPage.locator(Selectors.SignaturePage.riskModal.proceedAnyway).click()
await notificationPage.locator(Selectors.SignaturePage.riskModal.confirmUnsafe).click()
await notificationPage.locator(Selectors.SignaturePage.riskModal.acknowledgeRisks).click()
await notificationPage.locator(Selectors.SignaturePage.riskModal.reconfirmUnsafe).click()
}

export const signSimpleMessage = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const signMessage = async (notificationPage: Page) => {
}

const rejectMessage = async (notificationPage: Page) => {
await notificationPage.locator(Selectors.ActionFooter.rejectActionButton).click()
await notificationPage.locator(Selectors.ActionFooter.cancelActionButton).click()
}

// Used for:
Expand Down
4 changes: 2 additions & 2 deletions wallets/phantom/src/playwright/pages/NotificationPage/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export class NotificationPage {
this.page = page
}

async connectToDapp(extensionId: string, accounts?: string[]) {
async connectToDapp(extensionId: string, account?: string) {
const notificationPage = await getNotificationPageAndWaitForLoad(this.page.context(), extensionId)

await connectToDapp(notificationPage, accounts)
await connectToDapp(notificationPage, account)
}

// TODO: Revisit this logic in the future to see if we can increase the performance by utilizing `Promise.race`.
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

22 changes: 0 additions & 22 deletions wallets/phantom/src/playwright/pages/SettingsPage/page.ts

This file was deleted.

Loading

0 comments on commit 5cef9b2

Please sign in to comment.