Skip to content

Commit

Permalink
Add renameAccount method for MetaMask
Browse files Browse the repository at this point in the history
  • Loading branch information
duckception committed Aug 24, 2023
1 parent 379c85a commit 81d2557
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 1 deletion.
32 changes: 32 additions & 0 deletions commands/metamask.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,38 @@ const metamask = {
await switchToCypressIfNotActive();
return accountExists ? formErrorTxt : true;
},
async renameAccount(newAccountName) {
await switchToMetamaskIfNotActive();

await playwright.waitAndClick(mainPageElements.optionsMenu.button);
await playwright.waitAndClick(
mainPageElements.optionsMenu.accountDetailsButton,
);

await playwright.waitAndClick(mainPageElements.renameAccount.invokeInput);
await playwright.waitClearAndType(
newAccountName,
mainPageElements.renameAccount.input,
);

const formErrorEl = await playwright.waitFor(
mainPageElements.renameAccount.error,
);
const formErrorTxt = await formErrorEl.innerText();
const accountExists = 'This account name already exists' === formErrorTxt;

if (accountExists) {
log(`[createAccount] ${formErrorTxt}`);
} else {
await playwright.waitAndClick(
mainPageElements.renameAccount.confirmButton,
);
}

await playwright.waitAndClick(mainPageElements.accountModal.closeButton);
await switchToCypressIfNotActive();
return accountExists ? formErrorTxt : true;
},
async switchAccount(accountNameOrAccountNumber) {
if (typeof accountNameOrAccountNumber === 'string') {
accountNameOrAccountNumber = accountNameOrAccountNumber.toLowerCase();
Expand Down
8 changes: 8 additions & 0 deletions docs/synpress-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ Create new account in metamask.
createMetamaskAccount(accountName?: string): Chainable<Subject>;
```

#### `cy.renameMetamaskAccount()`

Rename current account in metamask.

```ts
createMetamaskAccount(newAccountName: string): Chainable<Subject>;
```

#### `cy.switchMetamaskAccount()`

Switch metamask account.
Expand Down
11 changes: 10 additions & 1 deletion pages/metamask/main-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,19 @@ const connectedSites = {
closeButton: `${connectedSitesSelector} [data-testid="popover-close"]`,
};

const accountModalSelector = '.account-modal';
const accountModal = {
walletAddressInput: '.account-modal .qr-code__address',
walletAddressInput: `${accountModalSelector} .qr-code__address`,
closeButton: '.account-modal__close',
};

const renameAccount = {
invokeInput: `${accountModalSelector} [data-testid="editable-label-button"]`,
input: `${accountModalSelector} [data-testid="editable-input"]`,
confirmButton: `${accountModalSelector} .editable-label__icon-button`,
error: `${accountModalSelector} .editable-label__error`,
};

const importAccountSelector = '.new-account';
const importAccount = {
page: importAccountSelector,
Expand Down Expand Up @@ -135,6 +143,7 @@ module.exports.mainPageElements = {
optionsMenu,
connectedSites,
accountModal,
renameAccount,
importAccount,
createAccount,
importToken,
Expand Down
1 change: 1 addition & 0 deletions plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ module.exports = (on, config) => {
unlockMetamask: metamask.unlock,
importMetamaskAccount: metamask.importAccount,
createMetamaskAccount: metamask.createAccount,
renameMetamaskAccount: metamask.renameAccount,
switchMetamaskAccount: metamask.switchAccount,
addMetamaskNetwork: metamask.addNetwork,
changeMetamaskNetwork: async network => {
Expand Down
4 changes: 4 additions & 0 deletions support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ Cypress.Commands.add('createMetamaskAccount', accountName => {
return cy.task('createMetamaskAccount', accountName);
});

Cypress.Commands.add('renameMetamaskAccount', newAccountName => {
return cy.task('renameMetamaskAccount', newAccountName);
});

Cypress.Commands.add('switchMetamaskAccount', accountNameOrAccountNumber => {
return cy.task('switchMetamaskAccount', accountNameOrAccountNumber);
});
Expand Down
6 changes: 6 additions & 0 deletions support/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ declare namespace Cypress {
* cy.createMetamaskAccount('accountName')
*/
createMetamaskAccount(accountName?: string): Chainable<boolean>;
/**
* Rename current account in metamask
* @example
* cy.renameMetamaskAccount('fancyAccountName')
*/
renameMetamaskAccount(newAccountName: string): Chainable<boolean>;
/**
* Switch metamask account
* @example
Expand Down
10 changes: 10 additions & 0 deletions tests/e2e/specs/metamask-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ describe('Metamask', () => {
expect(created).to.be.equal('This account name already exists');
});
});
it(`renameMetamaskAccount should rename metamask account`, () => {
cy.renameMetamaskAccount('custom-fancy-wallet').then(created => {
expect(created).to.be.true;
});
});
it(`renameMetamaskAccount should not fail when account with this name already exists`, () => {
cy.renameMetamaskAccount('custom-fancy-wallet').then(created => {
expect(created).to.be.equal('This account name already exists');
});
});
it(`switchMetamaskAccount should switch to another account using order number`, () => {
cy.switchMetamaskAccount(2).then(switched => {
expect(switched).to.be.true;
Expand Down

0 comments on commit 81d2557

Please sign in to comment.