如何在硒自动测试期间模拟离线互联网连接,然后使用BrowserMobProxy再次打开



我遇到了一个问题,在一些ui测试用例中,我需要模拟离线互联网连接。我一直在使用BrowserMobProxy用abort()方法关闭它,但我找不到重新打开互联网的方法。方法start()不起作用,因为它说代理服务器已经启动,所以我必须创建BrowserMobProxy的新实例,这在测试执行的这个阶段无法完成。

有其他方法可以重新打开互联网吗?

以下是一些Java代码:

BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.start();
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
DesiredCapabilites capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
WebDriver driver = new ChromeDriver(capabilities);
// some test logic here
proxy.abort();
// some assertions here
proxy.start(); // does not work to switch on internet connection
// some assertion here

感谢

  1. 手动设置新的chrome配置文件,该配置文件将无法连接任何网站(错误的代理设置,无论怎样(
  2. 创建加载了新chrome配置文件的WebDriver的新实例,如下所示https://www.edureka.co/community/80815/how-to-open-chrome-default-profile-with-selenium
  3. 在测试中,只需使用所需的WebDriver实例,两者都可以同时打开

像这样:

// besides other imports gonna need this
import org.openqa.selenium.chrome.ChromeOptions;
// standard instance
System.setProperty("webdriver.chrome.driver","your chromedriver.exe path");
WebDriver driver = new ChromeDriver();
// new instance for offline tests
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=PROFILE FOLDER"); // will be similar to "C:/Users/user_name/AppData/Local/Google/Chrome/User Data"
options.addArguments("--start-maximized");
WebDriver driver2 =  new ChromeDriver(options);
driver.get("some test URL");
WebElement e = driver.findElement(By.(Id("FOO")));
driver2.get("some test URL");
WebElement e2 = driver.findElement(By.(Id("BAR")));
// some JUnit test or whatever
assert.equals(e.getText(), e2.getText());

相关内容

最新更新