简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM