electron: webContents's session can not enableNetworkEmulation

Win10 Electron 7.1.0 and Electron6.1.4

let { remote } = require("electron");
let ses = remote.getCurrentWebContents().session;
await ses.clearCache();
await ses.clearStorageData();
ses.enableNetworkEmulation({
  offline: true
});
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.cnblogs.com/aggsite/AggStats");
xhr.onload = () => console.log(xhr.responseText);
xhr.send();

When I emulate the network outage, the xhr gets the web source correctly.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 3
  • Comments: 16 (10 by maintainers)

Most upvoted comments

Here’s a Fiddle which reproduces the issue: https://gist.github.com/7b6b11c8f57465aa2545e508f0e6d0f1

If it loads the webpage successfully, then this feature is broken.

I can reproduce this.

A little analysis: at least in the post-network-service code (7.x+), it looks like when we set the network conditions, we pass network_emulation_token_, which is never anywhere set as the throttling_profile_id of any request, so the network conditions are never applied.

As a workaround for now, you might try using the Network.emulateNetworkConditions method in the debugger API, e.g.

const dbg = myWebContents.debugger
dbg.attach()
await dbg.sendCommand('Network.enable')
await dbg.sendCommand('Network.emulateNetworkConditions', {offline: true, latency: 0, downloadThroughput: -1, uploadThroughput: -1})

NB. with this workaround, the network condition emulation will only last as long as the debugger is attached.