cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.09k stars 3.19k forks source link

Simulate offline mode #235

Open bahmutov opened 8 years ago

bahmutov commented 8 years ago

Description Offline support is becoming important to web applications. It would be nice to switch the browser to the offline mode from Cypress either by test or for all tests and run it. If might be difficult and might require Cypress to handle ServiceWorkers very well.

Additional Info

laerteneto commented 2 years ago

I tried, but it still does not work on Chrome when I am trying to get back online, it only works on Electron.

I also tried the solution posted in here, but without success: https://www.cypress.io/blog/2020/11/12/testing-application-in-offline-network-mode/

Copy exactly same from above and try again

aktibaba commented 2 years ago

your code is missing return, premise returning nothing, try this

cy.log('go offline'); return Cypress.automation('remote:debugger:protocol', { command: 'Network.enable', }).then(() => { Cypress.automation('remote:debugger:protocol', { command: 'Network.emulateNetworkConditions', params: { offline: true, latency: -1, downloadThroughput: -1, uploadThroughput: -1, }, }) return cy.wait(1000); });

laerteneto commented 2 years ago

your code is missing return, premise returning nothing, try this

cy.log('go offline'); return Cypress.automation('remote:debugger:protocol', { command: 'Network.enable', }).then(() => { Cypress.automation('remote:debugger:protocol', { command: 'Network.emulateNetworkConditions', params: { offline: true, latency: -1, downloadThroughput: -1, uploadThroughput: -1, }, }) return cy.wait(1000); });

I managed to get it offline, but the problem is to put it back online, in which it is working well only on Electron.

ifn commented 2 years ago

Downgraded Cypress to 7.2.0. Seems to work.

UPD Nope. Also rather torturous experience. I'll better try using forceNetworkError.

AndreyGulevich commented 2 years ago

Same problem: 7.9.0 version.

DariusNorv commented 2 years ago

I observe this problem in 10+ version in Electron runner as well.

maximkoshelenko commented 2 years ago

This one works for me

const goOffline = () => { cy.log('go offline'); return Cypress.automation('remote:debugger:protocol', { command: 'Network.enable', }).then(() => { Cypress.automation('remote:debugger:protocol', { command: 'Network.emulateNetworkConditions', params: { offline: true, latency: -1, downloadThroughput: -1, uploadThroughput: -1, }, }) return true }); };

Shnrqpdr commented 1 year ago

I'm facing this problem too. I'm using this code below:

network(params) {
    cy.log(`**Offline: ${params.offline}**`)
      .then(() => {
        Cypress.automation('remote:debugger:protocol', {
          command: 'Network.enable',
        });
      })
      .then(() => {
        Cypress.automation('remote:debugger:protocol', {
          command: 'Network.emulateNetworkConditions',
          params: {
            offline: params.offline,
            latency: 0,
            downloadThroughput: 0,
            uploadThroughput: 0,
            connectionType: 'none',
          },
        });
      });
  },

I've work with cypress 9.5.1. After going offline, can't get back online. Anyone has a solution for this?

asmirnov-style commented 1 year ago

Cypress v10.3.1 and still the same issue with going back Online. Looks like it's turn Network for a second and then break connection. Any ideas?

Shnrqpdr commented 1 year ago

Hello everyone

I have been stuck into this issue for at least 2 weeks ago and i got a "solution" to try simulate offline mode. The code is in the image below

image

Create this function on custom commands file of your project and call it using "cy.network({online: false})" This work for me. I hope that this workaround work for you too.

I'm using cypress 9.5.1

lolpez commented 1 year ago

Guys it's 2023 👀

mostafijur022 commented 1 year ago

laerteneto @laerteneto did you get any solution?

buffcode commented 1 year ago

Modifying the real onLine attribute, tested in Cypress 12.11.0:

// go offline
cy.window().then((win) => {
  cy.stub(win.navigator, 'onLine').value(false);
  cy.wrap(win).trigger('offline');
});

// go online
cy.window().then((win) => {
  cy.stub(win.navigator, 'onLine').value(true);
  cy.wrap(win).trigger('online');
});
AntolinaGonzalez commented 1 year ago

@laerteneto did you get any solution? Im stuck with go online too

maxim-koshelenko commented 1 year ago

Modifying the real onLine attribute, tested in Cypress 12.11.0:

// go offline
cy.window().then((win) => {
  cy.stub(win.navigator, 'onLine').value(false);
  cy.wrap(win).trigger('offline');
});

// go online
cy.window().then((win) => {
  cy.stub(win.navigator, 'onLine').value(true);
  cy.wrap(win).trigger('online');
});

Confirming the workability of this solution.

Cypress v10, Chrome 117 (only in headless).

Don't forget to put go online into before, beforeEach and afterEach

lolpez commented 7 months ago

Guys it's 2024 👀

snapris commented 4 months ago

I'm using this for Cypress 13.11.0 and Chrome v126: https://www.cypress.io/blog/2020/11/12/testing-application-in-offline-network-mode Works for me.

gabscogna commented 4 weeks ago

8 years later....

I did exactly what it's in this article, but the problem is that all tests after goOnline become absolutely slowly and I can't figure why. It seems like Network.disable doesn't work.