5

I am testing a Javascript application in Chrome browser. I want to load the page, and once loaded I would like to switch the browser to offline mode.

enter image description here

Note that I DON´T want to start the browser in offline mode as mentioned here: Selenium: How to set browser in offline mode?. Instead I want to start the browser connected and then switch it off and a specific moment.

Note that I don´t want to do it manually with the mouse, because this is for creating an automated test with Selenium. So I need to be able to send the instruction to go offline somehow with a script.

Is there anyway using Chrome to do that? could I modify the browser configuration executing a javascript? (probably not for security reasons but still wondering)

Thanks very much for your help.

4 Answers 4

4

In Python, this can be achieved like this:

driver.set_network_conditions(offline=True, latency=5, throughput=500 * 1024)

PS: I am using ChromeDriver 2.32.

Sign up to request clarification or add additional context in comments.

Comments

2

As was written in of. release notes:

ChromeDriver v2.26 (2016-12-09)

Resolved issue 984: Extend ChromeDriver capabilities to cover network throttling feature from Chrome DevTools [['OS-All', 'Pri-2']]

Here is full history log about this task. But there is no example, and no answer how it works.

Comments

1

In Java, you can use the following code to turn off the WiFi:

protected void disConnectInternet() throws IOException {
Map map = new HashMap();
map.put("offline", true);
map.put("latency", 5);
map.put("download_throughput", 500);
map.put("upload_throughput", 1024);


CommandExecutor executor = ((ChromeDriver)driver).getCommandExecutor();
Response response = executor.execute(
        new Command(((ChromeDriver)driver).getSessionId(), "setNetworkConditions", ImmutableMap.of("network_conditions", ImmutableMap.copyOf(map))));

}

1 Comment

How would you reset network conditions afterwards?
-1

You should be able to switch between online and offline mode using this:

browser.driver.setNetworkConnection(
  typeOrAirplaneMode: 0|1|2|3|4|5|6|7|boolean, wifi?: boolean, data?: boolean
);

More info here: http://webdriver.io/api/mobile/setNetworkConnection.html

1 Comment

the op is about selenium

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.