使用硒4模拟慢速网络条件

发布于 2025-02-13 13:10:40 字数 2183 浏览 2 评论 0原文

我正在使用以下代码来模拟慢速网络条件。问题是,除了在离线或在线模式页面之间切换,即使代码没有错误,任何操作也不会变得更慢。任何帮助将不胜感激。

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);


  }

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);


  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2025-02-20 13:10:40

如果您需要模拟Internet断开连接或任何给定的特定网络条件作为测试案例的一部分,则可以这样做,它可以执行víaselenium和c#实施下一个代码:

    // 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);
    

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);
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文