How to simulate Offline network conditions on Hybrid app?

Update: I’ve just answered my own question, in the reply below.

Hello,

Any idea on how can I simulate Offline network conditions on an Android device, that’s running an app with a Chrome WebView?

I’ve tried the following code:

((ExecuteCDPCommand) this.driver).executeCdpCommand("Network.emulateNetworkConditions", Map.of(
                        "offline", true,
                        "latency", 0,
                        "downloadThroughput", 0,
                        "uploadThroughput", 0));

But it doesn’t do anything.

Since AndroidDriver doesn’t implement the HasNetworkConditions interface, I’m unable to achieve what I usually do with Chromium based drivers, i.e., ((HasNetworkConditions)this.driver).setNetworkConditions(chromiumNetworkConditions);

Just wanted to add that I’ve found a solution. I was close to it, just needed to adjust a few things, and add a couple of steps before executing the Network.emulateNetworkConditions command.

So here it is,

driver.executeCdpCommand("Runtime.enable");
driver.executeCdpCommand("Network.enable");
driver.executeCdpCommand("Network.emulateNetworkConditions", Map.of(
        "offline", true,
        "latency", 0,
        "downloadThroughput", -1,
        "uploadThroughput", -1));

With the above code, the browser (webview) will be set to Offline mode, similarly to when you do it manually via DevTools > Network > Offline.

Keep in mind that this doesn’t put the Native context in an offline state, just the WebView context.
If you need the whole app or device to be in an offline state, you’d still need to turn OFF the Wi-Fi, or turn ON airplane mode.