Skip to content

Commit

Permalink
Merge pull request #750 from Synthetixio/foundry
Browse files Browse the repository at this point in the history
improve add/change network
  • Loading branch information
r3kt-eth authored May 13, 2023
2 parents 86eda0d + 2c20493 commit 371d6aa
Show file tree
Hide file tree
Showing 11 changed files with 402 additions and 190 deletions.
4 changes: 2 additions & 2 deletions commands/etherscan.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ let retries = 0;

module.exports = {
async getTransactionStatus(txid) {
const { getNetwork } = require('../helpers');
const currentNetwork = getNetwork().networkName;
const { getCurrentNetwork } = require('../helpers');
const currentNetwork = getCurrentNetwork().name;
const etherscanApi = require('etherscan-api').init(
process.env.ETHERSCAN_KEY,
currentNetwork,
Expand Down
189 changes: 110 additions & 79 deletions commands/metamask.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ const {
const {
confirmationPageElements,
} = require('../pages/metamask/confirmation-page');
const { setNetwork, getNetwork } = require('../helpers');
const {
setNetwork,
addNetwork,
findNetwork,
checkNetworkAdded,
getCurrentNetwork,
} = require('../helpers');

let extensionInitialUrl;
let extensionId;
Expand Down Expand Up @@ -372,114 +378,137 @@ const metamask = {
return true;
},
async changeNetwork(network) {
const currentNetwork = getNetwork();
// check if network is available in presets
if (typeof network === 'string') {
network = await findNetwork(network);
}

if (
typeof network === 'string' &&
(currentNetwork.networkDisplayName === network.toLowerCase() ||
currentNetwork.networkName === network.toLowerCase())
)
// handle a case if network is already changed
const currentNetwork = getCurrentNetwork();
if (network === currentNetwork) {
return false;
}

if (
typeof network === 'object' &&
(currentNetwork.networkDisplayName ===
network.networkName.toLowerCase() ||
currentNetwork.networkName === network.networkName.toLowerCase())
)
return false;
// uncomment to automatically add network if not added yet
// const networkAdded = await checkNetworkAdded(network);
// if (!networkAdded) {
// await module.exports.addNetwork(network);
// return true;
// }

await switchToMetamaskIfNotActive();
await playwright.waitAndClick(mainPageElements.networkSwitcher.button);
if (typeof network === 'string') {
network = network.toLowerCase();
if (network === 'mainnet') {
await playwright.waitAndClick(
mainPageElements.networkSwitcher.mainnetNetworkItem,
);
} else if (network === 'goerli') {
await playwright.waitAndClick(
mainPageElements.networkSwitcher.goerliNetworkItem,
);
} else if (network === 'sepolia') {
await playwright.waitAndClick(
mainPageElements.networkSwitcher.sepoliaNetworkItem,
);
} else if (network === 'localhost') {
await playwright.waitAndClick(
mainPageElements.networkSwitcher.localhostNetworkItem,
);
} else {
await playwright.waitAndClickByText(
mainPageElements.networkSwitcher.dropdownMenuItem,
network,
);
}
await playwright.waitForText(
mainPageElements.networkSwitcher.networkName,
network,
);
} else if (typeof network === 'object') {
network.networkName = network.networkName.toLowerCase();
await playwright.waitAndClickByText(
mainPageElements.networkSwitcher.dropdownMenuItem,
network.networkName,
);
await playwright.waitForText(
mainPageElements.networkSwitcher.networkName,
network.networkName,
);
}

await playwright.waitAndClickByText(
mainPageElements.networkSwitcher.dropdownMenuItem,
network.name,
);
await playwright.waitForText(
mainPageElements.networkSwitcher.networkName,
network.name,
);

await playwright.waitUntilStable();
await module.exports.closePopupAndTooltips();
// set network to currently active
await setNetwork(network);
await switchToCypressIfNotActive();
return true;
},
async addNetwork(network) {
await switchToMetamaskIfNotActive();
// check if available in presets
if (typeof network === 'string') {
network = await findNetwork(network);
}

// backward compatibility with older synpress versions
if (
typeof network === 'object' &&
(network.name || network.networkName) &&
network.rpcUrl &&
network.chainId &&
network.symbol
) {
network = {
id: network.chainId,
name: network.name || network.networkName,
nativeCurrency: {
symbol: network.symbol,
},
rpcUrls: {
public: { http: [network.rpcUrl] },
default: { http: [network.rpcUrl] },
},
testnet: network.isTestnet,
};

if (network.blockExplorer) {
network.blockExplorers = {
etherscan: { url: network.blockExplorer },
default: { url: network.blockExplorer },
};
}
}

// dont add network if already present
const networkAlreadyAdded = await checkNetworkAdded(network);
if (networkAlreadyAdded) {
// uncomment to automatically change network if it was already added
// await module.exports.changeNetwork(network);
return false;
}

// handle adding network with env vars
if (
process.env.NETWORK_NAME &&
process.env.RPC_URL &&
process.env.CHAIN_ID
process.env.CHAIN_ID &&
process.env.SYMBOL
) {
network = {
networkName: process.env.NETWORK_NAME,
rpcUrl: process.env.RPC_URL,
chainId: process.env.CHAIN_ID,
symbol: process.env.SYMBOL,
blockExplorer: process.env.BLOCK_EXPLORER,
isTestnet: process.env.IS_TESTNET,
id: process.env.CHAIN_ID,
name: process.env.NETWORK_NAME,
nativeCurrency: {
symbol: process.env.SYMBOL,
},
rpcUrls: {
public: { http: [process.env.RPC_URL] },
default: { http: [process.env.RPC_URL] },
},
blockExplorers: {
etherscan: { url: process.env.BLOCK_EXPLORER },
default: { url: process.env.BLOCK_EXPLORER },
},
testnet: process.env.IS_TESTNET,
};
}
if (typeof network === 'string') {
network = network.toLowerCase();
} else if (typeof network === 'object') {
network.networkName = network.networkName.toLowerCase();
}

// add network to presets
await addNetwork(network);

await switchToMetamaskIfNotActive();

await module.exports.goToAddNetwork();
await playwright.waitAndType(
addNetworkPageElements.networkNameInput,
network.networkName,
network.name,
);
await playwright.waitAndType(
addNetworkPageElements.rpcUrlInput,
network.rpcUrl,
network.rpcUrls.default.http[0],
);
await playwright.waitAndType(
addNetworkPageElements.chainIdInput,
network.chainId,
network.id,
);
await playwright.waitAndType(
addNetworkPageElements.symbolInput,
network.nativeCurrency.symbol,
);
if (network.symbol) {
await playwright.waitAndType(
addNetworkPageElements.symbolInput,
network.symbol,
);
}
if (network.blockExplorer) {
await playwright.waitAndType(
addNetworkPageElements.blockExplorerInput,
network.blockExplorer,
network.blockExplorers.default.url,
);
}
await playwright.waitAndClick(
Expand All @@ -490,11 +519,12 @@ const metamask = {
},
);
await module.exports.closePopupAndTooltips();
await setNetwork(network);
await playwright.waitForText(
mainPageElements.networkSwitcher.networkName,
network.networkName,
network.name,
);
// set as currently active network
await setNetwork(network);
await switchToCypressIfNotActive();
return true;
},
Expand Down Expand Up @@ -1218,7 +1248,8 @@ const metamask = {
const isCustomNetwork =
(process.env.NETWORK_NAME &&
process.env.RPC_URL &&
process.env.CHAIN_ID) ||
process.env.CHAIN_ID &&
process.env.SYMBOL) ||
typeof network == 'object';
if (playwrightInstance) {
await playwright.init(playwrightInstance);
Expand Down
7 changes: 5 additions & 2 deletions commands/playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,14 @@ module.exports = {
},
async waitAndClickByText(selector, text, page = metamaskWindow) {
await module.exports.waitFor(selector, page);
const element = page.locator(`text=${text}`);
await element.click();
const element = `:is(:text-is("${text}"), :text("${text}"))`;
await page.click(element);
await module.exports.waitUntilStable();
},
async waitAndType(selector, value, page = metamaskWindow) {
if (typeof value === 'number') {
value = value.toString();
}
const element = await module.exports.waitFor(selector, page);
await element.type(value);
await module.exports.waitUntilStable(page);
Expand Down
4 changes: 2 additions & 2 deletions docs/synpress-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ Switch to metamask notification window.
switchToMetamaskNotification(): Chainable<Subject>;
```

#### `cy.getNetwork()`
#### `cy.getCurrentNetwork()`

Get current network.

```ts
getNetwork(): Chainable<Subject>;
getCurrentNetwork(): Chainable<Subject>;
```

#### `cy.addMetamaskNetwork()`
Expand Down
Loading

0 comments on commit 371d6aa

Please sign in to comment.