简体   繁体   English

在 C# 中离线时使用 Selenium-WebDriver 测试 Chrome

[英]Testing Chrome using Selenium-WebDriver while offline in C#

I am using Chrome, and I would like to be able to run automation tests in C# that tests while the application is offline (I am testing a mobile app with PhoneGap ), and later on get online to complete the tests.我正在使用 Chrome,我希望能够在 C# 中运行自动化测试,该测试在应用程序离线时进行测试(我正在使用PhoneGap测试移动应用程序),然后再上线以完成测试。 Is it possible using WebDriver in Selenium 2 ?是否可以在Selenium 2中使用WebDriver

In Google Chrome you can set on developers tool like shown bellow:在 Google Chrome 中,您可以在开发人员工具上进行设置,如下所示:在此处输入图像描述

But I don't know how to set it automatically on Selenium.但我不知道如何在 Selenium 上自动设置。

I know there is a way to check if application is online/offline by checking the navigator.onLine property, but I would like to "fake" it's values for testing purposes;我知道有一种方法可以通过检查navigator.onLine属性来检查应用程序是否在线/离线,但我想“伪造”它的值以用于测试目的;

Selenium is only for web based applications, which means that you cannot run your automation tests when application is offline. Selenium 仅适用于基于 Web 的应用程序,这意味着您无法在应用程序离线时运行自动化测试。

It just replicates the user actions / behavior.它只是复制用户操作/行为。 If your user can access your site being in offline mode, then selenium also can do & vice-versa.如果您的用户可以在离线模式下访问您的站点,那么 selenium 也可以这样做,反之亦然。

@Tito @Vikram piece of code that works perfect for me: @Tito @Vikram 一段非常适合我的代码:

var driver = new ChromeDriver();
driver.NetworkConditions = new ChromeNetworkConditions()
        { IsOffline = true, DownloadThroughput = 5000, UploadThroughput = 5000, Latency = TimeSpan.FromMilliseconds(5) };

now you are in offline mode.现在您处于离线模式。 to switch it back you need to set IsOffline = false.要将其切换回来,您需要设置 IsOffline = false。

In Selenium4 for C# the property NetworkConditions they promised will be in IWebDriver interface在 C# 的 Selenium4 中,他们承诺的属性 NetworkConditions 将在 IWebDriver 接口中

var driver = new ChromeDriver(); var driver = new ChromeDriver(); driver.NetworkConditions = new ChromeNetworkConditions() { IsOffline = true, DownloadThroughput = 5000, UploadThroughput = 5000, Latency = TimeSpan.FromMilliseconds(5) }; driver.NetworkConditions = new ChromeNetworkConditions() { IsOffline = true, DownloadThroughput = 5000, UploadThroughput = 5000, Latency = TimeSpan.FromMilliseconds(5) };

I can not get this code to work on my .NET6.0 Selenium 4.3.0我无法在我的 .NET6.0 Selenium 4.3.0 上使用此代码

Does ChromeNetworkConditions() still exist? ChromeNetworkConditions() 是否仍然存在? If yes, what is the library I should import?如果是,我应该导入什么库?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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