Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: import wallets using Cypress #1219

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions wallets/metamask/cypress-no-wallet.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from 'cypress'
import configureSynpress from './src/cypress/configureSynpress'

export default defineConfig({
userAgent: 'synpress',
chromeWebSecurity: true,
e2e: {
baseUrl: 'http://localhost:9999',
specPattern: 'test/cypress/**/importWallet.cy.no-wallet.{js,jsx,ts,tsx}',
supportFile: 'src/cypress/support/e2e.{js,jsx,ts,tsx}',
testIsolation: false,
async setupNodeEvents(on, config) {
return configureSynpress(on, config, false)
}
}
})
1 change: 1 addition & 0 deletions wallets/metamask/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"test": "vitest run",
"test:coverage": "vitest run --coverage",
"test:cypress:headful": "cypress run --browser chrome --headed",
"test:cypress:headful:no-wallet": "cypress run --browser chrome --headed --config-file ./cypress-no-wallet.config.ts",
"test:playwright:headful": "playwright test",
"test:playwright:headless": "HEADLESS=true playwright test",
"test:playwright:headless:ui": "HEADLESS=true playwright test --ui",
Expand Down
29 changes: 25 additions & 4 deletions wallets/metamask/src/cypress/MetaMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,21 @@ export default class MetaMask {
}

async connectToDapp(accounts?: string[]) {
return this.metamaskPlaywright
.connectToDapp(accounts)
.then(() => true)
.catch(() => false)
await this.metamaskPlaywright.connectToDapp(accounts)

return true
}

async importWallet(seedPhrase: string) {
await this.metamaskPlaywright.importWallet(seedPhrase)

return true
}

async importWalletFromPrivateKey(privateKey: string) {
await this.metamaskPlaywright.importWalletFromPrivateKey(privateKey)

return true
}

async addNewAccount(accountName: string) {
Expand Down Expand Up @@ -324,6 +335,16 @@ export default class MetaMask {
})
}

async goBackToHomePage() {
await this.metamaskPlaywright.openSettings()

await expect(this.metamaskExtensionPage.locator(HomePageSelectors.copyAccountAddressButton)).not.toBeVisible()

await this.metamaskPlaywright.goBackToHomePage()

await expect(this.metamaskExtensionPage.locator(HomePageSelectors.copyAccountAddressButton)).toBeVisible()
}

// Lock/Unlock

