cypress icon indicating copy to clipboard operation
cypress copied to clipboard

Cypress.automation('remote:debugger:protocol') command started failing in Cypress 7.3.0

Open c32hedge opened this issue 3 years ago • 20 comments

Current behavior

Starting in 7.3.0, the test code below (mostly copied from https://www.cypress.io/blog/2020/11/12/testing-application-in-offline-network-mode/) throws this error:

CypressError: `cy.then()` timed out after waiting `4000ms`.

Your callback function returned a promise that never resolved.

The callback function was:

() => Cypress.automation('remote:debugger:protocol', {
      command: 'Network.emulateNetworkConditions',
      params: {
        offline: true,
        latency: -1,
        downloadThroughput: -1,
        uploadThroughput: -1
      }
    })
    at https://www.cypress.io/__cypress/runner/cypress_runner.js:136167:24
From previous event:
    at Context.thenFn (https://www.cypress.io/__cypress/runner/cypress_runner.js:136166:13)
    at Context.then (https://www.cypress.io/__cypress/runner/cypress_runner.js:136597:21)
    at Context.<anonymous> (https://www.cypress.io/__cypress/runner/cypress_runner.js:151455:21)
From Your Spec Code:
    at Context.eval (https://www.cypress.io/__cypress/tests?p=cypress/integration/spec.js:133:9)

Desired behavior

The test should run without error.

Test code to reproduce

Set the baseUrl configuration key (e.g. "https://www.cypress.io") and run the following spec. I can reproduce without setting the baseUrl but it's a little more complicated and I think this is better as a MWE.

describe('page', () => {
  it('tries to go offline', () => {
    cy.log('**go offline**')
    .then(() =>
      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
          }
        })
      );
    cy.visit('/');
  });
});

Cypress Version

7.3.0

Other

I encountered this when attempting to update to the latest version of Cypress, but traced it back to 7.3.0. Version 7.2.0 runs without error.

It looks like at least a couple other people have encountered this based on the comments on that blog post. I found that modifying the callback in the second then to not return anything did allow my tests to get further, only to hang indefinitely at random places in my test file so, as I suspected, that isn't a real solution.

c32hedge avatar Aug 12 '21 23:08 c32hedge

+1 to this. I'm seeing the same problem.

InfiniteTape avatar Aug 19 '21 17:08 InfiniteTape

The same regression issue in 8.0.0

szhong-becls avatar Aug 23 '21 02:08 szhong-becls

@jennifer-shehane did this get lost? It's been two weeks with no response 😦

c32hedge avatar Aug 24 '21 13:08 c32hedge

@jennifer-shehane Gleb pointed me to this Cypress recipe on the Cypress Gitter: https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/server-communication__offline/cypress/integration/offline-spec.js

Which is almost identical to my code except for the use of braces and explicit return statements for the arrow function bodies. Yet, I was able to use the MWE above in a cypress-test-tiny fork and trace the failure back to 7.3.0 as described.

I realize this is a bit of an obscure feature, but you have blogs and recipes that reference it, and multiple people, both here and in the comments on the blog post, are struggling getting code referenced in those blogs/recipes to work in newer versions of Cypress. Could you at least confirm whether you can reproduce the error I'm seeing?

c32hedge avatar Aug 25 '21 16:08 c32hedge

According to my investigation, this issue may be injected by this commit in socketio/engine.io-client repo. It supports a listen to the "offline" event feature in version 5.0.0 (2021-03-10).

The problemic code is as follows:

+ if (this.hostname !== "localhost") {
+   this.offlineEventListener = () => {
+     this.onClose("transport close");
+   };
+   addEventListener("offline", this.offlineEventListener, false);
+ }

The socket is closed after sending CDP command: { "command": "Network.emulateNetworkConditions", "parameters": { "offline": true } }. Then the command response is never received and Cypress.automation('remote:debugger:protocol', { ... }) is timeout.

szhong-becls avatar Aug 26 '21 07:08 szhong-becls

any idea or workaround? Can i get an older version of cypress to make it work?

It only fails when is not localhost, in fact i can go offline but i cant recover from this status to the online status

https://github.com/cypress-io/cypress/issues/17723#issuecomment-906152925

raultm avatar Sep 09 '21 17:09 raultm

A quick workaround would be to use a version before cypress 7.0.0. As I stated here https://github.com/cypress-io/cypress-example-recipes/issues/721#issuecomment-920609040 I got it successfully running in version 6.9.1.

svenjungnickel avatar Sep 16 '21 06:09 svenjungnickel

Another "work around". With the fact that Network.emulateNetworkConditions and offline: true did set the Chrome network to offline, but Cypress cannot receive the command execution result. So we can tell Cypress to ignore this particular error, and continue to run our test specs.

cy.on('fail', e => {
  if (
    e.name === 'CypressError'
    && e.message.includes('timed out')
    && e.message.includes('remote:debugger:protocol')
    && e.message.includes('Network.emulateNetworkConditions')
    && e.message.includes('offline: true')
  ) {
    // do not fail the test
    return false;
  }
});

cy.log('**go offline**')
  .then(() =>
    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
      }
    })
  );

szhong-becls avatar Sep 16 '21 06:09 szhong-becls

@szhong-becls your workaround did not work for me, unfortunately.

