Skip to content
Advertisement

Kill network connection in the middle of the test using Selenium Java

I’m trying to kill my network in the middle of a test. and when I kill the network, the system should prompt a dialog saying network is gone. When I do this manually, I get the dialog. And I used the following code,

public void exitBrowserTab() throws IOException {

        WebDriver driver = new ChromeDriver();
        driver.get("url");

        driver.findElement(By.id("login-username")).sendKeys("username");
        driver.findElement(By.id("login-password")).sendKeys("password");
        driver.findElement(By.id("signin-button")).click();


        Map map = new HashMap();
        map.put("offline", true);
        map.put("latency", 5);
        map.put("download_throughput", 500);
        map.put("upload_throughput", 1024);

        CommandExecutor executor = ((ChromeDriver)driver).getCommandExecutor();
        Response response = executor.execute(new Command(((ChromeDriver)driver).getSessionId(), "setNetworkConditions", ImmutableMap.of("network_conditions", ImmutableMap.copyOf(map))));



    }

When I run this code, it won’t give me that dialog. but, when I refresh the page, it tells me that there’s no network.

What did I do wrong?

Manual Steps –

  1. Go to the url
  2. Successful login
  3. Network drop
  4. Dialog appears

Advertisement

Answer

I could turn of internet using the bash command,

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

and turn it back on using,

Runtime.getRuntime().exec("cmd /c ipconfig /renew");

This will automatically turn off your wifi and turn it on again.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement