Skip to content

Commit

Permalink
Phantom support - First Cypress.io tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zgz2020 committed Dec 24, 2024
1 parent 4f2082f commit a783db3
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 30 deletions.
27 changes: 12 additions & 15 deletions wallets/phantom/src/cypress/Phantom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class Phantom {
*/
async getAccount(): Promise<string> {
return await this.phantomExtensionPage
.locator(this.phantomPlaywright.homePage.selectors.accountMenu.accountButton)
.locator(this.phantomPlaywright.homePage.selectors.accountMenu.accountName)
.innerText()
}

Expand All @@ -45,14 +45,6 @@ export default class Phantom {
return await this.phantomPlaywright.getAccountAddress(network)
}

/**
* Gets the current network name.
* @returns The current network name
*/
async getNetwork(): Promise<string> {
return await this.phantomExtensionPage.locator(this.phantomPlaywright.homePage.selectors.currentNetwork).innerText()
}

/**
* Connects Phantom to a dApp.
* @param accounts - Optional array of account addresses to connect
Expand Down Expand Up @@ -91,7 +83,7 @@ export default class Phantom {
async addNewAccount(accountName: string): Promise<boolean> {
await this.phantomPlaywright.addNewAccount(accountName)
await expect(
this.phantomExtensionPage.locator(this.phantomPlaywright.homePage.selectors.accountMenu.accountButton)
this.phantomExtensionPage.locator(this.phantomPlaywright.homePage.selectors.accountMenu.accountName)
).toHaveText(accountName)
return true
}
Expand All @@ -104,7 +96,7 @@ export default class Phantom {
async switchAccount(accountName: string): Promise<boolean> {
await this.phantomPlaywright.switchAccount(accountName)
await expect(
this.phantomExtensionPage.locator(this.phantomPlaywright.homePage.selectors.accountMenu.accountButton)
this.phantomExtensionPage.locator(this.phantomPlaywright.homePage.selectors.accountMenu.accountName)
).toHaveText(accountName)
return true
}
Expand All @@ -124,10 +116,6 @@ export default class Phantom {
newAccountName: string
}): Promise<boolean> {
await this.phantomPlaywright.renameAccount(currentAccountName, newAccountName)
await this.phantomExtensionPage.locator(HomePageSelectors.threeDotsMenu.accountDetailsCloseButton).click()
await expect(
this.phantomExtensionPage.locator(this.phantomPlaywright.homePage.selectors.accountMenu.accountButton)
).toHaveText(newAccountName)
return true
}

Expand Down Expand Up @@ -253,6 +241,15 @@ export default class Phantom {
return true
}

/**
* Navigates back to the home page.
* @returns True if the navigation was successful
*/
async goToHomePage(): Promise<boolean> {
await this.phantomPlaywright.goToHomePage()
return true
}

/**
* Navigates back to the home page.
* @returns True if the navigation was successful
Expand Down
4 changes: 1 addition & 3 deletions wallets/phantom/src/cypress/configureSynpress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ export default function configureSynpress(
}) => phantom?.renameAccount({ currentAccountName, newAccountName }),
resetApp: () => phantom?.resetApp(),

// Network
getNetwork: () => phantom?.getNetwork(),

// Token
approveTokenPermission: (options?: {
spendLimit?: number | 'max'
Expand All @@ -149,6 +146,7 @@ export default function configureSynpress(
toggleTestnetMode: () => phantom?.toggleTestnetMode(),

// Others
goToHomePage: () => phantom?.goToHomePage(),
goBackToHomePage: () => phantom?.goBackToHomePage(),
openSettings: () => phantom?.openSettings()
})
Expand Down
2 changes: 2 additions & 0 deletions wallets/phantom/src/cypress/support/importPhantomWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export default async function importPhantomWallet(port: number, importDefaultWal

if (importDefaultWallet) await phantom.importWallet(SEED_PHRASE)

await phantom.goToHomePage()

cypressPage = context.pages()[extensionPageIndex === 1 ? 0 : 1] as Page
await cypressPage.bringToFront()
}
Expand Down
18 changes: 8 additions & 10 deletions wallets/phantom/src/cypress/support/synpressCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ declare global {
importWalletFromPrivateKey(privateKey: string): Chainable<void>

getAccount(): Chainable<string>
getNetwork(): Chainable<string>

connectToDapp(accounts?: string[]): Chainable<void>

Expand Down Expand Up @@ -46,6 +45,7 @@ declare global {

toggleTestnetMode(): Chainable<void>

goToHomePage(): Chainable<void>
goBackToHomePage(): Chainable<void>
openSettings(): Chainable<void>
}
Expand Down Expand Up @@ -151,15 +151,6 @@ export default function synpressCommandsForPhantom(): void {
return cy.task('resetApp')
})

// Network

/**
* Gets the current network
*/
Cypress.Commands.add('getNetwork', () => {
return cy.task('getNetwork')
})

// Token

/**
Expand Down Expand Up @@ -239,6 +230,13 @@ export default function synpressCommandsForPhantom(): void {
return cy.task('rejectTransaction')
})

/**
* Navigates to the Phantom extension home page
*/
Cypress.Commands.add('goToHomePage', () => {
return cy.task('goToHomePage')
})

/**
* Goes back to the home page
*/
Expand Down
11 changes: 11 additions & 0 deletions wallets/phantom/src/playwright/Phantom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,17 @@ export class Phantom extends PhantomAbstract {
await this.notificationPage.rejectTokenPermission(this.extensionId)
}

/**
* Navigates to the home page or wallet dashboard.
*/
async goToHomePage(): Promise<void> {
if (!this.extensionId) {
throw NO_EXTENSION_ID_ERROR
}

await this.homePage.goToHomePage(this.extensionId)
}

/**
* Navigates back to the home page.
*/
Expand Down
4 changes: 4 additions & 0 deletions wallets/phantom/src/playwright/pages/HomePage/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export class HomePage {
this.page = page
}

async goToHomePage(extensionId: string) {
await this.page.goto(`chrome-extension://${extensionId}/popup.html`)
}

async goBackToHomePage() {
await this.page.locator(Selectors.settings.closeSettingsButton).click()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function importWallet(page: Page, seedPhrase: string, password: str
await expect(
page.locator(Selectors.SecretRecoveryPhrasePageSelectors.viewAccountsButton),
'Import accounts success screen should be visible'
).toBeVisible({ timeout: 10_000 })
).toBeVisible({ timeout: 30_000 })

await page.locator(Selectors.SecretRecoveryPhrasePageSelectors.continueButton).click()

Expand Down
5 changes: 5 additions & 0 deletions wallets/phantom/src/type/PhantomAbstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ export abstract class PhantomAbstract {
*/
abstract rejectTokenPermission(): void

/**
* Navigates to the home page of Phantom tab.
*/
abstract goToHomePage(): void

/**
* Goes back to the home page of Phantom tab.
*/
Expand Down
2 changes: 1 addition & 1 deletion wallets/phantom/test/cypress/addNewAccount.cy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
it.only('should add a new account with a specified name', () => {
it('should add a new account with a specified name', () => {
const accountName = 'Test Account 2'

cy.addNewAccount(accountName).then(() => {
Expand Down
10 changes: 10 additions & 0 deletions wallets/phantom/test/cypress/renameAccount.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const newAccountName = 'New Name'

it('should rename currently connected account with specified name', () => {
cy.addNewAccount(newAccountName).then(() => {
cy.renameAccount(newAccountName, 'Renaming test').then(() => {
cy.goToHomePage()
cy.getAccount().should('eq', 'Renaming test')
})
})
})
13 changes: 13 additions & 0 deletions wallets/phantom/test/cypress/switchAccount.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defaultAccount } from '../../src/cypress/constans'

it('should switch back to the `Account 1` account', () => {
const accountName = 'New Name'

cy.addNewAccount(accountName).then(() => {
cy.getAccount().should('eq', accountName)
})

cy.switchAccount(defaultAccount).then(() => {
cy.getAccount().should('eq', defaultAccount)
})
})

0 comments on commit a783db3

Please sign in to comment.