From f28edc1271e093a2bfca3098f4717984210b68c8 Mon Sep 17 00:00:00 2001 From: Mat <63294765+matstyler@users.noreply.github.com> Date: Sat, 27 Jul 2024 21:45:37 +0200 Subject: [PATCH] feat: Added renameAccount for Cypress (#1187) --- .../metamask/src/cypress/configureSynpress.ts | 25 +++++++++++++++++-- wallets/metamask/src/cypress/constans.ts | 1 + .../src/cypress/support/synpressCommands.ts | 8 +++++- wallets/metamask/src/playwright/MetaMask.ts | 5 ++-- .../pages/HomePage/actions/renameAccount.ts | 21 ++++++++++++++-- .../src/playwright/pages/HomePage/page.ts | 4 +-- .../metamask/test/cypress/renameAccount.cy.ts | 15 +++++++++++ .../metamask/test/cypress/switchAccount.cy.ts | 10 +++++--- .../metamask/test/cypress/switchNetwork.cy.ts | 6 ++++- .../test/playwright/e2e/renameAccount.spec.ts | 2 +- 10 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 wallets/metamask/src/cypress/constans.ts create mode 100644 wallets/metamask/test/cypress/renameAccount.cy.ts diff --git a/wallets/metamask/src/cypress/configureSynpress.ts b/wallets/metamask/src/cypress/configureSynpress.ts index 3397d0457..d90b036b9 100644 --- a/wallets/metamask/src/cypress/configureSynpress.ts +++ b/wallets/metamask/src/cypress/configureSynpress.ts @@ -1,6 +1,7 @@ import type { BrowserContext, Page } from '@playwright/test' import { expect } from '@playwright/test' import { ensureRdpPort } from '@synthetixio/synpress-core' +import Selectors from '../selectors/pages/HomePage' import getPlaywrightMetamask from './getPlaywrightMetamask' import importMetaMaskWallet from './support/importMetaMaskWallet' import { initMetaMask } from './support/initMetaMask' @@ -73,11 +74,11 @@ export default function configureSynpress(on: Cypress.PluginEvents, config: Cypr return await metamaskExtensionPage.locator(metamask.homePage.selectors.currentNetwork).innerText() }, - async connectToDapp() { + async connectToDapp(accounts?: string[]) { const metamask = getPlaywrightMetamask(context, metamaskExtensionPage, metamaskExtensionId) return metamask - .connectToDapp() + .connectToDapp(accounts) .then(() => true) .catch(() => false) }, @@ -106,6 +107,26 @@ export default function configureSynpress(on: Cypress.PluginEvents, config: Cypr return true }, + async renameAccount({ + currentAccountName, + newAccountName + }: { + currentAccountName: string + newAccountName: string + }) { + const metamask = getPlaywrightMetamask(context, metamaskExtensionPage, metamaskExtensionId) + + await metamask.renameAccount(currentAccountName, newAccountName) + + await metamaskExtensionPage.locator(Selectors.threeDotsMenu.accountDetailsCloseButton).click() + + await expect(metamaskExtensionPage.locator(metamask.homePage.selectors.accountMenu.accountButton)).toHaveText( + newAccountName + ) + + return true + }, + async switchNetwork({ networkName, isTestnet diff --git a/wallets/metamask/src/cypress/constans.ts b/wallets/metamask/src/cypress/constans.ts new file mode 100644 index 000000000..d94424e0b --- /dev/null +++ b/wallets/metamask/src/cypress/constans.ts @@ -0,0 +1 @@ +export const defaultAccount = 'Account 1' diff --git a/wallets/metamask/src/cypress/support/synpressCommands.ts b/wallets/metamask/src/cypress/support/synpressCommands.ts index 05245f806..0c4154d3e 100644 --- a/wallets/metamask/src/cypress/support/synpressCommands.ts +++ b/wallets/metamask/src/cypress/support/synpressCommands.ts @@ -15,9 +15,12 @@ declare global { getAccount(): Chainable getNetwork(): Chainable - connectToDapp(): Chainable + connectToDapp(accounts?: string[]): Chainable + addNewAccount(accountName: string): Chainable switchAccount(accountName: string): Chainable + renameAccount(currentAccountName: string, newAccountName: string): Chainable + switchNetwork(networkName: string, isTestnet?: boolean): Chainable } } @@ -40,6 +43,9 @@ export default function synpressCommands() { Cypress.Commands.add('switchAccount', (accountName: string) => { return cy.task('switchAccount', accountName) }) + Cypress.Commands.add('renameAccount', (currentAccountName: string, newAccountName: string) => { + return cy.task('renameAccount', { currentAccountName, newAccountName }) + }) Cypress.Commands.add('switchNetwork', (networkName: string, isTestnet = false) => { return cy.task('switchNetwork', { networkName, isTestnet }) }) diff --git a/wallets/metamask/src/playwright/MetaMask.ts b/wallets/metamask/src/playwright/MetaMask.ts index 94a278741..784f2322b 100644 --- a/wallets/metamask/src/playwright/MetaMask.ts +++ b/wallets/metamask/src/playwright/MetaMask.ts @@ -81,10 +81,11 @@ export class MetaMask extends MetaMaskAbstract { /** * Renames the currently selected account. * + * @param currentAccountName - The current account name. * @param newAccountName - The new name for the account. */ - async renameAccount(newAccountName: string) { - await this.homePage.renameAccount(newAccountName) + async renameAccount(currentAccountName: string, newAccountName: string) { + await this.homePage.renameAccount(currentAccountName, newAccountName) } /** diff --git a/wallets/metamask/src/playwright/pages/HomePage/actions/renameAccount.ts b/wallets/metamask/src/playwright/pages/HomePage/actions/renameAccount.ts index 466f7ded6..36cd7d69c 100644 --- a/wallets/metamask/src/playwright/pages/HomePage/actions/renameAccount.ts +++ b/wallets/metamask/src/playwright/pages/HomePage/actions/renameAccount.ts @@ -1,9 +1,26 @@ import type { Page } from '@playwright/test' import Selectors from '../../../../selectors/pages/HomePage' +import { allTextContents } from '../../../utils/allTextContents' -export async function renameAccount(page: Page, newAccountName: string) { +export async function renameAccount(page: Page, currentAccountName: string, newAccountName: string) { await page.locator(Selectors.accountMenu.accountButton).click() - await page.locator(Selectors.accountMenu.renameAccountMenu.listItemButton).nth(0).click() + + const accountNamesLocators = await page.locator(Selectors.accountMenu.accountNames).all() + + const accountNames = await allTextContents(accountNamesLocators) + + const seekedAccountNames = accountNames.filter( + (name) => name.toLocaleLowerCase() === currentAccountName.toLocaleLowerCase() + ) + + if (seekedAccountNames.length === 0) { + throw new Error(`[SwitchAccount] Account with name ${currentAccountName} not found`) + } + + // biome-ignore lint/style/noNonNullAssertion: this non-null assertion is intentional + const accountIndex = accountNames.indexOf(seekedAccountNames[0]!) + + await page.locator(Selectors.accountMenu.renameAccountMenu.listItemButton).nth(accountIndex).click() await page.locator(Selectors.threeDotsMenu.accountDetailsButton).click() await page.locator(Selectors.accountMenu.renameAccountMenu.renameButton).click() await page.locator(Selectors.accountMenu.renameAccountMenu.renameInput).fill(newAccountName) diff --git a/wallets/metamask/src/playwright/pages/HomePage/page.ts b/wallets/metamask/src/playwright/pages/HomePage/page.ts index 156de5e85..15ea8477b 100644 --- a/wallets/metamask/src/playwright/pages/HomePage/page.ts +++ b/wallets/metamask/src/playwright/pages/HomePage/page.ts @@ -38,8 +38,8 @@ export class HomePage { await addNewAccount(this.page, accountName) } - async renameAccount(newAccountName: string) { - await renameAccount(this.page, newAccountName) + async renameAccount(currentAccountName: string, newAccountName: string) { + await renameAccount(this.page, currentAccountName, newAccountName) } async getAccountAddress() { diff --git a/wallets/metamask/test/cypress/renameAccount.cy.ts b/wallets/metamask/test/cypress/renameAccount.cy.ts new file mode 100644 index 000000000..34bff2e2e --- /dev/null +++ b/wallets/metamask/test/cypress/renameAccount.cy.ts @@ -0,0 +1,15 @@ +import { defaultAccount } from '../../src/cypress/constans' + +const newAccountName = 'New Test Account Name' + +it('should rename currently connected account with specified name', () => { + cy.addNewAccount(newAccountName).then(() => { + cy.renameAccount(newAccountName, 'Renaming test').then(() => { + cy.getAccount().should('eq', 'Renaming test') + }) + }) +}) + +after(() => { + cy.switchAccount(defaultAccount) +}) diff --git a/wallets/metamask/test/cypress/switchAccount.cy.ts b/wallets/metamask/test/cypress/switchAccount.cy.ts index 43a8f798a..96578c244 100644 --- a/wallets/metamask/test/cypress/switchAccount.cy.ts +++ b/wallets/metamask/test/cypress/switchAccount.cy.ts @@ -1,11 +1,13 @@ +import { defaultAccount } from '../../src/cypress/constans' + it('should switch back to the `Account 1` account', () => { const accountName = 'New Account to test switch' cy.addNewAccount(accountName).then(() => { cy.getAccount().should('eq', accountName) - - cy.switchAccount('Account 1').then(() => { - cy.getAccount().should('eq', 'Account 1') - }) }) }) + +after(() => { + cy.switchAccount(defaultAccount) +}) diff --git a/wallets/metamask/test/cypress/switchNetwork.cy.ts b/wallets/metamask/test/cypress/switchNetwork.cy.ts index 205400e69..cd771ee5f 100644 --- a/wallets/metamask/test/cypress/switchNetwork.cy.ts +++ b/wallets/metamask/test/cypress/switchNetwork.cy.ts @@ -8,10 +8,14 @@ it('should switch network', () => { }) it('should switch network to the testnet', () => { - cy.getNetwork().should('eq', 'Linea Mainnet') + cy.getNetwork().should('eq', 'Ethereum Mainnet') const targetNetwork = 'Sepolia' cy.switchNetwork(targetNetwork, true).then(() => { cy.getNetwork().should('eq', targetNetwork) }) }) + +afterEach(() => { + cy.switchNetwork('Ethereum Mainnet') +}) diff --git a/wallets/metamask/test/playwright/e2e/renameAccount.spec.ts b/wallets/metamask/test/playwright/e2e/renameAccount.spec.ts index 34356b762..0e2c35f0f 100644 --- a/wallets/metamask/test/playwright/e2e/renameAccount.spec.ts +++ b/wallets/metamask/test/playwright/e2e/renameAccount.spec.ts @@ -11,7 +11,7 @@ test('should rename current account with specified name', async ({ context, meta const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword) const accountName = 'Test Account' - await metamask.renameAccount(accountName) + await metamask.renameAccount('Account 1', accountName) await expect(metamaskPage.locator(metamask.homePage.selectors.accountMenu.accountButton)).toHaveText(accountName) })