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

Add ability to wait for script to load (like cy.script('js') + cy.wait()) #1296

Closed
bensampaio opened this issue Feb 12, 2018 · 10 comments · Fixed by #4176
Closed

Add ability to wait for script to load (like cy.script('js') + cy.wait()) #1296

bensampaio opened this issue Feb 12, 2018 · 10 comments · Fixed by #4176
Assignees
Labels
type: feature New feature that does not currently exist

Comments

@bensampaio
Copy link

bensampaio commented Feb 12, 2018

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
@bahmutov
Copy link
Contributor

bahmutov commented Feb 12, 2018 via email

@brian-mann
Copy link
Member

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.

#687

@jennifer-shehane jennifer-shehane added stage: proposal 💡 No work has been done of this issue type: feature New feature that does not currently exist labels Jan 25, 2019
@tbell511
Copy link

Is this possible yet?

@Cubelaster
Copy link

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.

@jennifer-shehane jennifer-shehane changed the title Waiting for script to load Add ability to wait for script to load (like cy.script('js') + cy.wait()) Mar 29, 2019
@isaquediasm
Copy link

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

@HolyNoodle
Copy link

Any updates on the subject ?

@malixsys
Copy link

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

@bahmutov
Copy link
Contributor

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.

@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: proposal 💡 No work has been done of this issue labels Jun 24, 2020
@cypress-bot cypress-bot bot added stage: work in progress stage: needs review The PR code is done & tested, needs review and removed stage: needs review The PR code is done & tested, needs review stage: work in progress labels Aug 25, 2020
@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Aug 31, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Sep 1, 2020

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.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Sep 1, 2020
@jennifer-shehane
Copy link
Member

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!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: feature New feature that does not currently exist
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants