cypress: Page occasionally does not load from cy.visit()

Current behavior:

99+% of the time this is not an issue, but once in a while the cy.visit() at the beginning of a test will not load the page, no matter how long of a timeout I set, no XHR requests are sent, nothing is loaded in the page. This is only ever seen in cypress tests and has never been seen manually by our testers so I am sure it is a bug with Cypress.

CypressError: Timed out after waiting ‘60000ms’ for your remote page to load.

Your page did not fire its ‘load’ event within ‘60000ms’.

You can try increasing the ‘pageLoadTimeout’ value in ‘cypress.json’ to wait longer.

Browsers will not fire the ‘load’ event until all stylesheets and scripts are done downloading.

When this ‘load’ event occurs, Cypress will continue running commands.

Desired behavior:

Always load the page.

Steps to reproduce: (app code and test code)

I realize I am not providing a reproducible example, but it is not triggered by any specific test and is not reliably reproducible. I have seen in other tickets that members of the cypress team have asked for additional logging to be turned on. I would like to turn on additional logging that may help diagnose this and provide you the logs next time this happens.

Versions

Cypress 3.1.3

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 42
  • Comments: 85 (24 by maintainers)

Most upvoted comments

We also have this issue. Is there any update yet?

I having issue with cy.visit() as well but only with Electron browser… 100% of the time What are the steps of my application

  1. Open users page with cy.visit()
  2. Click on user and navigate to user profile
  3. Call cy.visit() to open users page again.

At the step 3 users page is never opened. 2019-05-21_1346 A small spinner in the URL field is always loading and error of Cypress Timeout 😦 image

UPDATE: The issue was because of the error with ‘beforeUnload’ event. This error was in the console. Chrome ignores is, but Electron not. Found a workaround with this code added to support/index.ts

Cypress.on('window:load', function(window) { const original = window.addEventListener; window.addEventListener = function() { if (arguments && arguments[0] === 'beforeunload') { return; } return original.apply(this, arguments); }; });

Known beforeload issue

We have found a situation where a Page load may time out if there is an event listener on beforeload on the application under test when running in Electron. This would prevent the page from navigating and timeout on the Page Load.

This error looks slightly different from a cy.visit() error timeout and will display the text below.

CypressError: Timed out after waiting '60000ms' for your remote page to load.

Your page did not fire its 'load' event within '60000ms'.

You can try increasing the 'pageLoadTimeout' value in 'cypress.json' to wait longer.

Browsers will not fire the 'load' event until all stylesheets and scripts are done downloading.

When this 'load' event occurs, Cypress will continue running commands.

When this happens:

This situation could happen if you have code similar to any of the code examples below:

window.addEventListener('beforeunload', (e) => {
  e.returnValue = 'Are you sure you want to leave?'
})

window.onbeforeunload = function(){
  return 'Are you sure you want to leave?';
};

Workaround:

There are 2 possible workarounds.

  1. Run your tests in Chrome using the --chrome flag during cypress run or choosing Chrome during cypress open.

OR

  1. Add the code below to your support/index.js file. This will prevent the prompts from occurring - and not hang Electron.
Cypress.on('window:before:load', function (window) {
  const original = window.EventTarget.prototype.addEventListener

  window.EventTarget.prototype.addEventListener = function () {
    if (arguments && arguments[0] === 'beforeunload') {
      return
    }
    return original.apply(this, arguments)
  }

  Object.defineProperty(window, 'onbeforeunload', {
    get: function () { },
    set: function () { }
  })
})

Everyone

Please try the workaround code above. If this solves your issue, refer to https://github.com/cypress-io/cypress/issues/2118 if this is your issue for official updates. This seems to be the issue for @mariusbutuc, @mattcrooks specifically.

We have had to give up on cypress due to this issue causing our team to loose trust in our tests.

Fingers crossed it can be fixed. This is a game stopper unfortunately for now.

Upgrading from Cypress 3.1.5 to 3.3.1, I’m having this issue only on CI for some reason.

