import logger from '../lib/logger'; import { browser } from '../state/browser'; import type { Network } from '../state/network'; import { defaultNetworks, network, setNetwork } from '../state/network'; import click from '../utils/click'; import clickByText from '../utils/clickByText'; import existsNow from '../utils/existsNow'; import getTextContent from '../utils/getTextContent'; import type from '../utils/type'; import visit from '../utils/visit'; import showCypress from './showCypress'; import showMetaMask from './showMetaMask'; /** * Show the MetaMask test networks, because they are disabled by default. * @returns {Promise} If the test networks have been activated. * @internal */ async function showTestNetworks(): Promise { await click('.network-display'); if (!(await existsNow('.network-dropdown-testnets--no-visibility'))) { // The test networks are already shown, skip. return false; } await visit('/home.html#settings/advanced'); const selTest = '.settings-page__content-row:nth-child(7) .toggle-button > div:nth-child(1)'; // Switch need two clicks, we do not know why await click(selTest); await click(selTest); await visit('/home.html'); return true; } /** * Switch to a network using its displayed name (e.g "Kovan Test Network"). * @param {string} name The network displayed name. * @returns {Promise} */ async function switchNetwork(name: string): Promise { if (!(await existsNow('.network-dropdown-list'))) { await click('.network-display'); } logger.debug(`Requested to switch to "${name}" network`); await clickByText('.network-name-item', name); await browser.metaMask.waitForTimeout(300); // Need to wait a bit to let MetaMask be stable } /** * Change the MetaMask network. * @param {Network} newNetwork The new network that will be used. * @returns {Promise} */ export default async function changeMetaMaskNetwork(newNetwork: Network): Promise { await showMetaMask(); if (!network) { // The network internal state is undefined, use the current network to populate the state. setNetwork(await getTextContent('.network-display')); } const currentName = await getTextContent('.network-display'); setNetwork(newNetwork); if (network.name === currentName) { logger.debug(`Requested to switch to ${network.name} but MetaMask is already configured to ${network.name}`); await showCypress(); return false; } // This is a built-in network, click on the corresponding button if (Object.values(defaultNetworks).includes(network)) { // Show/hide test networks await showTestNetworks(); await switchNetwork(network.name); } else { // Open the Networks > Add a network page await visit('/home.html#settings/networks/add-network'); // Fill the network form const input = (nth: number) => `.networks-tab__add-network-form-body .form-field:nth-child(${nth}) input`; await type(input(1), network.name); await type(input(2), network.url); await type(input(3), network.chainId.toString()); network.symbol && (await type(input(4), network.symbol)); network.explorer && (await type(input(5), network.explorer)); await click('.networks-tab__add-network-form-footer button:nth-child(2)'); } await showCypress(); return true; }