最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google chrome extension - Testing loss of internet connection during javascript execution - Stack Overflow

programmeradmin3浏览0评论

My problem

I am looking to test how a script handles a loss of internet connection part way through execution. At a lower level I simply need to cause a number of network request to fail after certain point in the script.

I am attempting to keep this test as end to end as possible without mocking any javascript units.

My thoughts on how I might achieve this

Chrome has a useful toggle in it's dev tools under the network tab which simulates a loss of internet connection. I would like to toggle this from the script I am executing (in a similar manner to how debugger triggers a breakpoint under the sources tab).

I suspect this won't be easy as it would create a significant security hole if web pages could trigger this. However, if there is anyway to hack Chrome into a state where it is possible, possibly using an extension or Chromium I'm all ears.

Is there a way to toggle the Chrome 'offline' dev tool from javascript?

Failing this is there any other way to simulate a loss of connection / cause network requests to fail from javascript? (By the way I'm not married to Chrome)

My problem

I am looking to test how a script handles a loss of internet connection part way through execution. At a lower level I simply need to cause a number of network request to fail after certain point in the script.

I am attempting to keep this test as end to end as possible without mocking any javascript units.

My thoughts on how I might achieve this

Chrome has a useful toggle in it's dev tools under the network tab which simulates a loss of internet connection. I would like to toggle this from the script I am executing (in a similar manner to how debugger triggers a breakpoint under the sources tab).

I suspect this won't be easy as it would create a significant security hole if web pages could trigger this. However, if there is anyway to hack Chrome into a state where it is possible, possibly using an extension or Chromium I'm all ears.

Is there a way to toggle the Chrome 'offline' dev tool from javascript?

Failing this is there any other way to simulate a loss of connection / cause network requests to fail from javascript? (By the way I'm not married to Chrome)

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Apr 25, 2017 at 16:54 thodicthodic 2,2691 gold badge21 silver badges38 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

You could create a custom chrome extension with the debugger permission and then attach to the tab. Then you would call Network.enable on that tab and then start emulating network conditions (e.g. go offline)

You could municate with your custom extension (e.g. telling it to go offline at a specific time) using onMessageExternal.

An easier way would be to set a breakpoint in the Sources tab at the critical point & then manually go offline in the Network tab.

Maybe also try this?

setInterval("stop()", 100);

According to docs, window.stop() stops the currently-loading resources from loading, like pressing the "stop" button. Repeatedly doing that on a loop would simulate a network loss.

If you wanted to programmatically disconnect and also restore the connection, maybe something like

var interval = setInterval("stop()", 100);
// To restore connectivity
clearInterval(interval);

Since you are on Javascript try leveraging asynchronous calls or promises to simulate this scenario.

发布评论

评论列表(0)

  1. 暂无评论