And in 100% of the cases, no way to make it work on CI, but all fine locally 🤷‍♂️

This is happening 100% of the time on a particular page for us. This only seems to happen when when the page requests confirmation to navigate away. There is an error in the console: Blocked attempt to show a 'beforeunload' confirmation panel for a frame that never had a user gesture since its load

Hoping this helps with the diagnosis

This only happens in Electron not Chrome.

We’ve blacklisted the hosts linked in #1608 and are still seeing this issue

So for me, it seems that this was an issue with loading resources on the page. I am testing a react application, and the first request to the page will compile the JS bundle. The first request was always taking much longer than the initial timeout since it is a large application. Subsequent attempts succeed (on the same machine) because the initial request already started the build process.

It seems like it might be helpful for the error message to indicate that there were still some resources loading if the visit doesn’t succeed because of resources (and maybe what the resources were). My page itself was loading from the server, but the load event was what wasn’t triggering. I realize that the error message indicates that for the most part, but more details would probably help.

@jennifer-shehane :

seeing this too on

Cypress:    3.2.0                                                                              
Browser:    Electron 59 (headless)

The spec passes when run using cypress open but fails when running in circle (underlying command is ./node_modules/.bin/cypress run --reporter junit --reporter-options 'mochaFile=results/junit.xml' --spec <path-to-our.spec.js>)

The test does has a beforeEach which executes a Command to make an API request. In the .then of that command, we run another to login as a user. We do both of these in most test with no problem.

The test then does the following:

cy.window().then((window) => {
	  const hasSeenIntroScreen = window.localStorage.getItem(
        LOCAL_STORAGE_INTRO_KEY,
      );
	  // this log is printed 
      cy.log('User has seen Intro screen', hasSeenIntroScreen);
      if (hasSeenIntroScreen) {
        // this cy.visit fails
        cy.visit(`${getUrl()}?step=step1`);
      } else {
        // something else
      }
}

Could it have something to do with cy.window? or with window.localStorage.getItem?

Also to note, the app page shows a confirm alert when exiting (the cy.visit would trigger that). Cypress seems to automatically accept alerts (https://docs.cypress.io/api/events/catalog-of-events.html#Window-Alert) and i’m seeing this when running the test with open but maybe this doesn’t work well when running with run. In which case, this could be related to https://github.com/cypress-io/cypress/issues/2118.

I’m able to repro this so let me know what info you need

@jennifer-shehane How is this awaiting information when you have shown reproductions in cypress’ own repos? https://github.com/cypress-io/cypress/issues/2938#issuecomment-448135595 As a paying subscriber is it possible to get any extra help in fixing this issue? I’m making a feature to investigate and fix this in cypress, but its pretty difficult… It would probably already had a resolution if someone had added exposed logging that could tell us what the problem is, because then we could run it on our builds, see the error and know what needed fixing.

Same thing here, on circleCI, even after upgrading to 3.1.5 that supposed to fix an onLoad bug.

I’ve ended up solving our problems by retrying : https://github.com/cypress-io/cypress/issues/1313#issuecomment-409342834

I too have been seeing this issue for several months. I tend to get anywhere between 1 to 3 test failures in every other run or so due to timeout, regardless of how high I configure it.

We have retries running but it’s causing the team to loose confidence in the stability of the tests.

I’m running 400+ tests hourly as well as chained to deployments and every time it breaks the build a little part of me dies.

Thanks I have deleted the history and update again.

I also want to make sure everyone visiting this issue is aware of a known issue when calling cy.visit() to a url that you are currently navigated to - https://github.com/cypress-io/cypress/issues/1311

It exhibits the exact same error of timing out waiting for the ‘load’ event - so ensure this is not the case for you - you likely would not be intentionally visiting the same url twice, but your application may be rerouting and end up visiting the exact url you are already on.

I’ve tried:

  1. navigating to a different page first
  2. turning on full logging
  3. waiting for a second before cy.visit

Is there some way to catch the visit failure and try visiting again?