diff --git a/commands/metamask.js b/commands/metamask.js index ef38eb8dd..554a75de3 100644 --- a/commands/metamask.js +++ b/commands/metamask.js @@ -840,12 +840,43 @@ const metamask = { }, async acceptAccess(options) { const notificationPage = await playwright.switchToMetamaskNotification(); + + if (options && options.accountIndexes) { + if ( + !Array.isArray(options.accountIndexes) || + options.accountIndexes.some(accIdx => Number.isNaN(Number(accIdx))) + ) { + console.error('`accountIndexes` must be an array of numbers'); + return false; + } + + const checkboxes = await notificationPage.locator( + notificationPageElements.selectAccountCheckbox, + ); + const count = await checkboxes.count(); + for (let i = 0; i < count; ++i) { + const accountIdx = i + 1; + const checkboxSelector = + notificationPageElements.getAccountCheckboxSelector(accountIdx); + const checkbox = await playwright.waitFor( + checkboxSelector, + notificationPage, + ); + const isChecked = await checkbox.isChecked(); + const shouldCheck = options.accountIndexes.includes(accountIdx); + if ((!isChecked && shouldCheck) || (isChecked && !shouldCheck)) { + await playwright.waitAndClick(checkboxSelector, notificationPage); + } + } + } + if (options && options.allAccounts) { await playwright.waitAndClick( notificationPageElements.selectAllCheckbox, notificationPage, ); } + await playwright.waitAndClick( notificationPageElements.nextButton, notificationPage, diff --git a/docs/synpress-commands.md b/docs/synpress-commands.md index 9fefccda5..60486d459 100644 --- a/docs/synpress-commands.md +++ b/docs/synpress-commands.md @@ -346,9 +346,10 @@ Accept metamask access request. ```ts acceptMetamaskAccess(options?: { - allAccounts?: boolean; - confirmSignatureRequest?: boolean; - confirmDataSignatureRequest?: boolean; + accountIndexes?: number[], + allAccounts?: boolean, + confirmSignatureRequest?: boolean, + confirmDataSignatureRequest?: boolean, }): Chainable; ``` diff --git a/pages/metamask/notification-page.js b/pages/metamask/notification-page.js index f848564fb..e690fb499 100644 --- a/pages/metamask/notification-page.js +++ b/pages/metamask/notification-page.js @@ -8,6 +8,9 @@ const customSpendingLimitInput = `${notificationPage} [data-testid="custom-spend const allowToSpendButton = `${notificationPage} [data-testid="page-container-footer-next"]`; const rejectToSpendButton = `${notificationPage} [data-testid="page-container-footer-cancel"]`; const selectAllCheckbox = `${notificationPage} .choose-account-list__header-check-box`; +const getAccountCheckboxSelector = (accountIdx = 1) => + `${notificationPage} .choose-account-list__account:nth-child(${accountIdx}) .check-box`; +const selectAccountCheckbox = `${notificationPage} .choose-account-list__account .check-box`; const approveWarningToSpendButton = `${notificationPage} .set-approval-for-all-warning__footer__approve-button`; const rejectWarningToSpendButton = `${notificationPage} .btn-secondary.set-approval-for-all-warning__footer__cancel-button`; @@ -22,6 +25,8 @@ module.exports.notificationPageElements = { allowToSpendButton, rejectToSpendButton, selectAllCheckbox, + getAccountCheckboxSelector, + selectAccountCheckbox, approveWarningToSpendButton, rejectWarningToSpendButton, }; diff --git a/support/index.d.ts b/support/index.d.ts index 531cb59d2..a2d24134d 100644 --- a/support/index.d.ts +++ b/support/index.d.ts @@ -292,8 +292,10 @@ declare namespace Cypress { * @example * cy.acceptMetamaskAccess() * cy.acceptMetamaskAccess({allAccounts: true, confirmSignatureRequest: true}) + * cy.acceptMetamaskAccess({ accountIndexes: [1, 2, 3] }) */ acceptMetamaskAccess(options?: { + accountIndexes?: number[]; allAccounts?: boolean; confirmSignatureRequest?: boolean; confirmDataSignatureRequest?: boolean; diff --git a/tests/e2e/specs/metamask-spec.js b/tests/e2e/specs/metamask-spec.js index ed8953e72..4f8e15b36 100644 --- a/tests/e2e/specs/metamask-spec.js +++ b/tests/e2e/specs/metamask-spec.js @@ -28,7 +28,24 @@ describe('Metamask', () => { expect(rejected).to.be.true; }); }); - it(`acceptMetamaskAccess should accept connection request to metamask`, () => { + it(`acceptMetamaskAccess should accept connection request to metamask with 2nd account`, () => { + cy.get('#connectButton').click(); + cy.acceptMetamaskAccess({ + accountIndexes: [2], + }).then(connected => { + expect(connected).to.be.true; + }); + cy.get('#network').contains('11155111'); + cy.get('#chainId').contains('0xaa36a7'); + cy.get('#accounts').should( + 'have.text', + '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', + ); + }); + it(`acceptMetamaskAccess should accept connection request to metamask with currently selected account (1st one) by default`, () => { + cy.switchMetamaskAccount(2); + cy.disconnectMetamaskWalletFromDapp(); + cy.switchMetamaskAccount(1); cy.get('#connectButton').click(); cy.acceptMetamaskAccess().then(connected => { expect(connected).to.be.true;