diff --git a/commands/metamask.js b/commands/metamask.js index fdad8daad..6f24d067f 100644 --- a/commands/metamask.js +++ b/commands/metamask.js @@ -852,7 +852,10 @@ const metamask = { ); return true; }, - async confirmPermissionToSpend(spendLimit) { + async confirmPermissionToSpend({ + spendLimit, + shouldWaitForPopupClosure = false, + } = {}) { const notificationPage = await playwright.switchToMetamaskNotification(); // experimental mode on if ( @@ -874,7 +877,7 @@ const metamask = { await playwright.waitAndClick( notificationPageElements.allowToSpendButton, notificationPage, - { waitForEvent: 'close' }, + shouldWaitForPopupClosure ? undefined : { waitForEvent: 'close' }, ); return true; }, @@ -963,7 +966,10 @@ const metamask = { ); return true; }, - async confirmTransaction(gasConfig) { + async confirmTransaction({ + gasConfig, + shouldWaitForPopupClosure = false, + } = {}) { let txData = {}; const notificationPage = await playwright.switchToMetamaskNotification(); if (gasConfig) { @@ -1189,7 +1195,7 @@ const metamask = { await playwright.waitAndClick( confirmPageElements.confirmButton, notificationPage, - { waitForEvent: 'close' }, + shouldWaitForPopupClosure ? undefined : { waitForEvent: 'close' }, ); txData.confirmed = true; log('[confirmTransaction] Transaction confirmed!'); diff --git a/docs/synpress-commands.md b/docs/synpress-commands.md index 2ae6bee81..4fe192a18 100644 --- a/docs/synpress-commands.md +++ b/docs/synpress-commands.md @@ -344,7 +344,10 @@ rejectMetamaskAddToken(): Chainable; Confirm metamask permission to spend asset. ```ts -confirmMetamaskPermissionToSpend(spendLimit?: string): Chainable; +confirmMetamaskPermissionToSpend(options: { + spendLimit?: string + shouldWaitForPopupClosure?: boolean +}): Chainable; ``` #### `cy.confirmMetamaskPermissionToApproveAll()` @@ -415,22 +418,23 @@ rejectMetamaskAccess(): Chainable; Confirm metamask transaction (auto-detects eip-1559 and legacy transactions). ```ts -confirmMetamaskTransaction( - gasConfig?: +confirmMetamaskTransaction(options: { + gasConfig: | { - gasLimit?: number; - baseFee?: number; - priorityFee?: number; + gasLimit?: number; + baseFee?: number; + priorityFee?: number; } | { - gasLimit?: number; - gasPrice?: number; - } + gasLimit?: number; + gasPrice?: number; + } | 'low' | 'market' | 'aggressive' - | 'site', -): Chainable; + | 'site', + shouldWaitForPopupClosure?: boolean +}): Chainable; ``` #### `cy.confirmMetamaskTransactionAndWaitForMining()` diff --git a/support/commands.js b/support/commands.js index 282ab8b3e..664571457 100644 --- a/support/commands.js +++ b/support/commands.js @@ -175,8 +175,14 @@ Cypress.Commands.add('rejectMetamaskAddToken', () => { Cypress.Commands.add( 'confirmMetamaskPermissionToSpend', - (spendLimit = '999999999999999999') => { - return cy.task('confirmMetamaskPermissionToSpend', spendLimit); + ({ + spendLimit = '999999999999999999', + shouldWaitForPopupClosure = false, + } = {}) => { + return cy.task('confirmMetamaskPermissionToSpend', { + spendLimit, + shouldWaitForPopupClosure, + }); }, ); @@ -192,9 +198,15 @@ Cypress.Commands.add('rejectMetamaskAccess', () => { return cy.task('rejectMetamaskAccess'); }); -Cypress.Commands.add('confirmMetamaskTransaction', gasConfig => { - return cy.task('confirmMetamaskTransaction', gasConfig); -}); +Cypress.Commands.add( + 'confirmMetamaskTransaction', + ({ gasConfig, shouldWaitForPopupClosure = false } = {}) => { + return cy.task('confirmMetamaskTransaction', { + gasConfig, + shouldWaitForPopupClosure, + }); + }, +); Cypress.Commands.add( 'confirmMetamaskTransactionAndWaitForMining', diff --git a/support/index.d.ts b/support/index.d.ts index 68d42b311..820a9dcae 100644 --- a/support/index.d.ts +++ b/support/index.d.ts @@ -272,9 +272,13 @@ declare namespace Cypress { * Confirm metamask permission to spend asset * @example * cy.confirmMetamaskPermissionToSpend() - * cy.confirmMetamaskPermissionToSpend('999999999') + * cy.confirmMetamaskPermissionToSpend({ spendLimit: '999999999' }) + * cy.confirmMetamaskPermissionToSpend({ spendLimit: '999999999', shouldWaitForPopupClosure: false }) */ - confirmMetamaskPermissionToSpend(spendLimit?: string): Chainable; + confirmMetamaskPermissionToSpend(options: { + spendLimit?: string + shouldWaitForPopupClosure?: boolean + }): Chainable; /** * Confirm metamask permission to access all elements (example: collectibles) * @example @@ -327,26 +331,28 @@ declare namespace Cypress { * Confirm metamask transaction (auto-detects eip-1559 and legacy transactions) * @example * cy.confirmMetamaskTransaction() - * cy.confirmMetamaskTransaction({ gasLimit: 1000000, baseFee: 20, priorityFee: 20 }) // eip-1559 - * cy.confirmMetamaskTransaction({ gasLimit: 1000000, gasPrice: 20 }) // legacy - * cy.confirmMetamaskTransaction('aggressive') // eip-1559 only! => available options: 'low', 'market', 'aggressive', 'site' (site is usually by default) + * cy.confirmMetamaskTransaction({ gasConfig: { gasLimit: 1000000, baseFee: 20, priorityFee: 20 } }) // eip-1559 + * cy.confirmMetamaskTransaction({ gasConfig: { gasLimit: 1000000, gasPrice: 20 } }) // legacy + * cy.confirmMetamaskTransaction({ gasConfig: 'aggressive' }) // eip-1559 only! => available options: 'low', 'market', 'aggressive', 'site' (site is usually by default) + * cy.confirmMetamaskTransaction({ shouldWaitForPopupClosure: false }) */ - confirmMetamaskTransaction( - gasConfig?: + confirmMetamaskTransaction(options: { + gasConfig: | { - gasLimit?: number; - baseFee?: number; - priorityFee?: number; - } + gasLimit?: number; + baseFee?: number; + priorityFee?: number; + } | { - gasLimit?: number; - gasPrice?: number; - } + gasLimit?: number; + gasPrice?: number; + } | 'low' | 'market' | 'aggressive' | 'site', - ): Chainable; + shouldWaitForPopupClosure?: boolean + }): Chainable; /** * Confirm metamask transaction (auto-detects eip-1559 and legacy transactions) and wait for ALL pending transactions to be mined * @example diff --git a/tests/e2e/specs/metamask-spec.js b/tests/e2e/specs/metamask-spec.js index 4cbdcf635..faf122f7b 100644 --- a/tests/e2e/specs/metamask-spec.js +++ b/tests/e2e/specs/metamask-spec.js @@ -347,12 +347,26 @@ describe('Metamask', () => { it(`confirmMetamaskTransaction should confirm legacy transaction using advanced gas settings`, () => { cy.get('#sendButton').click(); cy.confirmMetamaskTransaction({ - gasLimit: 210000, - gasPrice: 100, + gasConfig: { + gasLimit: 210000, + gasPrice: 100, + }, }).then(txData => { expect(txData.confirmed).to.be.true; }); }); + it(`confirmMetamaskTransaction should work for serial transactions`, () => { + cy.get('#sendEIP1559Button').click(); + cy.get('#sendEIP1559Button').click(); + cy.confirmMetamaskTransaction({ + shouldWaitForPopupClosure: true, + }).then(txData => { + expect(txData.confirmed).to.be.true; + }); + cy.confirmMetamaskTransaction().then(txData => { + expect(txData.confirmed).to.be.true; + }); + }); it(`confirmMetamaskTransaction should confirm legacy ETH transfer to yourself`, () => { cy.get('#fromInput').type('0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'); cy.get('#toInput').type('0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'); @@ -373,28 +387,38 @@ describe('Metamask', () => { }); it(`confirmMetamaskTransaction should confirm eip-1559 transaction using pre-defined (low, market, aggressive, site) gas settings`, () => { cy.get('#sendEIP1559Button').click(); - cy.confirmMetamaskTransaction('low').then(txData => { + cy.confirmMetamaskTransaction({ + gasConfig: 'low', + }).then(txData => { expect(txData.confirmed).to.be.true; }); cy.get('#sendEIP1559Button').click(); - cy.confirmMetamaskTransaction('market').then(txData => { + cy.confirmMetamaskTransaction({ + gasConfig: 'market', + }).then(txData => { expect(txData.confirmed).to.be.true; }); cy.get('#sendEIP1559Button').click(); - cy.confirmMetamaskTransaction('aggressive').then(txData => { + cy.confirmMetamaskTransaction({ + gasConfig: 'aggressive', + }).then(txData => { expect(txData.confirmed).to.be.true; }); cy.get('#sendEIP1559Button').click(); - cy.confirmMetamaskTransaction('site').then(txData => { + cy.confirmMetamaskTransaction({ + gasConfig: 'site', + }).then(txData => { expect(txData.confirmed).to.be.true; }); }); it(`confirmMetamaskTransaction should confirm eip-1559 transaction using advanced gas settings`, () => { cy.get('#sendEIP1559Button').click(); cy.confirmMetamaskTransaction({ - gasLimit: 210000, - baseFee: 100, - priorityFee: 10, + gasConfig: { + gasLimit: 210000, + baseFee: 100, + priorityFee: 10, + }, }).then(txData => { expect(txData.confirmed).to.be.true; }); @@ -526,6 +550,18 @@ describe('Metamask', () => { expect(approved).to.be.true; }); }); + it(`confirmMetamaskPermissionToSpend should work for serial transactions`, () => { + cy.get('#approveTokens').click(); + cy.get('#approveTokens').click(); + cy.confirmMetamaskPermissionToSpend({ + shouldWaitForPopupClosure: true, + }).then(approved => { + expect(approved).to.be.true; + }); + cy.confirmMetamaskPermissionToSpend().then(approved => { + expect(approved).to.be.true; + }); + }); it(`rejectMetamaskToAddNetwork should reject permission to add network`, () => { cy.get('#addEthereumChain').click(); cy.rejectMetamaskToAddNetwork().then(rejected => {