-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(metamask): Add network switching on demand (#1005)
- Loading branch information
1 parent
04ef607
commit 64297af
Showing
7 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
wallets/metamask/src/pages/HomePage/actions/switchNetwork.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { Page } from '@playwright/test' | ||
import Selectors from '../selectors' | ||
|
||
export async function switchNetwork(page: Page, networkName: string) { | ||
await page.locator(Selectors.networkDropdown.dropdownButton).click() | ||
|
||
const networkLocators = await page.locator(Selectors.networkDropdown.networks).all() | ||
|
||
const networkNames = await page.locator(Selectors.networkDropdown.networks).allTextContents() | ||
|
||
console.log({ networkNames }) | ||
|
||
const seekedNetworkNames = networkNames.filter((name) => name.toLocaleLowerCase() === networkName.toLocaleLowerCase()) | ||
|
||
if (seekedNetworkNames.length === 0) { | ||
throw new Error(`[SwitchNetwork] Network with name ${networkName} not found`) | ||
} | ||
|
||
// biome-ignore lint/style/noNonNullAssertion: this non-null assertion is intentional | ||
const accountIndex = networkNames.indexOf(seekedNetworkNames[0]!) // TODO: handle the undefined here better | ||
|
||
// biome-ignore lint/style/noNonNullAssertion: this non-null assertion is intentional | ||
await networkLocators[accountIndex]!.click() // TODO: handle the undefined here better | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { testWithSynpress } from 'fixtures' | ||
import { MetaMask, unlockForFixture } from '../../../src' | ||
|
||
import basicSetup from '../wallet-setup/basic.setup' | ||
|
||
const test = testWithSynpress(basicSetup, unlockForFixture) | ||
|
||
const { expect } = test | ||
|
||
test.use({ | ||
permissions: ['clipboard-read'] | ||
}) | ||
|
||
test('should switch network', async ({ context, metamaskPage }) => { | ||
const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword) | ||
|
||
await metamask.openSettings() | ||
|
||
const SidebarMenus = metamask.homePage.selectors.settings.SettingsSidebarMenus | ||
await metamask.openSidebarMenu(SidebarMenus.Advanced) | ||
|
||
await metamask.toggleShowTestNetworks() | ||
|
||
const networkBefore = await metamaskPage.locator(metamask.homePage.selectors.currentNetwork).textContent() | ||
|
||
expect(networkBefore).toEqual('Ethereum Mainnet') | ||
|
||
const targetNetwork = 'Sepolia test network' | ||
await metamask.switchNetwork(targetNetwork) | ||
|
||
const networkAfter = await metamaskPage.locator(metamask.homePage.selectors.currentNetwork).textContent() | ||
expect(networkAfter).toEqual(targetNetwork) | ||
}) | ||
|
||
test('should throw an error if there is no account with target name', async ({ context, metamaskPage }) => { | ||
const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword) | ||
|
||
const accountName = 'Account 420' | ||
const switchAccountPromise = metamask.switchAccount(accountName) | ||
|
||
await expect(switchAccountPromise).rejects.toThrowError(`[SwitchAccount] Account with name ${accountName} not found`) | ||
}) |