0

I am using Selenium and ChromeDriver to write a test to simulate network connection loss.

this.chromeDriver.setNetworkConnection(NetworkConnection.ConnectionType.AIRPLANE_MODE);
//wait and do actions
this.chromeDriver.setNetworkConnection(NetworkConnection.ConnectionType.WIFI);
//asserts  

My tests run within a container, and currently I am casting an injected instance of a WebDriver that I know is Chrome as this.chromeDriver = (ChromeDriver)this.webDriver; Looking online I do not see a way to turn off network connectivity through the WebDriver instance, and I would like to avoid turning off/on on host level just for the test. Is there a better way of achieving network simulation using Selenium in 3.6?

Niru
  • 1,407
  • 1
  • 28
  • 47

2 Answers2

0

The first thing that comes to mind is direct interface management using Java. For example:

Runtime.getRuntime ().exec ("ipconfig/release");

Naturally, you need to take into account specific points as an operating system.

0

edit: I noticed just now you asked for an answer in 3.6 and this is only for 4.0+ so apologies

You can use the chromium CommandExecutor in your own function like so:

public static void driverGoOffline(ChromeDriver driver) throws RuntimeException {
    Map map = new HashMap();
    map.put("offline", true);
    CommandExecutor executor = ((ChromeDriver)driver).getCommandExecutor();
    try
    {
        Response response = executor.execute(
            new Command(((ChromeDriver)driver).getSessionId(), "setNetworkConditions", ImmutableMap.of("network_conditions", ImmutableMap.copyOf(map)))
        );
    }
    catch (IOException e)
    {
        throw new RuntimeException(e);
    }
}

More info https://stackoverflow.com/a/56526094/17892492

It is best to do it within the automated browser rather than throttling your machine's actual internet connection for many reasons

Joe Arnold
  • 43
  • 5