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: New network and switch network Cypress APIs #1198

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 38 additions & 2 deletions wallets/metamask/src/cypress/configureSynpress.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { BrowserContext, Page } from '@playwright/test'
import { expect } from '@playwright/test'
import { ensureRdpPort } from '@synthetixio/synpress-core'
import { type CreateAnvilOptions, createPool } from '@viem/anvil'
import { type CreateAnvilOptions, type Pool, createPool } from '@viem/anvil'
import { waitFor } from '../playwright/utils/waitFor'
import HomePageSelectors from '../selectors/pages/HomePage'
import Selectors from '../selectors/pages/HomePage'
Expand All @@ -19,6 +19,8 @@ let metamaskExtensionId: string

let metamaskExtensionPage: Page

let pool: Pool

// TODO: Implement if needed to change the focus between pages
// let cypressPage: Page

Expand Down Expand Up @@ -151,7 +153,7 @@ export default function configureSynpress(on: Cypress.PluginEvents, config: Cypr
},

async createAnvilNode(options?: CreateAnvilOptions) {
const pool = createPool()
pool = createPool()

const nodeId = Array.from(pool.instances()).length
const anvil = await pool.start(nodeId, options)
Expand All @@ -164,6 +166,11 @@ export default function configureSynpress(on: Cypress.PluginEvents, config: Cypr
return { anvil, rpcUrl, chainId }
},

async emptyAnvilNode() {
await pool.empty()
return true
},

async connectToAnvil({
rpcUrl,
chainId
Expand Down Expand Up @@ -226,6 +233,22 @@ export default function configureSynpress(on: Cypress.PluginEvents, config: Cypr
return true
},

async approveNewNetwork() {
const metamask = getPlaywrightMetamask(context, metamaskExtensionPage, metamaskExtensionId)

await metamask.approveNewNetwork()

return true
},

async approveSwitchNetwork() {
const metamask = getPlaywrightMetamask(context, metamaskExtensionPage, metamaskExtensionId)

await metamask.approveSwitchNetwork()

return true
},

// Others

async providePublicEncryptionKey() {
Expand Down Expand Up @@ -278,6 +301,19 @@ export default function configureSynpress(on: Cypress.PluginEvents, config: Cypr
.catch(() => {
return false
})
},

async confirmTransactionAndWaitForMining() {
const metamask = getPlaywrightMetamask(context, metamaskExtensionPage, metamaskExtensionId)

return metamask
.confirmTransactionAndWaitForMining()
.then(() => {
return true
})
.catch(() => {
return false
})
}
})

Expand Down
16 changes: 16 additions & 0 deletions wallets/metamask/src/cypress/support/synpressCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ declare global {
chainId: number
}>
connectToAnvil(): Chainable<void>
emptyAnvilNode(): Chainable<void>
addNetwork(network: Network): Chainable<void>
approveNewNetwork(): Chainable<void>
approveSwitchNetwork(): Chainable<void>

deployToken(): Chainable<void>
addNewToken(): Chainable<void>
Expand All @@ -40,6 +43,7 @@ declare global {
decrypt(): Chainable<void>
confirmSignature(): Chainable<void>
confirmTransaction(): Chainable<void>
confirmTransactionAndWaitForMining(): Chainable<void>
}
}
}
Expand Down Expand Up @@ -95,9 +99,18 @@ export default function synpressCommands() {
return cy.task('addNetwork', network)
})
})
Cypress.Commands.add('emptyAnvilNode', () => {
return cy.task('emptyAnvilNode')
})
Cypress.Commands.add('addNetwork', (network: Network) => {
return cy.task('addNetwork', network)
})
Cypress.Commands.add('approveNewNetwork', () => {
return cy.task('approveNewNetwork')
})
Cypress.Commands.add('approveSwitchNetwork', () => {
return cy.task('approveSwitchNetwork')
})

// Token

Expand All @@ -122,4 +135,7 @@ export default function synpressCommands() {
Cypress.Commands.add('confirmTransaction', () => {
return cy.task('confirmTransaction')
})
Cypress.Commands.add('confirmTransactionAndWaitForMining', () => {
return cy.task('confirmTransactionAndWaitForMining')
})
}
1 change: 0 additions & 1 deletion wallets/metamask/test/cypress/addNewToken.cy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
before(() => {
cy.getNetwork().then((network) => {
console.log(network)
if (network !== 'Anvil') {
cy.switchNetwork('Anvil')
}
Expand Down
16 changes: 16 additions & 0 deletions wallets/metamask/test/cypress/approveNewNetwork.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
it('should add a new network', () => {
cy.createAnvilNode({
chainId: 1338,
port: 8546
}).then(() => {
cy.get('#addEthereumChain').click()

cy.approveNewNetwork().then(() => {
cy.approveSwitchNetwork().then(() => {
cy.get('#chainId').should('have.text', '0x53a')

cy.emptyAnvilNode()
})
})
})
})
16 changes: 16 additions & 0 deletions wallets/metamask/test/cypress/approveSwitchNetwork.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
it('should switch to the requested network', () => {
cy.createAnvilNode({
chainId: 1338,
port: 8546
}).then(() => {
cy.switchNetwork('Ethereum Mainnet').then(() => {
cy.get('#chainId').should('have.text', '0x1')

cy.get('#switchEthereumChain').click()

cy.approveSwitchNetwork().then(() => {
cy.get('#chainId').should('have.text', '0x53a')
})
})
})
})
19 changes: 19 additions & 0 deletions wallets/metamask/test/cypress/batchTransfer.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
before(() => {
cy.get('#deployERC1155Button').click()

cy.confirmTransaction().then(() => {
cy.wait(5000)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matstyler why wait? we have to avoid waits at all cost

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do not have tools to avoid it at the moment, I will have to resolve it separately

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matstyler from my understanding, this transaction has to be confirmed before we can execute next steps in this test.

I think we should add a new task for it => if specific flag is passed to confirmtransaction (waitfortx: true?), then it will automatically wait for tx to be confirmed before moving on.


cy.get('#batchMintButton').click()

cy.confirmTransactionAndWaitForMining()
})
})

it('should perform batch ERC115 transfer', () => {
cy.get('#batchTransferFromButton').click()

cy.confirmTransaction().then(() => {
cy.get('#erc1155Status').should('have.text', 'Batch Transfer From completed')
})
})
Loading