简体   繁体   中英

How to simulate offline internet connection during selenium automated test and then switch it on again using BrowserMobProxy?

I've faced a problem that in some ui test cases I need to simulate offline internet connection. I've been using BrowserMobProxy to switch it off with abort() method but I can not find a way to switch internet on again. Method start() does not work as it says that proxy server is already started so I have to create new instance of BrowserMobProxy which can not be done at this stage of test execution.

Is there any other way to switch internet on again?

Here is some code in 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

Thanks

  1. setup manualy new chrome profile which won't be able to connect any web site (wrong proxy settings, whatever)
  2. create new instace of WebDriver loaded with the new chrome profile, like this https://www.edureka.co/community/80815/how-to-open-chrome-default-profile-with-selenium
  3. in your test just use required WebDriver instance, both can be opened simultaneously

like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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