async lock() {
Expand Down
18 changes: 15 additions & 3 deletions wallets/metamask/src/cypress/configureSynpress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
// TODO: Implement if needed to change the focus between pages
// let cypressPage: Page

export default function configureSynpress(on: Cypress.PluginEvents, config: Cypress.PluginConfigOptions) {
export default function configureSynpress(
on: Cypress.PluginEvents,
config: Cypress.PluginConfigOptions,
importDefaultWallet = true
) {
const browsers = config.browsers.filter((b) => b.name === 'chrome')
if (browsers.length === 0) {
throw new Error('No Chrome browser found in the configuration')
Expand Down Expand Up @@ -47,7 +51,7 @@
metamaskExtensionId: _metamaskExtensionId,
extensionPage: _extensionPage,
cypressPage: _cypressPage
} = await importMetaMaskWallet(rdpPort)
} = await importMetaMaskWallet(rdpPort, importDefaultWallet)
if (_extensionPage && _metamaskExtensionId) {
context = _context
metamaskExtensionId = _metamaskExtensionId
Expand All @@ -63,8 +67,12 @@

// Synpress API
on('task', {
// Account
// Wallet
connectToDapp: () => metamask?.connectToDapp(),
importWallet: (seedPhrase: string) => metamask?.importWallet(seedPhrase),
importWalletFromPrivateKey: (privateKey: string) => metamask?.importWalletFromPrivateKey(privateKey),

// Account
getAccount: () => metamask?.getAccount(),
getAccountAddress: () => metamask?.getAccountAddress(),
addNewAccount: (accountName: string) => metamask?.addNewAccount(accountName),
Expand Down Expand Up @@ -125,6 +133,10 @@
// Lock/Unlock
lock: () => metamask?.lock(),
unlock: () => metamask?.unlock()

// Others

goBackToHomePage: () => metamask?.goBackToHomePage()
Fixed Show fixed Hide fixed
})

return {
Expand Down
4 changes: 2 additions & 2 deletions wallets/metamask/src/cypress/support/importMetaMaskWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import getPlaywrightMetamask from '../getPlaywrightMetamask'

const SEED_PHRASE = 'test test test test test test test test test test test junk'

export default async function importMetaMaskWallet(port: number) {
export default async function importMetaMaskWallet(port: number, importDefaultWallet = true) {
const debuggerDetails = await fetch(`http://127.0.0.1:${port}/json/version`)

const debuggerDetailsConfig = (await debuggerDetails.json()) as {
Expand All @@ -28,7 +28,7 @@ export default async function importMetaMaskWallet(port: number) {

const metamask = getPlaywrightMetamask(context, extensionPage, metamaskExtensionId)

await metamask.importWallet(SEED_PHRASE)
if (importDefaultWallet) await metamask.importWallet(SEED_PHRASE)

cypressPage = context.pages()[extensionPageIndex === 1 ? 0 : 1] as Page
await cypressPage.bringToFront()
Expand Down
39 changes: 29 additions & 10 deletions wallets/metamask/src/cypress/support/synpressCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import type { Network } from '../../type/Network'
declare global {
namespace Cypress {
interface Chainable {
importWallet(seedPhrase: string): Chainable<void>
importWalletFromPrivateKey(privateKey: string): Chainable<void>

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

Expand Down Expand Up @@ -60,16 +63,21 @@ declare global {

lock(): Chainable<void>
unlock(): Chainable<void>

goBackToHomePage(): Chainable<void>
}
}
}

export default function synpressCommands() {
Cypress.Commands.add('getAccount', () => {
return cy.task('getAccount')
// Wallet

Cypress.Commands.add('importWallet', (seedPhrase: string) => {
return cy.task('importWallet', seedPhrase)
})
Cypress.Commands.add('getNetwork', () => {
return cy.task('getNetwork')

Cypress.Commands.add('importWalletFromPrivateKey', (privateKey: string) => {
return cy.task('importWalletFromPrivateKey', privateKey)
})

Cypress.Commands.add('connectToDapp', () => {
Expand All @@ -78,6 +86,9 @@ export default function synpressCommands() {

// Account

Cypress.Commands.add('getAccount', () => {
return cy.task('getAccount')
})
Cypress.Commands.add('addNewAccount', (accountName: string) => {
return cy.task('addNewAccount', accountName)
})
Expand All @@ -93,6 +104,9 @@ export default function synpressCommands() {

// Network

Cypress.Commands.add('getNetwork', () => {
return cy.task('getNetwork')
})
Cypress.Commands.add('switchNetwork', (networkName: string, isTestnet = false) => {
return cy.task('switchNetwork', { networkName, isTestnet })
})
Expand Down Expand Up @@ -157,6 +171,15 @@ export default function synpressCommands() {
Cypress.Commands.add('rejectTokenPermission', () => {
return cy.task('rejectTokenPermission')
})

// Lock/Unlock

Cypress.Commands.add('lock', () => {
return cy.task('lock')
})
Cypress.Commands.add('unlock', () => {
return cy.task('unlock')
})

// Others

Expand Down Expand Up @@ -187,11 +210,7 @@ export default function synpressCommands() {
Cypress.Commands.add('closeTransactionDetails', () => {
return cy.task('closeTransactionDetails')
})

Cypress.Commands.add('lock', () => {
return cy.task('lock')
})
Cypress.Commands.add('unlock', () => {
return cy.task('unlock')
Cypress.Commands.add('goBackToHomePage', () => {
return cy.task('goBackToHomePage')
})
}
10 changes: 10 additions & 0 deletions wallets/metamask/test/cypress/importWallet.cy.no-wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const SEED_PHRASE = 'test test test test test test test test test test test junk'

describe('MetaMask Wallet Import', () => {
it('should go through the onboarding flow and import wallet from seed phrase', () => {
cy.importWallet(SEED_PHRASE)

cy.getAccount().should('eq', 'Account 1')
cy.getAccountAddress().should('eq', '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe('Import wallet from private key', () => {
it('should import a new wallet from private key', () => {
cy.importWalletFromPrivateKey('ea084c575a01e2bbefcca3db101eaeab1d8af15554640a510c73692db24d0a6a')

cy.getAccountAddress().should('eq', '0xa2ce797cA71d0EaE1be5a7EffD27Fd6C38126801')
})
})
Loading