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

Update acceptMetamaskAccess() to accept a list of account indexes. #686

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from
Open
31 changes: 31 additions & 0 deletions commands/metamask.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions docs/synpress-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Subject>;
```

Expand Down
5 changes: 5 additions & 0 deletions pages/metamask/notification-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`;

Expand All @@ -22,6 +25,8 @@ module.exports.notificationPageElements = {
allowToSpendButton,
rejectToSpendButton,
selectAllCheckbox,
getAccountCheckboxSelector,
selectAccountCheckbox,
approveWarningToSpendButton,
rejectWarningToSpendButton,
};
Expand Down
2 changes: 2 additions & 0 deletions support/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 18 additions & 1 deletion tests/e2e/specs/metamask-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down