diff --git a/commands/metamask.js b/commands/metamask.js index ef38eb8dd..aee3e8fc8 100644 --- a/commands/metamask.js +++ b/commands/metamask.js @@ -1232,7 +1232,7 @@ const metamask = { }, async allowToAddAndSwitchNetwork() { await module.exports.allowToAddNetwork(); - await module.exports.allowToSwitchNetwork(); + await allowToSwitchNetworkIfNeeded(); return true; }, async getWalletAddress() { @@ -1335,6 +1335,14 @@ async function switchToCypressIfNotActive() { return switchBackToCypressWindow; } +async function allowToSwitchNetworkIfNeeded() { + await playwright.assignWindows().then(async () => { + if (await playwright.isNotificationOpen()) { + await module.exports.allowToSwitchNetwork(); + } + }); +} + async function activateAdvancedSetting( toggleOn, toggleOff, diff --git a/commands/playwright.js b/commands/playwright.js index f895d7a1d..74c7ea1a9 100644 --- a/commands/playwright.js +++ b/commands/playwright.js @@ -481,4 +481,14 @@ module.exports = { return extensionsData; }, + async isNotificationOpen() { + await sleep(200); + let pages = browser.contexts()[0].pages(); + for (const page of pages) { + if (page.url().includes('notification')) { + return true; + } + } + return false; + }, }; diff --git a/tests/e2e/specs/metamask-spec.js b/tests/e2e/specs/metamask-spec.js index ed8953e72..131583279 100644 --- a/tests/e2e/specs/metamask-spec.js +++ b/tests/e2e/specs/metamask-spec.js @@ -457,5 +457,12 @@ describe('Metamask', () => { expect(approved).to.be.true; }); }); + it(`allowToAddAndSwitchNetwork should switch network if this network was previously already added`, () => { + cy.changeMetamaskNetwork('mainnet'); + cy.get('#addEthereumChain').click(); + cy.allowMetamaskToAddAndSwitchNetwork().then(approved => { + expect(approved).to.be.true; + }); + }); }); });