Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate network failure / offline mode in Capybara?

I have some JavaScript in my app that detects when the network connection goes away and temporarily caches data in local storage, to be synced with the server when the connection is re-established.

I've been trying to find a way to test this end-to-end using Capybara, but I can't seem to find any way of either temporarily disabling the app server or switching the headless browser into offline mode. FWIW I'm using Poltergeist as the driver.

Does anyone have an idea how this could be tested? (I can test the JavaScript app using sinon to fake the server going away, but I'd like to be able to test it end-to-end with a headless browser if possible).

like image 987
Louis Simoneau Avatar asked Oct 31 '22 22:10

Louis Simoneau


1 Answers

If you stumbled on this question looking for a way to test offline / progressive web apps with Capybara and Chrome Headless, here's how:

params = {
  cmd:    'Network.emulateNetworkConditions',
  params: {
    offline:            true,
    latency:            0,
    downloadThroughput: 0,
    uploadThroughput:   0
  }
}
page.driver.browser.send(:bridge).send_command(params)
like image 170
ErJab Avatar answered Nov 09 '22 15:11

ErJab