From 493155d8efc096618e9c1483b7fdd9edb17271e8 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Wed, 15 Mar 2023 08:06:21 +0200 Subject: [PATCH 01/13] Add 'accountIndexes' to the acceptAccess api --- commands/metamask.js | 26 ++++++++++++++++++++++++++ pages/metamask/notification-page.js | 4 ++++ tests/e2e/specs/metamask-spec.js | 12 ++++++++++++ 3 files changed, 42 insertions(+) diff --git a/commands/metamask.js b/commands/metamask.js index b32c62856..925ca4c3e 100644 --- a/commands/metamask.js +++ b/commands/metamask.js @@ -795,6 +795,32 @@ const metamask = { notificationPage, ); } + + 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; + } + + // Uncheck accounts + + for await (let accountIdx of options.accountIndexes) { + const checkboxSelector = + notificationPageElements.getAccountCheckboxSelector(accountIdx); + const checkbox = await playwright.waitFor( + checkboxSelector, + notificationPage, + ); + const attrClass = await checkbox.getAttribute('class'); + if (!attrClass.includes('check-box__checked')) { + await playwright.waitAndClick(checkboxSelector, notificationPage); + } + } + } + await playwright.waitAndClick( notificationPageElements.nextButton, notificationPage, diff --git a/pages/metamask/notification-page.js b/pages/metamask/notification-page.js index 8c7ca6e7d..40e869fa0 100644 --- a/pages/metamask/notification-page.js +++ b/pages/metamask/notification-page.js @@ -7,6 +7,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`; + module.exports.notificationPageElements = { notificationPage, notificationAppContent, @@ -17,6 +20,7 @@ module.exports.notificationPageElements = { allowToSpendButton, rejectToSpendButton, selectAllCheckbox, + getAccountCheckboxSelector, }; const confirmSignatureRequestButton = `${notificationPage} .request-signature__footer__sign-button`; diff --git a/tests/e2e/specs/metamask-spec.js b/tests/e2e/specs/metamask-spec.js index 782942798..bea7536d6 100644 --- a/tests/e2e/specs/metamask-spec.js +++ b/tests/e2e/specs/metamask-spec.js @@ -21,6 +21,18 @@ describe('Metamask', () => { expect(disconnected).to.be.true; }); }); + it('acceptMetamaskAccess should accpet connection request for given account index', () => { + cy.visit('/'); + cy.get('#connectButton').click(); + cy.importMetamaskAccount( + '89fe6cf31a686f718017d664b08f75a51b706edc85edd6c67b1d2b56da628964', + ); + cy.acceptMetamaskAccess({ + accountIndexes: [2], + }).then(connected => { + expect(connected).to.be.true; + }); + }); it(`acceptMetamaskAccess should accept connection request to metamask`, () => { cy.visit('/'); cy.get('#connectButton').click(); From f09e580c91213374192dee3a77bb8355433b78e3 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Wed, 15 Mar 2023 10:30:44 +0200 Subject: [PATCH 02/13] refactor: remove test case --- commands/metamask.js | 15 +++++++++++---- pages/metamask/notification-page.js | 2 ++ tests/e2e/specs/metamask-spec.js | 12 ------------ 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/commands/metamask.js b/commands/metamask.js index 925ca4c3e..6ff86b383 100644 --- a/commands/metamask.js +++ b/commands/metamask.js @@ -796,6 +796,7 @@ const metamask = { ); } + // ========== Account Selection ========== if (options && options.accountIndexes) { if ( !Array.isArray(options.accountIndexes) || @@ -805,9 +806,13 @@ const metamask = { return false; } - // Uncheck accounts - - for await (let accountIdx of options.accountIndexes) { + // Uncheck selected accounts + 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( @@ -815,7 +820,9 @@ const metamask = { notificationPage, ); const attrClass = await checkbox.getAttribute('class'); - if (!attrClass.includes('check-box__checked')) { + const isChecked = attrClass.includes('check-box__checked'); + const shouldCheck = options.accountIndexes.includes(accountIdx); + if ((!isChecked && shouldCheck) || (isChecked && !shouldCheck)) { await playwright.waitAndClick(checkboxSelector, notificationPage); } } diff --git a/pages/metamask/notification-page.js b/pages/metamask/notification-page.js index 40e869fa0..c835d9544 100644 --- a/pages/metamask/notification-page.js +++ b/pages/metamask/notification-page.js @@ -9,6 +9,7 @@ const rejectToSpendButton = `${notificationPage} [data-testid="page-container-fo 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`; module.exports.notificationPageElements = { notificationPage, @@ -21,6 +22,7 @@ module.exports.notificationPageElements = { rejectToSpendButton, selectAllCheckbox, getAccountCheckboxSelector, + selectAccountCheckbox, }; const confirmSignatureRequestButton = `${notificationPage} .request-signature__footer__sign-button`; diff --git a/tests/e2e/specs/metamask-spec.js b/tests/e2e/specs/metamask-spec.js index bea7536d6..782942798 100644 --- a/tests/e2e/specs/metamask-spec.js +++ b/tests/e2e/specs/metamask-spec.js @@ -21,18 +21,6 @@ describe('Metamask', () => { expect(disconnected).to.be.true; }); }); - it('acceptMetamaskAccess should accpet connection request for given account index', () => { - cy.visit('/'); - cy.get('#connectButton').click(); - cy.importMetamaskAccount( - '89fe6cf31a686f718017d664b08f75a51b706edc85edd6c67b1d2b56da628964', - ); - cy.acceptMetamaskAccess({ - accountIndexes: [2], - }).then(connected => { - expect(connected).to.be.true; - }); - }); it(`acceptMetamaskAccess should accept connection request to metamask`, () => { cy.visit('/'); cy.get('#connectButton').click(); From 3a6d3e0852f91510a60f7794b7e11651d95c9054 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Wed, 15 Mar 2023 10:34:38 +0200 Subject: [PATCH 03/13] test: add account index to 'acceptMetamaskAccess' --- tests/e2e/specs/metamask-spec.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/e2e/specs/metamask-spec.js b/tests/e2e/specs/metamask-spec.js index 782942798..622901e5d 100644 --- a/tests/e2e/specs/metamask-spec.js +++ b/tests/e2e/specs/metamask-spec.js @@ -24,7 +24,9 @@ describe('Metamask', () => { it(`acceptMetamaskAccess should accept connection request to metamask`, () => { cy.visit('/'); cy.get('#connectButton').click(); - cy.acceptMetamaskAccess().then(connected => { + cy.acceptMetamaskAccess({ + accountIndexes: [1], + }).then(connected => { expect(connected).to.be.true; }); cy.get('#network').contains('5'); From 14db98b8faead0734d176fc5c7c60a04fd793bac Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Wed, 15 Mar 2023 11:03:35 +0200 Subject: [PATCH 04/13] docs: update types def & generate new docs --- docs/synpress-commands.md | 1 + support/index.d.ts | 2 ++ 2 files changed, 3 insertions(+) diff --git a/docs/synpress-commands.md b/docs/synpress-commands.md index 06e670680..aebdf6f34 100644 --- a/docs/synpress-commands.md +++ b/docs/synpress-commands.md @@ -331,6 +331,7 @@ Accept metamask access request. ```ts acceptMetamaskAccess(options?: { allAccounts?: boolean; + accountIndexes: number[]; signInSignature?: boolean; }): Chainable; ``` diff --git a/support/index.d.ts b/support/index.d.ts index 0abff7a3a..0456e6fbd 100644 --- a/support/index.d.ts +++ b/support/index.d.ts @@ -258,9 +258,11 @@ declare namespace Cypress { * @example * cy.acceptMetamaskAccess() * cy.acceptMetamaskAccess({allAccounts: true, signInSignature: true}) + * cy.acceptMetamaskAccess({ accountIndexes: [1, 2, 3] }) */ acceptMetamaskAccess(options?: { allAccounts?: boolean; + accountIndexes: number[]; signInSignature?: boolean; }): Chainable; /** From cfc251a8ab96e84ea49510683c8ee9f1ad36689b Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Wed, 15 Mar 2023 11:13:14 +0200 Subject: [PATCH 05/13] feat: allow 'allAccounts' flag to overwrite 'accountIndexes' --- commands/metamask.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/commands/metamask.js b/commands/metamask.js index 6ff86b383..36b9fc635 100644 --- a/commands/metamask.js +++ b/commands/metamask.js @@ -789,12 +789,6 @@ const metamask = { }, async acceptAccess(options) { const notificationPage = await playwright.switchToMetamaskNotification(); - if (options && options.allAccounts) { - await playwright.waitAndClick( - notificationPageElements.selectAllCheckbox, - notificationPage, - ); - } // ========== Account Selection ========== if (options && options.accountIndexes) { @@ -806,7 +800,6 @@ const metamask = { return false; } - // Uncheck selected accounts const checkboxes = await notificationPage.locator( notificationPageElements.selectAccountCheckbox, ); @@ -828,6 +821,13 @@ const metamask = { } } + if (options && options.allAccounts) { + await playwright.waitAndClick( + notificationPageElements.selectAllCheckbox, + notificationPage, + ); + } + await playwright.waitAndClick( notificationPageElements.nextButton, notificationPage, From d1152f5e781be0cde1500b9126f86fbc6c7b2993 Mon Sep 17 00:00:00 2001 From: duckception Date: Thu, 17 Aug 2023 21:53:04 +0200 Subject: [PATCH 06/13] Update docs --- docs/synpress-commands.md | 6 +++--- support/index.d.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/synpress-commands.md b/docs/synpress-commands.md index aebdf6f34..164c0189b 100644 --- a/docs/synpress-commands.md +++ b/docs/synpress-commands.md @@ -330,9 +330,9 @@ Accept metamask access request. ```ts acceptMetamaskAccess(options?: { - allAccounts?: boolean; - accountIndexes: number[]; - signInSignature?: boolean; + accountIndexes: number[], + allAccounts?: boolean, + signInSignature?: boolean, }): Chainable; ``` diff --git a/support/index.d.ts b/support/index.d.ts index 0456e6fbd..d866619ea 100644 --- a/support/index.d.ts +++ b/support/index.d.ts @@ -258,7 +258,7 @@ declare namespace Cypress { * @example * cy.acceptMetamaskAccess() * cy.acceptMetamaskAccess({allAccounts: true, signInSignature: true}) - * cy.acceptMetamaskAccess({ accountIndexes: [1, 2, 3] }) + * cy.acceptMetamaskAccess({ accountIndexes: [0, 1, 2, 3] }) */ acceptMetamaskAccess(options?: { allAccounts?: boolean; From 4ad8056a762b7c503f268d309a0ea6b265aa4201 Mon Sep 17 00:00:00 2001 From: duckception Date: Thu, 17 Aug 2023 21:53:26 +0200 Subject: [PATCH 07/13] Use `.isChecked()` instead of a class --- commands/metamask.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/commands/metamask.js b/commands/metamask.js index 36b9fc635..7f6d33774 100644 --- a/commands/metamask.js +++ b/commands/metamask.js @@ -790,7 +790,6 @@ const metamask = { async acceptAccess(options) { const notificationPage = await playwright.switchToMetamaskNotification(); - // ========== Account Selection ========== if (options && options.accountIndexes) { if ( !Array.isArray(options.accountIndexes) || @@ -812,8 +811,7 @@ const metamask = { checkboxSelector, notificationPage, ); - const attrClass = await checkbox.getAttribute('class'); - const isChecked = attrClass.includes('check-box__checked'); + const isChecked = await checkbox.isChecked(); const shouldCheck = options.accountIndexes.includes(accountIdx); if ((!isChecked && shouldCheck) || (isChecked && !shouldCheck)) { await playwright.waitAndClick(checkboxSelector, notificationPage); From e3c5c5dc0ad90f69d785689808106573f2987878 Mon Sep 17 00:00:00 2001 From: duckception Date: Thu, 17 Aug 2023 21:58:59 +0200 Subject: [PATCH 08/13] Update order of options in `index.d.ts` --- support/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/support/index.d.ts b/support/index.d.ts index 490d95550..07ee6582b 100644 --- a/support/index.d.ts +++ b/support/index.d.ts @@ -295,8 +295,8 @@ declare namespace Cypress { * cy.acceptMetamaskAccess({ accountIndexes: [0, 1, 2, 3] }) */ acceptMetamaskAccess(options?: { - allAccounts?: boolean; accountIndexes: number[]; + allAccounts?: boolean; confirmSignatureRequest?: boolean; confirmDataSignatureRequest?: boolean; }): Chainable; From ca9eb01385c4b4900d0f927faa914c1b549dc0d3 Mon Sep 17 00:00:00 2001 From: duckception Date: Thu, 17 Aug 2023 22:04:16 +0200 Subject: [PATCH 09/13] Update docs after merge --- docs/synpress-commands.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/synpress-commands.md b/docs/synpress-commands.md index cf66f9666..7919fb833 100644 --- a/docs/synpress-commands.md +++ b/docs/synpress-commands.md @@ -348,7 +348,7 @@ Accept metamask access request. acceptMetamaskAccess(options?: { accountIndexes: number[], allAccounts?: boolean, - confirmSignatureRequest?: boolean; + confirmSignatureRequest?: boolean, confirmDataSignatureRequest?: boolean, }): Chainable; ``` From 36b1e61e7aa4bfdd71e56c9fb105991cb8742697 Mon Sep 17 00:00:00 2001 From: duckception Date: Thu, 17 Aug 2023 22:06:02 +0200 Subject: [PATCH 10/13] Mark `accountIndexes` as optional in docs --- docs/synpress-commands.md | 2 +- support/index.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/synpress-commands.md b/docs/synpress-commands.md index 7919fb833..60486d459 100644 --- a/docs/synpress-commands.md +++ b/docs/synpress-commands.md @@ -346,7 +346,7 @@ Accept metamask access request. ```ts acceptMetamaskAccess(options?: { - accountIndexes: number[], + accountIndexes?: number[], allAccounts?: boolean, confirmSignatureRequest?: boolean, confirmDataSignatureRequest?: boolean, diff --git a/support/index.d.ts b/support/index.d.ts index 07ee6582b..bbef8e91a 100644 --- a/support/index.d.ts +++ b/support/index.d.ts @@ -295,7 +295,7 @@ declare namespace Cypress { * cy.acceptMetamaskAccess({ accountIndexes: [0, 1, 2, 3] }) */ acceptMetamaskAccess(options?: { - accountIndexes: number[]; + accountIndexes?: number[]; allAccounts?: boolean; confirmSignatureRequest?: boolean; confirmDataSignatureRequest?: boolean; From 68779e2e78aa808d55da53682f235a3d0dfeba6f Mon Sep 17 00:00:00 2001 From: duckception Date: Fri, 18 Aug 2023 15:37:20 +0200 Subject: [PATCH 11/13] Update example in types --- support/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/support/index.d.ts b/support/index.d.ts index bbef8e91a..a2d24134d 100644 --- a/support/index.d.ts +++ b/support/index.d.ts @@ -292,7 +292,7 @@ declare namespace Cypress { * @example * cy.acceptMetamaskAccess() * cy.acceptMetamaskAccess({allAccounts: true, confirmSignatureRequest: true}) - * cy.acceptMetamaskAccess({ accountIndexes: [0, 1, 2, 3] }) + * cy.acceptMetamaskAccess({ accountIndexes: [1, 2, 3] }) */ acceptMetamaskAccess(options?: { accountIndexes?: number[]; From dcb84c0b07bd83ea46c8fc85936058508b023b31 Mon Sep 17 00:00:00 2001 From: duckception Date: Fri, 18 Aug 2023 15:50:46 +0200 Subject: [PATCH 12/13] Add test --- tests/e2e/specs/metamask-spec.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/e2e/specs/metamask-spec.js b/tests/e2e/specs/metamask-spec.js index d028a2ff8..32dbe6c4c 100644 --- a/tests/e2e/specs/metamask-spec.js +++ b/tests/e2e/specs/metamask-spec.js @@ -28,15 +28,30 @@ 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: [1], + 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; + }); + cy.get('#network').contains('11155111'); + cy.get('#chainId').contains('0xaa36a7'); cy.get('#accounts').should( 'have.text', '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', From d6b5d13c8beb6bc4f20db63115794b5d879ca2de Mon Sep 17 00:00:00 2001 From: duckception Date: Fri, 18 Aug 2023 18:21:36 +0200 Subject: [PATCH 13/13] Lint --- tests/e2e/specs/metamask-spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/e2e/specs/metamask-spec.js b/tests/e2e/specs/metamask-spec.js index 32dbe6c4c..4f8e15b36 100644 --- a/tests/e2e/specs/metamask-spec.js +++ b/tests/e2e/specs/metamask-spec.js @@ -43,9 +43,9 @@ describe('Metamask', () => { ); }); 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.switchMetamaskAccount(2); + cy.disconnectMetamaskWalletFromDapp(); + cy.switchMetamaskAccount(1); cy.get('#connectButton').click(); cy.acceptMetamaskAccess().then(connected => { expect(connected).to.be.true;