synpress icon indicating copy to clipboard operation
synpress copied to clipboard

switchMetamaskAccount('Account 0') not Working

Open kwdikeman opened this issue 3 years ago • 2 comments

I can create/import wallets/accounts and name them anything I'd like. In the latest test I've have a 'default QA', Account 0, Account 1, Account 2, Account 3.

CreateNode_account0.js - this is to switch to 'Account 0' and create a node - The last test was import/Rename for 'Account 3' The first part of the test does not error out nor does it switch to 'Account 3'. It seem it doesn't matter what I name the account, upper or lower case.

Below is the Metamask and Puppeteer code used for switch. In my create node I'm simply calling: cy.switchMetamaskAccount('Account 0').then(switched, => { expect(switched).to.be.true }); Metamask Code:

switchAccount: async accountNameOrAccountNumber => {
    if (typeof accountNameOrAccountNumber === 'string') {
      accountNameOrAccountNumber = accountNameOrAccountNumber.toLowerCase();
    }
    await switchToMetamaskIfNotActive();
    await puppeteer.waitAndClick(mainPageElements.accountMenu.button);
    if (typeof accountNameOrAccountNumber === 'number') {
      await puppeteer.waitAndClick(
        mainPageElements.accountMenu.accountButton(accountNameOrAccountNumber),
      );
    } else {
      await puppeteer.waitAndClickByText(
        mainPageElements.accountMenu.accountName,
        accountNameOrAccountNumber,
      );
    }

Puppeteer Code:

waitAndClickByText: async (selector, text, page = metamaskWindow) => {
    await module.exports.waitFor(selector, page);
    const elements = await page.$$(selector);
    if (elements) {
      for (let el of elements) {
        const elText = await page.evaluate(el => el.textContent, el);
        if (elText.toLowerCase().includes(text.toLowerCase())) {
          await el.click();
          break();
        }
      }
    }
  },

kwdikeman avatar Mar 20 '22 23:03 kwdikeman

Hi there.

Trying to do the same but I cannot even switch. In this case because of more than one match. Trying to figure it out a workaround.

CypressError:cy.task('switchMetamaskAccount')` failed with the following error:

locator.click: Error: strict mode violation: locator('text=empty wallet 1') r esolved to 2 elements: 1)

aka getByRole('butt on', { name: 'empty wallet 1 0 ETH' }) 2) aka getByRole(' button', { name: 'empty wallet 1 0x9C3...620B' })

=========================== logs =========================== waiting for locator('text=empty wallet 1') strict mode violation: locator('text=empty wallet 1') resolved to 2 elements: 1)

aka getByRole('butt on', { name: 'empty wallet 1 0 ETH' }) 2) aka getByRole(' button', { name: 'empty wallet 1 0x9C3...620B' })

`

arhuaco avatar Jan 13 '23 21:01 arhuaco

Nelson,

This is what I ended up using which worked for me.

describe('Load MM Accounts', () => { context('Reset Accounts', () => {

it( Reset Account 0, () => { cy.switchMetamaskAccount(config.mmAccount0.name); cy.resetMetamaskAccount(config.mmAccount0.name).then(reset => { expect(reset).to.be.true; }); }) it( Reset Account 1, () => { cy.switchMetamaskAccount(config.mmAccount1.name); cy.resetMetamaskAccount(config.mmAccount1.name, ).then(reset => { expect(reset).to.be.true; }); }) it( Reset Account 2, () => { cy.switchMetamaskAccount(config.mmAccount2.name); cy.resetMetamaskAccount(config.mmAccount2.name, ).then(reset => { expect(reset).to.be.true; }); }) it( Reset Account 3, () => { cy.switchMetamaskAccount(config.mmAccount3.name); cy.resetMetamaskAccount(config.mmAccount3.name, ).then(reset => { expect(reset).to.be.true; }); }) it('Switch to Account 0', () => { cy.switchMetamaskAccount(config.mmAccount0.name); }) }) })

On Fri, Jan 13, 2023 at 1:56 PM Nelson Castillo @.***> wrote:

Hi there.

Trying to do the same but I cannot even switch. In this case because of more than one match. Trying to figure it out a workaround.

CypressError:cy.task('switchMetamaskAccount')` failed with the following error:

locator.click: Error: strict mode violation: locator('text=empty wallet 1') r esolved to 2 elements: 1) empty wallet 1 aka getByRole('butt on', { name: 'empty wallet 1 0 ETH' }) 2) empty wallet 1 aka getByRole(' button', { name: 'empty wallet 1 0x9C3...620B' })

=========================== logs =========================== waiting for locator('text=empty wallet 1') strict mode violation: locator('text=empty wallet 1') resolved to 2 elements: 1) empty wallet 1 aka getByRole('butt on', { name: 'empty wallet 1 0 ETH' }) 2) empty wallet 1 aka getByRole(' button', { name: 'empty wallet 1 0x9C3...620B' })

`

— Reply to this email directly, view it on GitHub https://github.com/Synthetixio/synpress/issues/362#issuecomment-1382436395, or unsubscribe https://github.com/notifications/unsubscribe-auth/AW5CN5ZZ5JH5RDBOLPCIPSLWSHFSVANCNFSM5RGGBOLQ . You are receiving this because you authored the thread.Message ID: @.***>

kwdikeman avatar Jan 16 '23 15:01 kwdikeman