dappeteer icon indicating copy to clipboard operation
dappeteer copied to clipboard

What's the correct way to switch to localhost?

Open cinjon opened this issue 2 years ago • 3 comments

Describe the bug Switching to the local network doesn't seem to work correctly. There appear to be multiple suggested ways in the docs. I see switchNetwork("local"), switchNetwork("localhost"), switchNetwork("Localhost8545"). What's the correct way?

To Reproduce In setup.js:

  const browser = await launch(puppeteer, puppeteerConfig);
  try {
    const metamask = await setupMetamask(browser, metamaskOptions);    
    await new Promise(resolve => setTimeout(resolve, 1000));

    // Both of these do not change the network; instead it just stalls at the changing network popup.
    await metamask.switchNetwork("local");
    await metamask.switchNetwork("localhost");

    // This one doesn't stall but also doesn't change the network from Ethereum:
    await metamask.switchNetwork("Localhost8545");

    global.metamask = metamask;
    global.browser = browser;
  } catch (error) {
    console.log(error);
    throw error;
  }

Expected behavior I expect it to switchNetwork but it doesn't do so.

System:

  • dAppeteer version 3.0.0-rc.0
  • testing framework jest
  • testing framework version 26.6.3

Additional context This is in docker.

cinjon avatar Jul 05 '22 18:07 cinjon

This appears to be the correct way - await metamask.switchNetwork("Localhost 8545").

However, when I do that, it just stalls at the window and never switches. See the attached video:

https://user-images.githubusercontent.com/615351/177401684-1cc44866-473d-4b80-b302-75b0447e1cc7.mp4

cinjon avatar Jul 05 '22 19:07 cinjon

Hey @cinjon

I did some research and I have been able to reproduce the case when there is no local EVM network available. If you can switch to a network like kovan => await metamask.switchNetwork('kovan'); then differently is an issue with missing local network. To resolve it start your local network before running tests (ganache, hardhat, etc...)

BeroBurny avatar Jul 06 '22 08:07 BeroBurny

Thanks for the quick response @BeroBurny . You're right that trying 'kovan' instead of any of the other options did work, which suggests that something is off about the local network. However, if I do the following addNetwork:

   await metamask.addNetwork({
      networkName: 'Localhost8545',
      rpc: "http://`${process.env['CHAIN_PROTOCOL']}://${process.env['CHAIN_HOST']}/`,
      chainId: 1337,
      symbol: 'ETH',
      explorer: `${process.env['CHAIN_PROTOCOL']}://${process.env['CHAIN_HOST']}`
    });

I get the error SyntaxError: This Chain ID is currently used by the Localhost 8545 network.

This suggests to me that Localhost 8545 is there already. However, await metamask.switchNetwork("Localhost 8545") still doesn't do anything.

A bit more about this setup is that it's an e2e testing setup on docker. After build and setting up the chain, etc, the Dockerfile is eventually running yarn run hardhat test --network docker, which kicks off tests. The non-web tests connect to the chain and pass without issue, which also suggests to me that the chain is def running. The problem I run into is that only the web tests don't pass.

cinjon avatar Jul 07 '22 00:07 cinjon

This is definitely an issue with the local setup as we have tests for that exact scenario. If you are using dockers, it is very likely that chrome cannot access local chain in docker hence why adding network stalls.

YOu can also try adding a network with a different name to see if the chain id is a problem because you added that chain already.

mpetrunic avatar Nov 11 '22 08:11 mpetrunic