svenjungnickel avatar Sep 16 '21 06:09 svenjungnickel

Any update on this? My tests are also failing where this functionality is required

matthew-nash-cko avatar Sep 24 '21 08:09 matthew-nash-cko

I am also experiencing this issue on our project when I upgraded to v8.6.0. Is there a provided solution or an ETA for a fix? We cannot upgrade till this is fixed. 😭

myasul avatar Oct 20 '21 12:10 myasul

I am also suffering with this issue. I have confirmed Cypress 6.9.1 is still working properly. I have tested Cypress 6.9.1 against Chrome 96 and Electron 87 (working properly in both cases). Cypress 8.7.0 fails, Cypress 9.0.0 fails. So, or we go back to 6.9.1, or...

lorenzofidalgo avatar Nov 19 '21 10:11 lorenzofidalgo

Same for me :( Cannot upgrade cypress version because of this issue

maayanzrihann avatar Nov 22 '21 15:11 maayanzrihann

Same! :eyes:

Gu7z avatar Nov 24 '21 15:11 Gu7z

Same, still not working in 9.1.1

Do anyone know alternative solution to simulate browser offline mode?

ughunoup avatar Dec 16 '21 15:12 ughunoup

Same here. Stuck with 6.9.1 because of this issue

weilu avatar Jan 19 '22 20:01 weilu

Another "work around". With the fact that Network.emulateNetworkConditions and offline: true did set the Chrome network to offline, but Cypress cannot receive the command execution result. So we can tell Cypress to ignore this particular error, and continue to run our test specs.

cy.on('fail', e => {
  if (
    e.name === 'CypressError'
    && e.message.includes('timed out')
    && e.message.includes('remote:debugger:protocol')
    && e.message.includes('Network.emulateNetworkConditions')
    && e.message.includes('offline: true')
  ) {
    // do not fail the test
    return false;
  }
});

cy.log('**go offline**')
  .then(() =>
    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
      }
    })
  );

This workaround worked for my with cypress open but hangs for cypress run

distante avatar Mar 21 '22 12:03 distante

same problem on "cypress": "^9.6.1"

xdubx avatar Aug 09 '22 15:08 xdubx

Can we please get this working? None of the workarounds above works anymore. I hate to be stuck with version 6.9 because of this

weilu avatar Oct 27 '22 19:10 weilu

We were also going offline and online in tests and are missing it... +1 here.

snefa avatar Nov 24 '22 12:11 snefa

Year 2023 Cypress v10.3.1 Hi there!👋 Issue with going back online still exist.

asmirnov-style avatar Jan 05 '23 22:01 asmirnov-style

Hope everyone had a happy holiday season! As of v12.3.0 looks like the remote:debugger:protocol still fails to go back online. Can anyone else confirm?

robertbenthompson avatar Jan 10 '23 15:01 robertbenthompson

Hope everyone had a happy holiday season! As of v12.3.0 looks like the remote:debugger:protocol still fails to go back online. Can anyone else confirm?

Yes, unfortunately this issue stills. Any workaround that worked for you?

Shnrqpdr avatar Jan 23 '23 13:01 Shnrqpdr

Cypress 12.6.0, still has the same issue! How do you guys test offline/online in Cypress??

AlanCodega avatar Feb 22 '23 13:02 AlanCodega

Cypress 12.6.0, still has the same issue! How do you guys test offline/online in Cypress??

Pls check my answer (the final answer in the issue discussion) at https://github.com/cypress-io/cypress/issues/235 (i couldn't cite the issue), that workaround worked for me.

Shnrqpdr avatar Mar 03 '23 20:03 Shnrqpdr

Cypress 12.6.0, still has the same issue! How do you guys test offline/online in Cypress??

@AlanCodega mock all requests

xdubx avatar Mar 06 '23 21:03 xdubx

Kind a way for simulate OFFLINE (while Cypress team fixing 4 y.o bug)

` goOfflineIntercept() {
        cy.log("**go offline**").then(() => {
            cy.intercept(
                {
                    method: "GET",
                    url: "*",
                },
                {
                    statusCode: 404, // if you like and need it
                }
            );
        });
    }

goOnlineIntercept() {
        cy.log("**go online**").then(() => {
            cy.intercept("GET", "*", (req) => {
                req.continue();
            });
        });
    }`

asmirnov-style avatar Mar 06 '23 21:03 asmirnov-style

Thanks @asmirnov-style!

I adapted mine a bit too:

const goOffline = () => {
    cy.log('**go offline**')
        // stub every request with a StaticResponse to simulate network error
        .then(() => cy.intercept('*', { forceNetworkError: true }))
        .then(() =>
            cy.window().then((win) => win.dispatchEvent(new Event('offline')))
        )
}
const goOnline = () => {
    cy.log('**go online**')
        // go back to normal network behavior
        .then(() => cy.intercept('*'))
        .then(() =>
            cy.window().then((win) => win.dispatchEvent(new Event('online')))
        )
}

KaiVandivier avatar Mar 15 '23 16:03 KaiVandivier

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');
});

buffcode avatar May 24 '23 20:05 buffcode

Is this issue fixed in v13?

I tried this test and the results are as follows:

  • v12.17.4: failed
  • v13.0.0: passed

megos avatar Feb 21 '24 04:02 megos