Cypress: Add ability to wait for script to load (like cy.script('js') + cy.wait())

Created on 12 Feb 2018  Â·  10Comments  Â·  Source: cypress-io/cypress

Current behavior:

I am working on a React application where we create splitting points here and there in order to separate the code into different bundles. This means that sometimes it is necessary to wait for a script to load before continuing. The only way I found of doing that was by adding a cy.wait(5000) for those cases. Is there any other way of doing this?

Desired behavior:

Similar approach to cy.route so that you can specify which scripts to wait for. Something like:

cy.script('js*').as('getBundle');

cy.wait('@getBundle').then(...)
  • Operating System: macOS High Sierra
  • Cypress Version: 1.4.2
  • Browser Version: Chrome 64
feature

Most helpful comment

Is this possible yet?

All 10 comments

This would be possible when full network stubbing feature lands

Sent from my iPhone

On Feb 12, 2018, at 08:41, Bruno Sampaio notifications@github.com wrote:

Operating System: macOS High Sierra
Cypress Version: 1.4.2
Browser Version: Chrome 64
Is this a Feature or Bug?

Feature

Current behavior:

I am working on a React application where we create splitting points here and there in order to separate the code into different bundles. This means that sometimes it is necessary to wait for a script to load before continuing. The only way I found of doing that was by adding a cy.wait(5000) for those cases. Is there any other way of doing this?

Desired behavior:

Similar approach to cy.route so that you can specify which scripts to wait for. Something like:

cy.script('js*').as('getBundle');

cy.wait('@getBundle').then(...)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

Yes, this epic is tracking the network stubbing rewrite. @bahmutov started on it but had to pivot to finish other projects. It's coming relatively soon.

https://github.com/cypress-io/cypress/issues/687

Is this possible yet?

This also seems to be my problem.
Initial problem was that fetch api wasn't working.
I overcame this problem, but now it seems to start loading over fetch without first loading my *.js file.

Is there any update on that issue? cc @brian-mann

Any updates on the subject ?

If your script sets a global variable after loading, let's say google, you can use this command:

Cypress.Commands.add('resolve', (name, options = {}) => {
    const getValue = () => {
        const win = cy.state('window');
        return win[name];
    };
    const resolveValue = () => {
        return Cypress.Promise.try(getValue).then(value => {
            return cy.verifyUpcomingAssertions(value, options, {
                onRetry: resolveValue
            });
        });
    };

    return resolveValue();
});

and then ...

        cy.resolve('google').should(value => {
            expect(value).to.be.ok;
        })

We have a recipe https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/testing-dom__wait-for-resource showing how to wait for delayed style, JavaScript resource or even an image to load.

Released in 5.1.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v5.1.0, please open a new issue.

The features requested in this issue are now possible as part of cy.route2().

cy.route2() is currently experimental and requires being enabled by passing "experimentalNetworkStubbing": true through your Cypress configuration. This will eventually be merged in as part of our standard API.

Please see the cy.route2() docs for full details: https://on.cypress.io/route2

If you encounter any issues or unexpected behavior while using cy.route2() we encourage you to open a new issue so that we can work out all the issues before public release. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

egucciar picture egucciar  Â·  3Comments

jennifer-shehane picture jennifer-shehane  Â·  3Comments

rbung picture rbung  Â·  3Comments

simonhaenisch picture simonhaenisch  Â·  3Comments

dkreft picture dkreft  Â·  3Comments