Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cypress.io - wait for an lazy loaded js-file

Tags:

cypress

we want to test a webpage that does an ajax-request after clicking a button.

We are able to wait for the response of this ajax-request by defining a cy.route()

cy.server()
cy.route("POST", '/exampleAjax').as('exampleAjax')
cy.get('.button').click()
cy.wait('@exampleAjax')

Within the onComplete-Block of the ajax-Response we create an script-Tag and insert it:

new Ajax.Request( "exampleAjax", {
    method: "post",
    parameters: {'data-id': dataID},
    onComplete: function(transport) {
        var snode = document.createElement('script');  
        snode.setAttribute('type','text/javascript');                  
        snode.setAttribute('src','/some.js');
        document.getElementsByTagName('head')[0].appendChild(snode); 
    }
});

Now we want to wait for some.js to be loaded and tried

cy.route("GET", '/some.js').as('some_js')
cy.wait('@some_js')

But this does not work. How can we achieve this?

like image 736
toasty Avatar asked Jan 23 '19 15:01

toasty


People also ask

How do you wait until the page is loaded in Cypress?

wait() command. Test will only continue once that command is finished. Finding the right request to intercept is a great way to make sure that Cypress will wait until page loads with all the right data loaded.

Does Cy Visit wait for page to load?

By default, Cypress will wait for your page to load all resources before continuing the test. Your cy. wait() shouldn't be necessary.

What can I use instead of a Cy wait?

type() and should() will both retry until they succeed.


2 Answers

This is not possible with cypress currently. Cypress team is working on network stubbing. Take a look at this issue.

like image 122
Art713 Avatar answered Oct 04 '22 03:10

Art713


While we are still waiting for the feature request linked by Art713 we found a workaround for us:

We let cypress check for a global var / function that is defined within the lazy-loaded js-file:

cy.window().its('<any global var / function>')

In our case we also increased the defaultCommandTimeout within cypress.json in order to avoid a timeout after 4 seconds

{
  "defaultCommandTimeout": 10000
}
like image 36
toasty Avatar answered Oct 04 '22 02:10

toasty