3

I am using below code to simulate slow network condition. Issue is that except for switching between offline or online mode page load or any action is not getting slower even when the code has no errors. Any help is appreciated.

WebDriver augmentedDriver;
ChromiumNetworkConditions networkConditions;

public void switchToNetworkModeUsingAugmentor(String networkMode, String offline, final WebDriver driver) throws IOException {


    if (augmentedDriver == null && networkConditions == null) {
      augmentedDriver = new Augmenter().augment(driver);
      networkConditions = new ChromiumNetworkConditions();
    }
    int currentDownloadThrouhput = networkConditions.getDownloadThroughput();
    int currentUploadThrouhput = networkConditions.getUploadThroughput();
    Duration currentLatency = networkConditions.getLatency();
    Integer currentLatencyInSeconds = (int) currentLatency.getSeconds();
    LOGGER.info("Current Download throughput {}", currentDownloadThrouhput);
    LOGGER.info("Current Upload throughput {}", currentUploadThrouhput);
    LOGGER.info("Current Latency in seconds {}", currentLatencyInSeconds);


    if (networkMode.equalsIgnoreCase("Super Slow")) {
      networkConditions.setDownloadThroughput(5000);
      networkConditions.setUploadThroughput(5000);
      networkConditions.setOffline(BooleanUtils.toBoolean(offline));
      networkConditions.setLatency(Duration.ofMillis(15000));
    } else if (networkMode.equalsIgnoreCase("Slow")) {
      networkConditions.setDownloadThroughput(5000);
      networkConditions.setUploadThroughput(5000);
      networkConditions.setOffline(BooleanUtils.toBoolean(offline));
      networkConditions.setLatency(Duration.ofMillis(10000));
    }

    ((HasNetworkConditions) augmentedDriver).setNetworkConditions(networkConditions);
    
    currentDownloadThrouhput = networkConditions.getDownloadThroughput();
    currentUploadThrouhput = networkConditions.getUploadThroughput();
    currentLatency = networkConditions.getLatency();
    currentLatencyInSeconds = (int) currentLatency.getSeconds();
    LOGGER.info("Current Download throughput {}", currentDownloadThrouhput);
    LOGGER.info("Current Upload throughput {}", currentUploadThrouhput);
    LOGGER.info("Current Latency in seconds {}", currentLatencyInSeconds);


  }

1 Answers1

0

If you need simulate Internet Disconnection or any given specific network conditions as part of your test case you can do it vía Selenium and C# implementing the next code:

    // Replace V110 with the current version of the Selenium.WebDriver.ChromeDriver NuGet package
    using DevToolsSessionDomains = OpenQA.Selenium.DevTools.V110.DevToolsSessionDomains; 
    using Network = OpenQA.Selenium.DevTools.V110.Network;

...

    var devTools = driver as IDevTools;
    var session = devTools.GetDevToolsSession();
    var domains = session.GetVersionSpecificDomains<DevToolsSessionDomains>();
    domains.Network.Enable(new Network.EnableCommandSettings());
    Network.EmulateNetworkConditionsCommandSettings command = new Network.EmulateNetworkConditionsCommandSettings();
    command.Offline = true; // This line allows you to simulate the disconnection 
    command.Latency = 0; // You can also play with thesse next 3 properties if you want to simulate slow connections
    command.DownloadThroughput = 0;
    command.UploadThroughput = 0;
    domains.Network.EmulateNetworkConditions(command);
    
David López
  • 366
  • 3
  • 12