Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visit command does not call the onBeforeLoad function #7933

Closed
delomer opened this issue Jul 9, 2020 · 3 comments
Closed

Visit command does not call the onBeforeLoad function #7933

delomer opened this issue Jul 9, 2020 · 3 comments

Comments

@delomer
Copy link

delomer commented Jul 9, 2020

Current behavior:

onBeforeLoad does not working!

this example:

it('onload', function() {
  cy.on('window:before:load', e => {
    console.log('window:before:load');
  });

  cy.on('window:load', e => {
    console.log('window:load');
  });

  cy.visit('/', {
    onBeforeLoad(win) {
      console.log('onBeforeLoad');
    },
    onLoad(win) {
      console.log('onLoad');
    }
  });
});

Produces this logs:

window:load
onLoad

Desired behavior:

the onLoadBefore callbacks are not called, this logs are missing:

window:before:load
onBeforeLoad

Test code to reproduce

see example above

Versions

Cypress version: 4.5.0
Browser: Chrome 83
OS: Windows 10 Enterprise, Version 1709

@jennifer-shehane
Copy link
Member

I'm not seeing this behavior in 4.10.0 (or 4.5.0).

Perhaps this is relevant to the application you are testing? Can you provide a fully reproducible example?

Screen Shot 2020-07-10 at 12 47 11 PM

@jennifer-shehane jennifer-shehane added the stage: needs information Not enough info to reproduce the issue label Jul 10, 2020
@delomer
Copy link
Author

delomer commented Jul 10, 2020

You are right, my app uses a service-worker and i think if the app is loaded from cache no onLoadBefore event is firing!
When i cancel all already runnig registrations before the test is running the onLoadBefore event is firing.

My updated spec now looks like this and every thing works like expected:

describe('test', () => {
  beforeEach(() => {
   // remove already running service-worker registrations
    if (window.navigator && navigator.serviceWorker) {
      navigator.serviceWorker.getRegistrations().then(registrations => {
        registrations.forEach(registration => {
          registration.unregister();
        });
      });
    }
  });

  it('onload', function() {
    cy.on('window:before:load', e => {
      console.log('window:before:load');
    });

    cy.on('window:load', e => {
      console.log('window:load');
    });

    cy.visit('/', {
      onBeforeLoad(win) {
        console.log('onBeforeLoad');
        // prevent new service-worker registrations
        // https://glebbahmutov.com/blog/cypress-tips-and-tricks/#disable-serviceworker
        delete win.navigator.__proto__.serviceWorker;
      },
      onLoad(win) {
        console.log('onLoad');
      }
    });
  });
});

@jennifer-shehane jennifer-shehane removed the stage: needs information Not enough info to reproduce the issue label Jul 14, 2020
@jennifer-shehane
Copy link
Member

See #702

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants