使用硒4模拟慢速网络条件
我正在使用以下代码来模拟慢速网络条件。问题是,除了在离线或在线模式页面之间切换,即使代码没有错误,任何操作也不会变得更慢。任何帮助将不胜感激。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要模拟Internet断开连接或任何给定的特定网络条件作为测试案例的一部分,则可以这样做,它可以执行víaselenium和c#实施下一个代码:
...
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:
...